fix: allow master process to kill worker

This commit is contained in:
Alexis Tyler
2020-02-16 12:47:13 +10:30
parent 048558b9ed
commit 26e05986d0

View File

@@ -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