Comparing version
@@ -63,2 +63,44 @@ 'use strict'; | ||
if (clusterWorker.restarts.allowed !== false && | ||
clusterWorker.restarts.automatic < clusterWorker.restarts.allowed) { | ||
forks[clusterWorker.id].once('exit', function(code, signal) { | ||
if (shutdown) { | ||
return; | ||
} | ||
// if `signal` is defined, it means | ||
// - master killed the worker | ||
// - someone from the outside killed the worker | ||
// In both cases, we do not want to restart the worker | ||
// If you want to restart the worker, use the REPL | ||
if (code === 0 || signal) { | ||
masterWorker.emit('worker stopped', clusterWorker); | ||
clusterWorker.status = 'stopped'; | ||
forks[clusterWorker.id] = null; | ||
return; | ||
} | ||
clearTimeout(autoRestart); | ||
autoRestart = setTimeout(function restart() { | ||
clusterWorker.restarts.automatic++; | ||
forks[clusterWorker.id] = null; | ||
clusterWorker.start(); | ||
clusterWorker.status = 'restarting'; | ||
masterWorker.emit('worker restarted', clusterWorker); | ||
}, 1 * 1000); | ||
}); | ||
} else { | ||
forks[clusterWorker.id].once('exit', function workerStopped() { | ||
if (shutdown) { | ||
return; | ||
} | ||
masterWorker.emit('worker stopped', clusterWorker); | ||
clusterWorker.status = 'stopped'; | ||
forks[clusterWorker.id] = null; | ||
}); | ||
} | ||
async.series([ | ||
@@ -74,44 +116,2 @@ waitForReady.bind(null, clusterWorker), | ||
if (clusterWorker.restarts.allowed !== false && | ||
clusterWorker.restarts.automatic < clusterWorker.restarts.allowed) { | ||
forks[clusterWorker.id].once('exit', function(code, signal) { | ||
if (shutdown) { | ||
return; | ||
} | ||
// if `signal` is defined, it means | ||
// - master killed the worker | ||
// - someone from the outside killed the worker | ||
// In both cases, we do not want to restart the worker | ||
// If you want to restart the worker, use the REPL | ||
if (code === 0 || signal) { | ||
masterWorker.emit('worker stopped', clusterWorker); | ||
clusterWorker.status = 'stopped'; | ||
forks[clusterWorker.id] = null; | ||
return; | ||
} | ||
clearTimeout(autoRestart); | ||
autoRestart = setTimeout(function restart() { | ||
clusterWorker.restarts.automatic++; | ||
forks[clusterWorker.id] = null; | ||
clusterWorker.start(); | ||
clusterWorker.status = 'restarting'; | ||
masterWorker.emit('worker restarted', clusterWorker); | ||
}, 1 * 1000); | ||
}); | ||
} else { | ||
forks[clusterWorker.id].once('exit', function workerStopped() { | ||
if (shutdown) { | ||
return; | ||
} | ||
masterWorker.emit('worker stopped', clusterWorker); | ||
clusterWorker.status = 'stopped'; | ||
forks[clusterWorker.id] = null; | ||
}); | ||
} | ||
masterWorker.emit('worker started', clusterWorker); | ||
@@ -118,0 +118,0 @@ manualRestart = false; |
{ | ||
"name": "forkie", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "forkie likes your forks", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -388,3 +388,3 @@ describe('creating a graceful master process', function () { | ||
describe('when using automatic restart', function() { | ||
beforeEach(function(done) { | ||
beforeEach(function() { | ||
master = gracefulMaster([ | ||
@@ -395,59 +395,83 @@ 'a-restarted-module.js' | ||
}); | ||
}); | ||
process.nextTick(function() { | ||
forks[0].emit('message', { | ||
graceful: { | ||
status: 'ready', | ||
title: 'yeepee' | ||
} | ||
describe('and worker as started gracefully', function() { | ||
beforeEach(function(done) { | ||
process.nextTick(function() { | ||
forks[0].emit('message', { | ||
graceful: { | ||
status: 'ready', | ||
title: 'yeepee' | ||
} | ||
}); | ||
forks[0].emit('message', { | ||
graceful: { | ||
status: 'started' | ||
} | ||
}); | ||
done(); | ||
}); | ||
}); | ||
forks[0].emit('message', { | ||
graceful: { | ||
status: 'started' | ||
} | ||
describe('when fork exits with 1', function(done) { | ||
beforeEach(function () { | ||
forks[0].emit('exit', 1); | ||
// restart timeout | ||
this.clock.tick(1000); | ||
}); | ||
done(); | ||
}); | ||
}); | ||
it('calls fork again', function() { | ||
// two times: init and restart | ||
expect(fakeCp.fork).to.be.calledTwice; | ||
expect(fakeCp.fork).to.be.calledWithExactly('a-restarted-module.js'); | ||
}); | ||
describe('when fork exits with 1', function(done) { | ||
beforeEach(function () { | ||
forks[0].emit('exit', 1); | ||
// restart timeout | ||
this.clock.tick(1000); | ||
}); | ||
it('sends a restarted event', function() { | ||
expect(workerEmit).to.be.calledWithMatch('worker restarted', { | ||
restarts: { manual: 0, automatic: 1 } | ||
}); | ||
}); | ||
it('calls fork again', function() { | ||
// two times: init and restart | ||
expect(fakeCp.fork).to.be.calledTwice; | ||
expect(fakeCp.fork).to.be.calledWithExactly('a-restarted-module.js'); | ||
}); | ||
it('sends a restarted event', function() { | ||
expect(workerEmit).to.be.calledWithMatch('worker restarted', { | ||
restarts: { manual: 0, automatic: 1 } | ||
describe('when fork exits with 1 and a signal was sent', function(done) { | ||
beforeEach(function () { | ||
forks[0].emit('exit', 1, 'SIGKILL'); | ||
// restart timeout | ||
this.clock.tick(1000); | ||
}); | ||
}); | ||
}); | ||
it('do not call fork again', function() { | ||
expect(fakeCp.fork).to.be.calledOnce; | ||
}); | ||
describe('when fork exits with 1 and a signal was sent', function(done) { | ||
beforeEach(function () { | ||
forks[0].emit('exit', 1, 'SIGKILL'); | ||
// restart timeout | ||
this.clock.tick(1000); | ||
}); | ||
it('does not sends a restarted event', function() { | ||
expect(workerEmit).to.not.be.calledWithExactly('worker restarted', { | ||
id: 0, | ||
toFork: 'a-restarted-module.js', | ||
restarts: { manual: 0, automatic: 1 } | ||
}); | ||
}); | ||
it('do not call fork again', function() { | ||
expect(fakeCp.fork).to.be.calledOnce; | ||
}); | ||
it('does not sends a restarted event', function() { | ||
expect(workerEmit).to.not.be.calledWithExactly('worker restarted', { | ||
id: 0, | ||
toFork: 'a-restarted-module.js', | ||
restarts: { manual: 0, automatic: 1 } | ||
describe('when fork exits with 0', function(done) { | ||
beforeEach(function () { | ||
forks[0].emit('exit', 0); | ||
}); | ||
it('does not call fork again', function() { | ||
expect(fakeCp.fork).to.be.calledOnce; | ||
}); | ||
it('does not sends a restarted event', function() { | ||
expect(workerEmit).to.not.be.calledWithExactly('worker restarted', { | ||
id: 0, | ||
toFork: 'a-restarted-module.js', | ||
restarts: { manual: 0, automatic: 1 } | ||
}); | ||
}); | ||
}); | ||
@@ -457,23 +481,26 @@ | ||
describe('when fork exits with 0', function(done) { | ||
beforeEach(function () { | ||
forks[0].emit('exit', 0); | ||
}); | ||
describe('and worker did not yet start', function() { | ||
describe('when fork exits with 1', function() { | ||
beforeEach(function () { | ||
forks[0].emit('exit', 1); | ||
// restart timeout | ||
this.clock.tick(1000); | ||
}); | ||
it('does not call fork again', function() { | ||
expect(fakeCp.fork).to.be.calledOnce; | ||
}); | ||
it('calls fork again', function() { | ||
// two times: init and restart | ||
expect(fakeCp.fork).to.be.calledTwice; | ||
expect(fakeCp.fork).to.be.calledWithExactly('a-restarted-module.js'); | ||
}); | ||
it('does not sends a restarted event', function() { | ||
expect(workerEmit).to.not.be.calledWithExactly('worker restarted', { | ||
id: 0, | ||
toFork: 'a-restarted-module.js', | ||
restarts: { manual: 0, automatic: 1 } | ||
it('sends a restarted event', function() { | ||
expect(workerEmit).to.be.calledWithMatch('worker restarted', { | ||
restarts: { manual: 0, automatic: 1 } | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
41950
1.64%1146
2.05%