20c0a382a3
-Replace tabs with double-spaces -Betting spacing and lining up of code functions -Add missing semi-colons -Remove extra characters and spaces where applicable -Remove commented-out code fragments -Add missing 2021 date to LICENSE -Small touchups and other misc nitpickings
44 lines
1.3 KiB
Plaintext
44 lines
1.3 KiB
Plaintext
var cluster = require('cluster');
|
|
var fs = require('fs');
|
|
|
|
if (cluster.isMaster) {
|
|
fs.writeFile('./tmp/cluster.pid', process.pid.toString(), function (err) {
|
|
if (err) {
|
|
console.log('Error: unable to create cluster.pid');
|
|
process.exit(1);
|
|
} else {
|
|
console.log('Starting cluster with pid: ' + process.pid);
|
|
|
|
// ensure workers exit cleanly
|
|
process.on('SIGINT', function() {
|
|
console.log('Cluster shutting down..');
|
|
|
|
for (var id in cluster.workers) {
|
|
console.log('Worker shutting down (' + id + ')');
|
|
cluster.workers[id].kill();
|
|
}
|
|
|
|
// exit the master process
|
|
process.exit(0);
|
|
});
|
|
|
|
// 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 shut down.');
|
|
else if ((signal != 'SIGINT') && (worker['process']['exitCode'] !== 0) && (worker.exitedAfterDisconnect !== true)) {
|
|
console.log('Cluster restarting...');
|
|
cluster.fork();
|
|
}
|
|
});
|
|
}
|
|
});
|
|
} else
|
|
require('./instance'); |