Fix cluster for later node versions

This commit is contained in:
joeuhren
2020-11-20 14:22:08 -07:00
parent 6b7dc4e4ce
commit 988bf5b719
+17 -7
View File
@@ -11,11 +11,16 @@ if (cluster.isMaster) {
//ensure workers exit cleanly //ensure workers exit cleanly
process.on('SIGINT', function() { process.on('SIGINT', function() {
console.log('Cluster shutting down..'); console.log('Cluster shutting down..');
for (var id in cluster.workers) { setTimeout(function() {
cluster.workers[id].kill(); for (var id in cluster.workers) {
} console.log('Worker shutting down (' + id + ')');
// exit the master process cluster.workers[id].kill();
process.exit(0); }
setTimeout(function() {
// exit the master process
process.exit(0);
}, 3000);
}, 1000);
}); });
// Count the machine's CPUs // Count the machine's CPUs
@@ -27,8 +32,13 @@ if (cluster.isMaster) {
} }
// Listen for dying workers // Listen for dying workers
cluster.on('exit', function () { cluster.on('exit', function (worker, code, signal) {
cluster.fork(); 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();
}
}); });
} }
}); });