import { writeFile } from "node:fs/promises";
import { session_id, start_ts } from "@cocalc/project/consts";
import {
projectPidFile,
sessionIDFile,
startTimestampFile,
} from "@cocalc/project/data";
import { pidUpdateIntervalMs } from "@cocalc/util/project-info";
import { getLogger } from "@cocalc/project/logger";
const logger = getLogger("pid-file");
export default async function init() {
logger.debug("init -- writing out initial pid file info");
await Promise.all([
writeFile(projectPidFile, `${process.pid}`),
writeFile(startTimestampFile, `${start_ts}`),
writeFile(sessionIDFile, `${session_id}`),
]);
setInterval(async () => {
try {
logger.debug("updating ", projectPidFile);
await writeFile(projectPidFile, `${process.pid}`);
} catch (err) {
logger.debug("ERROR updating ", projectPidFile, err);
}
}, pidUpdateIntervalMs);
}