Skip to content

Commit

Permalink
[fix] give sigkills after a timeout given by options.killTTL in MS
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeck committed Nov 16, 2011
1 parent 8c8d670 commit 5f20181
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 10 deletions.
8 changes: 8 additions & 0 deletions examples/signal-ignore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function noop() {
console.log('IGNORED!')
}
process.on('SIGTERM',noop);
process.on('SIGINT',noop);
setInterval(function(){
console.log('heartbeat');
}, 100);
53 changes: 44 additions & 9 deletions lib/forever/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var Monitor = exports.Monitor = function (script, options) {
this.uid = options.uid || forever.randomString(24);
this.pidFile = options.pidFile || path.join(forever.config.get('pidPath'), this.uid + '.pid');
this.max = options.max;
this.killTTL = options.killTTL;
this.childExists = false;
this.times = 0;

Expand Down Expand Up @@ -358,9 +359,10 @@ Monitor.prototype.stop = function () {
// Kills the ChildProcess object associated with this instance.
//
Monitor.prototype.kill = function (forceStop) {
var self = this;
var self = this,
child = this.child;

if (!this.child || !this.running) {
if (!child || !this.running) {
process.nextTick(function () {
self.emit('error', new Error('Cannot stop process that is not running.'));
});
Expand All @@ -372,25 +374,58 @@ Monitor.prototype.kill = function (forceStop) {
// fires in `Monitor.prototype.start` we can short circuit
// and prevent auto-restart
//
var toKill = [this.child.pid];
if (forceStop) {
this.forceStop = true;
//
// If we have a time before we truly kill forcefully, set up a timer
//
if (this.killTTL) {
var timer = setTimeout(function () {
toKill.forEach(function(pid) {
try {
process.kill(pid, 'SIGKILL');
}
catch (e) {
//conditions for races may exist, this is most likely an ESRCH
//these should be ignored, and then we should emit that it is dead
}
});
self.emit('stop', this.childData);
}, this.killTTL);
child.on('exit', function () {
clearTimeout(timer);
});
}
}


child.on('exit', function() {
self.emit('stop', this.childData);
});

function killProcesses() {
toKill.forEach(function (pid) {
try {
process.kill(pid, 'SIGTERM');
}
catch (e) {
//conditions for races may exist, this is most likely an ESRCH
//these should be ignored, and then we should emit that it is dead
}
});
}

if (this.killTree) {
psTree(this.child.pid, function (err, children) {
var pids = children.map(function (p) {
return p.PID;
});

pids.unshift(self.child.pid);
spawn('kill', ['-9'].concat(pids)).on('exit', function () {
self.emit('stop', self.childData);
});
killProcesses();
});
}
else {
this.child.kill();
this.emit('stop', this.childData);
killProcesses();
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/multiple-processes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ vows.describe('forever/multiple-processes').addBatch({
},
"should spawn both processes appropriately": function (err, data) {
assert.isNull(err);
assert.lengthOf(data.monitors, 2);
assert.equal(data.monitors.length, 2);
this.child1.stop();
this.child2.stop();
}
Expand Down
50 changes: 50 additions & 0 deletions test/signal-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* signal-test.js: Tests for spin restarts in forever.
*
* (C) 2010 Nodejitsu Inc.
* MIT LICENCE
*
*/

var assert = require('assert'),
path = require('path'),
vows = require('vows'),
forever = require('../lib/forever');

vows.describe('forever/signal').addBatch({
"When using forever": {
"and spawning a script that ignores signals SIGINT and SIGTERM": {
"with killTTL defined": {
topic: function () {
var script = path.join(__dirname, '..', 'examples', 'signal-ignore.js'),
child = new (forever.Monitor)(script, { silent: true, killTTL: 1000 }),
callback = this.callback,
timer;

timer = setTimeout(function () {
callback(new Error('Child did not die when killed by forever'), child);
}, 3000);

child.on('exit', function () {
callback.apply(null, [null].concat([].slice.call(arguments)));
clearTimeout(timer);
});

child.on('start', function () {
//
// Give it time to set up signal handlers
//
setTimeout(function() {
child.stop();
}, 1000);
});

child.start();
},
"should forcibly kill the processes": function (err, child, spinning) {
assert.isNull(err);
}
}
}
}
}).export(module);

0 comments on commit 5f20181

Please sign in to comment.