Master.prototype.respawn = function respawn(worker) {
var index = this.workers.indexOf(worker);
if (index !== -1)
this.workers.splice(index, 1);
this.spawnWorker();
};
Suppose we have a workers list [w1, w2, w3];
When w2 crashs down, master will create a new worker to replace it. Then the works list will become [w1, w3, w4]
w3 will not receive the old sessions any more.
To fix this issue, just set the worker to the correct index;
Master.prototype.spawnWorker = function spawnWorker(index ) {
var worker = cluster.fork(this.env);
var self = this;
worker.on('exit', function(code) {
debug('worker=%d died with code=%d', worker.process.pid, code);
self.spawn(index); // CHANGE here.
});
Master.prototype.respawn = function respawn(worker) { var index = this.workers.indexOf(worker); if (index !== -1) this.workers.splice(index, 1); this.spawnWorker(); }; Suppose we have a workers list [w1, w2, w3]; When w2 crashs down, master will create a new worker to replace it. Then the works list will become [w1, w3, w4] w3 will not receive the old sessions any more.
To fix this issue, just set the worker to the correct index; Master.prototype.spawnWorker = function spawnWorker(index ) { var worker = cluster.fork(this.env);
var self = this; worker.on('exit', function(code) { debug('worker=%d died with code=%d', worker.process.pid, code); self.spawn(index); // CHANGE here. });
worker.on('message', function(msg) { // Graceful exit if (msg.type === 'close') self.respawn(worker); });
debug('worker=%d spawn', worker.process.pid); this.workers[index] = worker; // CHANGE here. }
Also we need to change the line in construct function: for (var i = 0; i < workerCount; i++) this.spawnWorker(i); // CHANGE here.