Files
AtreidesAssetsFree/server/supervisor.mjs
T

67 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2026-10-29 11:57:29 +01:00
// Generated, do not edit. sha256:9c19f153125a535056932eee985b7b6abf166ab4af48fe08f3cd2fb0dbf6edfd
import { spawn } from 'node:child_process';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { CONFIG } from '../shared/config.js';
const HIER = dirname(fileURLToPath(import.meta.url));
const SERVER = join(HIER, 'index.mjs');
const STOPSIGNALEN = ['SIGINT', 'SIGTERM', 'SIGHUP'];
let kind = null;
let stoppen = false;
let eersteStart = true;
function startServer() {
const env = { ...process.env, ASSETAPP_SUPERVISED: '1' };
if (!eersteStart) delete env.ASSETAPP_OPEN;
eersteStart = false;
kind = spawn(process.execPath, [SERVER], {
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
env,
});
kind.on('exit', (code, signal) => {
kind = null;
if (stoppen) return;
if (code === CONFIG.PORTFOLIO.RESTART_EXIT_CODE) {
console.log('[supervisor] portefeuille gewisseld; de server start opnieuw.');
startServer();
return;
}
if (signal) {
console.log(`[supervisor] de server stopte op signaal ${signal}.`);
process.exit(1);
}
process.exit(code ?? 0);
});
kind.on('error', (e) => {
console.error(`[supervisor] kan de server niet starten: ${e.message}`);
process.exit(1);
});
}
for (const signaal of STOPSIGNALEN) {
process.on(signaal, () => {
stoppen = true;
if (!kind) process.exit(0);
kind.kill(signaal);
const hard = setTimeout(() => { if (kind) kind.kill('SIGKILL'); }, 10_000);
hard.unref();
});
}
process.on('exit', () => { if (kind) kind.kill('SIGKILL'); });
process.on('uncaughtException', (e) => {
console.error(`[supervisor] onverwachte fout: ${e.message}`);
if (kind) kind.kill('SIGKILL');
process.exit(1);
});
if (resolve(process.argv[1] ?? '') === resolve(fileURLToPath(import.meta.url))) startServer();
export { STOPSIGNALEN, SERVER };