35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
|
|
// Keeps the dashboard server restartable without losing the process the
|
||
|
|
// launcher (or the console) holds: this process only runs server.js as a
|
||
|
|
// worker and starts it again when it exits with RESTART_CODE ("Neu starten"
|
||
|
|
// in the UI after the code was updated). Any other exit ends this process too.
|
||
|
|
// The worker ends itself when this process goes away (IPC disconnect), so
|
||
|
|
// closing the window never leaves a server behind.
|
||
|
|
const { fork } = require('child_process')
|
||
|
|
|
||
|
|
const RESTART_CODE = 75
|
||
|
|
|
||
|
|
function supervise(script) {
|
||
|
|
let child = null
|
||
|
|
const start = () => {
|
||
|
|
child = fork(script, process.argv.slice(2), {
|
||
|
|
env: { ...process.env, DASHBOARD_WORKER: '1' },
|
||
|
|
stdio: 'inherit',
|
||
|
|
})
|
||
|
|
child.on('exit', (code, signal) => {
|
||
|
|
if (code === RESTART_CODE) {
|
||
|
|
console.log('Dashboard wird neu gestartet …')
|
||
|
|
start()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
process.exit(code ?? (signal ? 1 : 0))
|
||
|
|
})
|
||
|
|
}
|
||
|
|
// Ctrl+C / normal end: take the worker along
|
||
|
|
const stop = () => { try { child?.kill() } catch {} process.exit(0) }
|
||
|
|
process.on('SIGINT', stop)
|
||
|
|
process.on('SIGTERM', stop)
|
||
|
|
start()
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = { supervise, RESTART_CODE }
|