Files
purple-explorer/bin/cluster
T
Joe Uhren bae4d50087 Various core improvements and easier updating
-The update_explorer.js script has been improved with better spacing and the ability to restart the explorer automatically to ensure new changes take effect immidiately (works with npm start, pm2 and forever)
-The code to compile scss to css has been moved from the prestart script into its own compile_css.js script which is now called from the update-explorer.js script to apply css changes after update
-The cluster code now handles a custom restart msg which is used to restart the explorer from the update explorer process
-Pm2 and Forever are now referenced by the name 'explorer' instead of ./bin/instance or ./bin/cluster [SEE IMPORTANT NOTE BELOW]
-Added reload/restart scripts to the package.json for pm2 and forever
-Pm2 and forever now write a pid file to the tmp directory when started. NOTE: Forever is now started from the prestart script due to a bug in forever that prevents the pid from being written to a different directory without the absolute path
-Fixed a bug which caused the prestart script to be run twice when starting the explorer with `npm start`
-The cluster code now accepts a numeric argument to force a specific number of instances to be loaded
-The `npm run start-instance` cmd now loads using the cluster code with a single instance
-The is_locked function now accepts an optional 'silent' argument to prevent displaying msgs while checking for pid/lock files
-Added some process.exit statements to the stop_explorer.js file
-Updated the README with cmd changes from package.json and updated description of the "Update Explorer Script"

IMPORTANT NOTE: It is strongly recommended to stop the explorer before performing this update. If the explorer is running while you perform this update, you will need to stop and restart the explorer for this update to fully take effect. Because of the changes in this commit, stopping the explorer using the built-in pm2 and/or forever stop cmds will not work and you will need to type out the full stop cmd this one time only, and going forward from now on you should no longer need to even stop the explorer for any update as it is now built into the update cmd.

If running using pm2 and you cannot stop the explorer, you can use stop using the following full cmd syntax:

Windows:
pm2 stop ./bin/instance

Linux and other OS's:
node node_modules/pm2/bin/pm2 stop ./bin/instance

If running using forever and you cannot stop the explorer, you can use stop using the following full cmd syntax:

All OS's:
node node_modules/forever/bin/forever stop ./bin/cluster
2022-07-03 19:13:50 -06:00

89 lines
2.8 KiB
Plaintext

var cluster = require('cluster');
if (cluster.isMaster) {
const isWinOS = process.platform == 'win32';
const lib = require('../lib/explorer');
const instances = (process.argv[2] != null && process.argv[2] != '' && !isNaN(process.argv[2]) && Number.isInteger(parseFloat(process.argv[2])) ? parseInt(process.argv[2]) : require('os').cpus().length);
console.log('Starting cluster with pid: ' + process.pid);
function startWorkers() {
// create worker instances
for (var i = 0; i < instances; i += 1)
cluster.fork();
}
function killAllWorkers(isRestart) {
// send kill cmd to all workers
for (var id in cluster.workers) {
console.log('Worker (' + id + ') shutting down...');
if (!isWinOS || isRestart) {
// only kill the worker if not on windows (otherwise an error is displayed) or if the cluster is being restarted
process.kill(cluster.workers[id]['process']['pid'], 'SIGINT');
}
}
}
function waitForWorkerShutdown(isRestart) {
if (Object.keys(cluster.workers).length > 0) {
// continue waiting since worker threads are still open
setTimeout(waitForWorkerShutdown, 100, isRestart);
} else {
// all worker threads have closed
// check if the cluster process should stop or if the workers should be restarted
if (isRestart) {
// start new worker threads
startWorkers();
} else {
// exit the master process
if (isWinOS) {
// command line in windows doesn't seem to release itself
// show a message that the app has finished
console.log('The application is now safe to close');
}
process.exit(0);
}
}
}
// ensure workers exit cleanly
process.on('SIGINT', () => {
console.log('Cluster shutting down..');
// send kill cmd to all workers
killAllWorkers(false);
// wait for workers to stop
waitForWorkerShutdown(false);
});
startWorkers();
// 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['process']['exitCode'] !== 999 && worker.exitedAfterDisconnect !== true) {
console.log('Cluster restarting...');
cluster.fork();
}
});
// listen for the restart cmd
cluster.on('message', function (data, msg) {
// check if this was the restart cmd
if (msg != null && msg == 'restart') {
console.log('Cluster restarting..');
// send kill cmd to all workers
killAllWorkers(true);
// wait for workers to stop
waitForWorkerShutdown(true);
}
});
} else
require('./instance');