CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/servers/pid-file.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { writeFile } from "node:fs/promises";
7
import { session_id, start_ts } from "@cocalc/project/consts";
8
import {
9
projectPidFile,
10
sessionIDFile,
11
startTimestampFile,
12
} from "@cocalc/project/data";
13
import { pidUpdateIntervalMs } from "@cocalc/util/project-info";
14
import { getLogger } from "@cocalc/project/logger";
15
16
const logger = getLogger("pid-file");
17
18
export default async function init() {
19
logger.debug("init -- writing out initial pid file info");
20
await Promise.all([
21
writeFile(projectPidFile, `${process.pid}`),
22
writeFile(startTimestampFile, `${start_ts}`),
23
writeFile(sessionIDFile, `${session_id}`),
24
]);
25
26
// we also write the pid file out periodically so that the server
27
// knows *this* particular project is really alive and working.
28
setInterval(async () => {
29
try {
30
logger.debug("updating ", projectPidFile);
31
await writeFile(projectPidFile, `${process.pid}`);
32
} catch (err) {
33
// this will likely result in the server killing the project...
34
logger.debug("ERROR updating ", projectPidFile, err);
35
}
36
}, pidUpdateIntervalMs);
37
}
38
39