Browse files

Added custom child stop signal support

  • Loading branch information...
1 parent 89b6f4e commit 26c73b4a35eeac32c99a1288b715eed9d22ccc91 @estliberitas estliberitas committed Dec 9, 2012
Showing with 54 additions and 6 deletions.
  1. +11 −0 examples/graceful-exit.js
  2. +4 −4 lib/forever-monitor/common.js
  3. +2 −1 lib/forever-monitor/monitor.js
  4. +37 −1 test/monitor/signal-test.js
View
11 examples/graceful-exit.js
@@ -0,0 +1,11 @@
+process.on('SIGTERM',function() {
+ console.log('received SIGTERM');
+ setTimeout(function() {
+ console.log('Exiting after some time.');
+ process.exit(0);
+ }, 1000);
+});
+
+setInterval(function(){
+ console.log('Heartbeat');
+}, 100);
View
8 lib/forever-monitor/common.js
@@ -24,7 +24,7 @@ exports.checkProcess = function (pid) {
//
// Trying to kill non-existent process here raises a ESRCH - no such
// process exception. Also, signal 0 doesn't do no harm to a process - it
- // only checks if sending a singal to a given process is possible.
+ // only checks if sending a signal to a given process is possible.
//
process.kill(pid, 0);
return true;
@@ -34,20 +34,20 @@ exports.checkProcess = function (pid) {
}
};
-exports.kill = function(pid, killTree, callback) {
+exports.kill = function(pid, killTree, signal, callback) {
if (killTree) {
psTree(pid, function (err, children) {
var pids = children.map(function (p) {
return p.PID;
});
pids.unshift(pid);
- spawn('kill', ['-9'].concat(pids)).on('exit', callback || function() {});
+ spawn('kill', ['--signal', signal].concat(pids)).on('exit', callback || function() {});
});
}
else {
try {
- process.kill(pid);
+ process.kill(pid, signal);
}
catch (ex) { }
callback && callback();
View
3 lib/forever-monitor/monitor.js
@@ -48,6 +48,7 @@ var Monitor = exports.Monitor = function (script, options) {
this.pidFile = options.pidFile;
this.max = options.max;
this.killTTL = options.killTTL;
+ this.killSignal = options.killSignal || 'SIGKILL';
this.childExists = false;
this.checkFile = options.checkFile !== false;
this.times = 0;
@@ -358,7 +359,7 @@ Monitor.prototype.kill = function (forceStop) {
}
}
- common.kill(this.child.pid, this.killTree, function () {
+ common.kill(this.child.pid, this.killTree, this.killSignal, function () {
self.emit('stop', self.childData);
});
}
View
38 test/monitor/signal-test.js
@@ -47,4 +47,40 @@ vows.describe('forever-monitor/monitor/signal').addBatch({
}
}
}
-}).export(module);
+}).addBatch({
+ "When using forever-monitor": {
+ "and spawning script that gracefully exits on SIGTERM": {
+ "with killSignal defined child": {
+ topic: function () {
+ var script = path.join(__dirname, '..', '..', 'examples', 'graceful-exit.js'),
+ child = new (fmonitor.Monitor)(script, { silent: true, killSignal: 'SIGTERM'}),
+ 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 gracefully shutdown when receives SIGTERM": function (err, child, spinning) {
+ assert.isNull(err);
+ }
+ }
+ }
+ }
+ }).export(module);

0 comments on commit 26c73b4

Please sign in to comment.