Skip to content

Commit

Permalink
Merge pull request #19 from trailbehind/dont-retry-forever
Browse files Browse the repository at this point in the history
Dont keep reforking forever
  • Loading branch information
Jesse Crocker committed Dec 19, 2019
2 parents 633701f + b4544f7 commit a218c23
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 a218c23

Please sign in to comment.