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