From 988bf5b719107e370bbeab14439ff2a4b7b9182b Mon Sep 17 00:00:00 2001 From: joeuhren <46763106+joeuhren@users.noreply.github.com> Date: Fri, 20 Nov 2020 14:22:08 -0700 Subject: [PATCH] Fix cluster for later node versions --- bin/cluster | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/bin/cluster b/bin/cluster index 7bcdd66..493d1c1 100644 --- a/bin/cluster +++ b/bin/cluster @@ -11,11 +11,16 @@ if (cluster.isMaster) { //ensure workers exit cleanly process.on('SIGINT', function() { console.log('Cluster shutting down..'); - for (var id in cluster.workers) { - cluster.workers[id].kill(); - } - // exit the master process - process.exit(0); + setTimeout(function() { + for (var id in cluster.workers) { + console.log('Worker shutting down (' + id + ')'); + cluster.workers[id].kill(); + } + setTimeout(function() { + // exit the master process + process.exit(0); + }, 3000); + }, 1000); }); // Count the machine's CPUs @@ -27,8 +32,13 @@ if (cluster.isMaster) { } // Listen for dying workers - cluster.on('exit', function () { - cluster.fork(); + cluster.on('exit', function (worker, code, signal) { + if (worker['process']['exitCode'] === 0) { + console.log('Worker shut down.'); + } else if ((signal != 'SIGINT') && (worker['process']['exitCode'] !== 0) && (worker.exitedAfterDisconnect !== true)) { + console.log('Cluster restarting...'); + cluster.fork(); + } }); } });