Skip to content

Commit

Permalink
always killTree
Browse files Browse the repository at this point in the history
  • Loading branch information
fjakobs authored and indexzero committed Nov 18, 2011
1 parent e4f2b09 commit 7ab97bd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 38 deletions.
35 changes: 26 additions & 9 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
});

Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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
Expand Down
32 changes: 3 additions & 29 deletions lib/forever/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ var util = require('util'),
winston = require('winston'),
watch = require('watch'),
minimatch = require('minimatch'),
psTree = require('ps-tree'),
forever = require('../forever');

//
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7ab97bd

Please sign in to comment.