mirror of
https://github.com/unraid/api.git
synced 2026-01-03 15:09:48 -06:00
fix: allow master process to kill worker
This commit is contained in:
19
index.js
19
index.js
@@ -14,8 +14,14 @@ process.chdir(__dirname);
|
||||
const RESTART_ATTEMPTS = 10;
|
||||
let currentRestartAttempt = 0;
|
||||
let currentWorker;
|
||||
let restart = true;
|
||||
|
||||
// @see https://github.com/nodejs/node-v0.x-archive/blob/master/doc/api/process.markdown#exit-codes
|
||||
const onWorkerExit = (_, code) => {
|
||||
if (!restart) {
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// Reload worker
|
||||
if (code === null || code === 0) {
|
||||
const newWorker = cluster.fork();
|
||||
@@ -26,12 +32,6 @@ const onWorkerExit = (_, code) => {
|
||||
return;
|
||||
}
|
||||
|
||||
// Unknown error, kill process
|
||||
// @see https://github.com/nodejs/node-v0.x-archive/blob/master/doc/api/process.markdown#exit-codes
|
||||
if (code !== 7) {
|
||||
process.exit(code);
|
||||
}
|
||||
|
||||
// Too many restarts, kill process
|
||||
if (currentRestartAttempt >= RESTART_ATTEMPTS) {
|
||||
log.debug(`No restart attempts left. Exiting.`);
|
||||
@@ -75,6 +75,13 @@ if (cluster.isMaster) {
|
||||
log.debug('<master> Updating log level.');
|
||||
currentWorker.send('SIGUSR1');
|
||||
});
|
||||
|
||||
// Kill all workers then exit gracefully
|
||||
process.on('SIGTERM', () => {
|
||||
log.info(`Killing worker`);
|
||||
restart = false;
|
||||
currentWorker.send('shutdown');
|
||||
});
|
||||
}
|
||||
|
||||
// Worker
|
||||
|
||||
Reference in New Issue
Block a user