You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
4.2 KiB
125 lines
4.2 KiB
'use strict'; |
|
// DSH memory bridge supervisor. |
|
// Runs bridge/server.js in a loop. Exit codes: |
|
// 0 - clean stop (POST /shutdown) -> supervisor exits |
|
// 42 - restart requested (/shutdown {restart:true}) -> wait, restart |
|
// other - crash; if the bridge never became ready, count as startup failure |
|
// After MAX_FAILS consecutive startup failures the supervisor gives up. |
|
const { spawn } = require('child_process'); |
|
const net = require('net'); |
|
const path = require('path'); |
|
const fs = require('fs'); |
|
|
|
const ROOT = path.join(__dirname, '..'); |
|
const BRIDGE = path.join(ROOT, 'bridge', 'server.js'); |
|
const CONFIG = path.join(ROOT, 'bridge', 'config.json'); |
|
const DELAY_MS = 5000; |
|
const READY_WAIT_MS = 3000; |
|
const MAX_FAILS = 3; |
|
const PORT_FREE_WAIT_MS = 10000; |
|
|
|
function readPort() { |
|
try { return JSON.parse(fs.readFileSync(CONFIG, 'utf8')).port || 8766; } |
|
catch { return 8766; } |
|
} |
|
|
|
function portOpen(port) { |
|
return new Promise((resolve) => { |
|
const s = net.connect({ host: '127.0.0.1', port }); |
|
let done = false; |
|
const fin = (ok) => { if (done) return; done = true; try { s.destroy(); } catch {} resolve(ok); }; |
|
s.setTimeout(600); |
|
s.on('connect', () => fin(true)); |
|
s.on('timeout', () => fin(false)); |
|
s.on('error', () => fin(false)); |
|
}); |
|
} |
|
|
|
const ts = () => new Date().toISOString(); |
|
let fails = 0; |
|
|
|
// PID lock: prevents a second supervisor from starting when the first is alive. |
|
const LOCK = path.join(__dirname, '.supervisor.pid'); |
|
function pidAlive(pid) { |
|
try { process.kill(pid, 0); return true; } |
|
catch (e) { return e.code === 'EPERM'; } |
|
} |
|
function acquireLock() { |
|
try { |
|
const raw = fs.readFileSync(LOCK, 'utf8').trim(); |
|
const old = parseInt(raw, 10); |
|
if (old && old !== process.pid && pidAlive(old)) { |
|
console.log(ts() + ' [supervisor] another instance running (pid ' + old + '), exiting'); |
|
process.exit(0); |
|
} |
|
} catch {} |
|
try { fs.writeFileSync(LOCK, String(process.pid), 'utf8'); } |
|
catch (e) { console.warn(ts() + ' [supervisor] lock write failed: ' + (e && e.message)); } |
|
} |
|
function releaseLock() { |
|
try { |
|
const raw = fs.readFileSync(LOCK, 'utf8').trim(); |
|
if (parseInt(raw, 10) === process.pid) fs.unlinkSync(LOCK); |
|
} catch {} |
|
} |
|
|
|
async function runOnce() { |
|
const port = readPort(); |
|
const waitStart = Date.now(); |
|
while (await portOpen(port)) { |
|
if (Date.now() - waitStart > PORT_FREE_WAIT_MS) { |
|
console.error(ts() + ' [supervisor] port ' + port + ' still busy after ' + PORT_FREE_WAIT_MS + 'ms, giving up'); |
|
process.exit(1); |
|
} |
|
await new Promise(r => setTimeout(r, 500)); |
|
} |
|
|
|
console.log(ts() + ' [supervisor] starting bridge (port ' + port + ')'); |
|
const child = spawn(process.execPath, [BRIDGE], { |
|
cwd: ROOT, |
|
stdio: 'inherit', |
|
env: Object.assign({}, process.env, { DSH_SUPERVISOR: '1' }), |
|
}); |
|
|
|
let ready = false; |
|
const readyTimer = setTimeout(async () => { |
|
if (await portOpen(port)) { |
|
ready = true; |
|
fails = 0; |
|
console.log(ts() + ' [supervisor] bridge is up on port ' + port); |
|
} |
|
}, READY_WAIT_MS); |
|
|
|
const code = await new Promise((resolve) => { |
|
child.on('exit', (c) => resolve(c === null ? -1 : c)); |
|
child.on('error', () => resolve(-2)); |
|
}); |
|
clearTimeout(readyTimer); |
|
|
|
if (code === 0) { |
|
console.log(ts() + ' [supervisor] clean stop, exiting'); |
|
process.exit(0); |
|
} |
|
if (code === 42) { |
|
console.log(ts() + ' [supervisor] restart requested, waiting ' + DELAY_MS + 'ms'); |
|
setTimeout(runOnce, DELAY_MS); |
|
return; |
|
} |
|
if (!ready) { |
|
fails++; |
|
console.log(ts() + ' [supervisor] startup failure ' + fails + '/' + MAX_FAILS + ' (exit ' + code + ')'); |
|
if (fails >= MAX_FAILS) { |
|
console.error(ts() + ' [supervisor] too many failures, giving up. Check the log above.'); |
|
process.exit(1); |
|
} |
|
} else { |
|
console.log(ts() + ' [supervisor] bridge crashed after running (exit ' + code + '), restarting'); |
|
} |
|
setTimeout(runOnce, DELAY_MS); |
|
} |
|
|
|
process.on('exit', releaseLock); |
|
process.on('SIGINT', () => { console.log(ts() + ' [supervisor] interrupted'); process.exit(0); }); |
|
process.on('SIGTERM', () => { console.log(ts() + ' [supervisor] terminated'); process.exit(0); }); |
|
acquireLock(); |
|
runOnce().catch((e) => { console.error(ts() + ' [supervisor] fatal: ' + (e && e.message)); process.exit(1); });
|
|
|