Skip to content

Commit

Permalink
Dont keep reforking forever
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Crocker committed Dec 19, 2019
1 parent 633701f commit b4544f7
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions bin/squirrel.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,21 @@ function run(opts) {
numCPUs = require("os").cpus().length;

var numworkers = process.env.WORKER_COUNT || numCPUs;
var failureCount = 0;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numworkers; i++) {
cluster.fork();
}

cluster.on("exit", (worker, code, signal) => {
// Restart the worker
var worker = cluster.fork();
console.log(`worker ${worker.process.pid} died and has been replaced by ${worker.process.pid}`);
cluster.on("exit", (oldWorker, code, signal) => {
failureCount += 1;
if (failureCount < numworkers * 3) { // Dont restart forever, only allow 3 restarts per worker.
var worker = cluster.fork();
console.log(`worker ${oldWorker.process.pid} died and has been replaced by ${worker.process.pid}`);
} else {
console.log(`worker ${oldWorker.process.pid} died. Not starting a new worker because of too many failures.`);
}
});
} else {
console.log("Starting worker");
Expand Down

0 comments on commit b4544f7

Please sign in to comment.