From 26e05986d0d02bcb80611c443a8e74eb42f85a67 Mon Sep 17 00:00:00 2001 From: Alexis Tyler Date: Sun, 16 Feb 2020 12:47:13 +1030 Subject: [PATCH] fix: allow master process to kill worker --- index.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 04b12d28b..50231d866 100644 --- a/index.js +++ b/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(' 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