-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.mjs
45 lines (37 loc) · 1 KB
/
server.mjs
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 * as dotenv from "dotenv";
dotenv.config({
path: process.env.CONFIG_FILE || "./data/config",
});
import express from "express";
import { fork } from "node:child_process";
import { log } from "./lib/log.mjs";
const syncInterval = Number(process.env.SYNC_INTERVAL_MS || 60000);
const processTimeout = Number(process.env.PROCESS_TIMEOUT_MS || 60000);
let lastSuccess = 0;
function runTask() {
const cp = fork("./index.mjs", {
stdio: "inherit",
});
const t = setTimeout(() => {
log("Killing process by timeout");
cp.kill("SIGKILL");
}, processTimeout);
cp.on("exit", (code) => {
if (code === 0) {
lastSuccess = Date.now();
}
setTimeout(runTask, syncInterval);
clearTimeout(t);
});
}
const app = express();
app.disable("x-powered-by");
app.get("/status", (_, res) => {
const isOk = lastSuccess > Date.now() - syncInterval * 4;
res.status(isOk ? 200 : 500).send();
});
app.listen(
Number(process.env.SERVER_PORT || 3000),
process.env.SERVER_HOST,
runTask
);