Files
purple-explorer/bin/cluster
T
joeuhren f948e29085 Cluster updates + add new start/stop options
-More graceful shutdown of node cluster on 'npm stop' with better cleanup of resources on exit
-Added new stop_explorer.sh script which looks up the explorer port # via settings file and closes the application running on that port # instead of saving and killing the process by pid as it did before
-Added support for pm2 and forever using 'npm run start-pm2' and 'npm run start-forever' respectively
-pm2 is automatically installed when starting with 'npm run start-pm2' if it is not already installed
-forever is automatically installed when starting with 'npm run start-forever' if it is not already installed
-Updated existing npm commands in package.json by replacing hardcoded 'node' with '$(which node)'
-/path/to/nodejs changed to /path/to/node in the /settings.json.template, /lib/settings.js and /scripts/sync.js files
-README updates:
 -Added a new 'Start/Stop the Explorer' section
 -Added PM2 instructions to the 'Start/Stop the Explorer' section
 -Moved Start/Stop Explorer instructions to the 'Start/Stop the Explorer' section
 -Moved Forever instructions to the 'Start/Stop the Explorer' section
 -/path/to/nodejs changed to /path/to/node
 -Some additional small misc fixes
2021-04-17 13:26:05 -06:00

48 lines
1.4 KiB
Plaintext

var cluster = require('cluster');
var fs = require('fs');
if (cluster.isMaster) {
console.log('Starting cluster with pid: ' + process.pid);
// ensure workers exit cleanly
process.on('SIGINT', () => {
console.log('Cluster shutting down..');
// send kill cmd to all workers
for (var id in cluster.workers) {
console.log('Worker (' + id + ') shutting down...');
process.kill(cluster.workers[id]['process']['pid'], 'SIGINT');
}
function waitForWorkerShutdown () {
if (Object.keys(cluster.workers).length > 0) {
// continue waiting since worker threads are still open
setTimeout(waitForWorkerShutdown, 100);
} else {
// all worker threads have closed
// now exit the master process
process.exit(0);
}
}
waitForWorkerShutdown();
});
// count the machine's CPUs
var cpuCount = require('os').cpus().length;
// create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1)
cluster.fork();
// listen for dying workers
cluster.on('exit', function (worker, code, signal) {
if (worker['process']['exitCode'] === 0) {
console.log('Worker (' + worker['id'] + ') shutdown complete');
} else if ((signal != 'SIGINT') && (worker['process']['exitCode'] !== 0) && (worker.exitedAfterDisconnect !== true)) {
console.log('Cluster restarting...');
cluster.fork();
}
});
} else
require('./instance');