Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to improve treekill #3554

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 7 additions & 75 deletions lib/TreeKill.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,14 @@
'use strict';

// From https://raw.githubusercontent.com/pkrumins/node-tree-kill/master/index.js

var childProcess = require('child_process');
var spawn = childProcess.spawn;
var exec = childProcess.exec;
var pidtree = require('pidtree');

module.exports = function (pid, signal, callback) {
var tree = {};
var pidsToProcess = {};
tree[pid] = [];
pidsToProcess[pid] = 1;
pidtree(pid, {root: true}, function(err, pids) {
if (err) {
console.error('Error while reading process tree', err)
}

switch (process.platform) {
case 'win32':
exec('taskkill /pid ' + pid + ' /T /F', { windowsHide: true }, callback);
break;
case 'darwin':
buildProcessTree(pid, tree, pidsToProcess, function (parentPid) {
return spawn('pgrep', ['-P', parentPid]);
}, function () {
killAll(tree, signal, callback);
});
break;
// case 'sunos':
// buildProcessTreeSunOS(pid, tree, pidsToProcess, function () {
// killAll(tree, signal, callback);
// });
// break;
default: // Linux
buildProcessTree(pid, tree, pidsToProcess, function (parentPid) {
return spawn('ps', ['-o', 'pid', '--no-headers', '--ppid', parentPid]);
}, function () {
killAll(tree, signal, callback);
});
break;
}
killAll(pids, signal, callback)
})
};

function killAll (tree, signal, callback) {
Expand Down Expand Up @@ -74,44 +47,3 @@ function killPid(pid, signal) {
console.error(err);
}
}

function buildProcessTree (parentPid, tree, pidsToProcess, spawnChildProcessesList, cb) {
var ps = spawnChildProcessesList(parentPid);
var allData = '';

ps.on('error', function(err) {
console.error(err);
});

if (ps.stdout) {
ps.stdout.on('data', function (data) {
data = data.toString('ascii');
allData += data;
});
}

var onClose = function (code) {
delete pidsToProcess[parentPid];

if (code !== 0) {
// no more parent processes
if (Object.keys(pidsToProcess).length == 0) {
cb();
}
return;
}
var pids = allData.match(/\d+/g) || [];
if (pids.length === 0)
return cb();

pids.forEach(function (pid) {
pid = parseInt(pid, 10);
tree[parentPid].push(pid);
tree[pid] = [];
pidsToProcess[pid] = 1;
buildProcessTree(pid, tree, pidsToProcess, spawnChildProcessesList, cb);
});
};

ps.on('close', onClose);
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
"moment": "^2.19",
"needle": "^2.2.0",
"nssocket": "0.6.0",
"pidtree": "^0.2.0",
"pidusage": "^2.0.5",
"pm2-axon": "3.1.0",
"pm2-axon-rpc": "0.5.0",
Expand Down