blob: 2237f9e1bb8fce7720403202b9fb10b6e861f2f6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
import { writeFileSync, unlinkSync, existsSync, mkdirSync } from "fs";
import { pathFromRoot, log_execution, spawn_detached_process } from './ActionUtilities';
import { resolve } from "path";
import { red, yellow } from "colors";
const daemonPath = pathFromRoot("./src/server/daemon/persistence_daemon.ts");
export namespace ProcessManager {
export async function initialize() {
const logPath = pathFromRoot("./logs");
const filePath = resolve(logPath, "./server_pids.txt");
const exists = existsSync(logPath);
if (exists) {
unlinkSync(filePath);
} else {
mkdirSync(logPath);
}
const { pid } = process;
if (process.env.SPAWNED === "true") {
writeFileSync(filePath, `${pid} created at ${new Date().toUTCString()}\n`);
}
}
let daemonInitialized = false;
export async function trySpawnDaemon() {
if (!daemonInitialized) {
daemonInitialized = true;
await log_execution({
startMessage: "\ninitializing persistence daemon",
endMessage: ({ result, error }) => {
const success = error === null && result !== undefined;
if (!success) {
console.log(red("failed to initialize the persistance daemon"));
process.exit(0);
}
return "persistence daemon process closed";
},
action: () => spawn_detached_process("npx ts-node", [daemonPath]),
color: yellow
});
}
}
}
|