Files

58 lines
1.4 KiB
JavaScript

// EST 서버 감시: 작업 데이터 디렉터리는 감시하지 않는다.
const { spawn } = require("node:child_process");
const fs = require("node:fs");
const path = require("node:path");
const base = __dirname;
let child,
timer,
restarting = false,
stopping = false;
function start() {
child = spawn(process.execPath, [path.join(base, "server.cjs")], {
cwd: base,
env: process.env,
stdio: "inherit",
});
child.on("exit", (code, signal) => {
if (stopping) return;
if (restarting) {
restarting = false;
start();
} else {
console.error(`[supervisor] 서버 종료: ${code ?? signal}`);
process.exit(code || 1);
}
});
}
const watcher = fs.watch(base, (event, filename) => {
const name = filename && filename.toString();
if (!name || name === "watch-server.cjs" || !/\.(?:js|cjs|html|css)$/i.test(name)) return;
clearTimeout(timer);
timer = setTimeout(() => {
if (restarting || stopping) return;
restarting = true;
console.log(`[supervisor] 변경 감지: ${name}`);
child.kill();
}, 300);
});
watcher.on("error", (error) => {
console.error(`[supervisor] 감시 오류: ${error.message}`);
stopping = true;
child.kill();
process.exit(1);
});
for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) {
process.on(signal, () => {
stopping = true;
clearTimeout(timer);
watcher.close();
child.kill();
process.exit(0);
});
}
start();