From 7ab97bd16b5129ecd8f6f493cfbafee2e4ba1b05 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Tue, 8 Nov 2011 14:33:06 +0100 Subject: [PATCH] always killTree --- lib/forever.js | 35 ++++++++++++++++++++++++++--------- lib/forever/monitor.js | 32 +++----------------------------- 2 files changed, 29 insertions(+), 38 deletions(-) diff --git a/lib/forever.js b/lib/forever.js index 6a368edb..635e2703 100644 --- a/lib/forever.js +++ b/lib/forever.js @@ -19,6 +19,8 @@ var fs = require('fs'), mkdirp = require('mkdirp').mkdirp, portfinder = require('portfinder'), timespan = require('timespan'), + spawn = require('child_process').spawn, + psTree = require('ps-tree'), winston = require('winston'); var forever = exports; @@ -420,10 +422,7 @@ forever.stop = function (target, format) { if (procs && procs.length > 0) { procs.forEach(function (proc) { [proc.foreverPid, proc.pid].forEach(function (pid) { - try { - process.kill(pid); - } - catch (ex) { } + forever.kill(pid, true); }); }); @@ -556,12 +555,9 @@ forever.stopAll = function (format) { }); fPids.concat(cPids).forEach(function (pid) { - try { - if (pid !== process.pid) { - process.kill(pid); - } + if (pid !== process.pid) { + forever.kill(pid, true); } - catch (ex) { } }); process.nextTick(function () { @@ -868,6 +864,27 @@ forever.checkProcess = function (pid) { } }; +forever.kill = function(pid, killTree, 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() {}); + }); + } + else { + try { + process.kill(pid); + } + catch (ex) { } + callback && callback(); + } + +} + // // ### @columns {Object} // Property descriptors for accessing forever column information diff --git a/lib/forever/monitor.js b/lib/forever/monitor.js index 6ddd6b49..18240827 100644 --- a/lib/forever/monitor.js +++ b/lib/forever/monitor.js @@ -14,7 +14,6 @@ var util = require('util'), winston = require('winston'), watch = require('watch'), minimatch = require('minimatch'), - psTree = require('ps-tree'), forever = require('../forever'); // @@ -398,35 +397,10 @@ Monitor.prototype.kill = function (forceStop) { }); } } - - 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; - }); - killProcesses(); - }); - } - else { - killProcesses(); - } + forever.kill(this.child.pid, this.killTree, function() { + self.emit('stop', self.childData); + }); } return this;