-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
337 lines (280 loc) · 9.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
var childProcess = require("child_process");
var async = require("async");
var chalk = require("chalk");
var cy = chalk.cyan;
var r = chalk.red;
var gr = chalk.green;
function debug(message) {
var d = new Date();
console.log("DEBUG "
+ ("" + d.getHours()).padStart(2, "0")
+ ":" + ("" + d.getMinutes()).padStart(2, "0")
+ ":" + ("" + d.getSeconds()).padStart(2, "0")
+ "." + ("" + d.getMilliseconds()).padStart(3, "0")
+ " " +message);
}
function shallowCopy(obj) {
var copy = {};
for (var key in obj) {
copy[key] = obj[key];
}
return copy;
}
function fillLast(arr, n) {
for (var i = arr.length; i < n; i++) {
arr.push(arr[arr.length - 1]);
}
return arr;
}
function normalizeOptions(options) {
var defaultRetryInterval = 500;
var defaultRetryCount = 3;
var defaultTimeout = 2000;
options = shallowCopy(options);
if (typeof(options.signal) == "string") {
options.signal = [options.signal];
}
if (options.retryInterval) { // + retryCount + timeout
if (Array.isArray(options.retryInterval)) {
options.retryCount = Math.max(options.retryCount || 0, options.retryInterval.length);
} else {
options.retryInterval = [options.retryInterval];
if (!options.hasOwnProperty("retryCount")) {
options.timeout = options.timeout || defaultTimeout;
options.retryCount = Math.floor((options.timeout - 1) / options.retryInterval[0]);
}
}
options.retryInterval = fillLast(options.retryInterval, options.retryCount);
var retryIntervalsSum = options.retryInterval.reduce(function(a, b) { return a + b; }, 0);
if (!options.timeout || options.timeout <= retryIntervalsSum) {
var lastInterval = options.retryInterval[options.retryInterval.length - 1];
options.timeout = retryIntervalsSum + lastInterval;
}
} else if (options.retryCount != undefined) { // + timeout
options.timeout = options.timeout || defaultTimeout;
if (options.retryCount == 0) {
options.retryInterval = [];
} else {
options.retryInterval = Math.floor(options.timeout / (options.retryCount + 1));
options.retryInterval = fillLast([options.retryInterval], options.retryCount);
}
} else if (options.timeout) {
if (options.timeout <= defaultRetryInterval) {
options.retryInterval = [];
options.retryCount = 0;
} else {
options.retryInterval = [defaultRetryInterval];
options.retryCount = Math.floor((options.timeout - 1) / options.retryInterval[0]);
}
} else {
options.timeout = defaultTimeout;
options.retryInterval = [defaultRetryInterval];
options.retryCount = defaultRetryCount;
}
options.checkInterval = options.checkInterval || 50;
options.signal = fillLast(options.signal || ["SIGINT"], options.retryCount + 1);
options.usePGID = options.usePGID || true;
options.killChildrenImmediately = options.killChildrenImmediately || false;
return options;
}
function getProcessesList(callback) {
var cmd = "ps -A -o ppid,pgid,pid";
var fields = ["ppid", "pgid", "pid"];
if (process.platform == "win32") {
cmd = "wmic PROCESS GET ParentProcessId,ProcessId";
fields = ["ppid", "pid"];
}
childProcess.exec(cmd, { encoding: "utf8" }, function (err, output) {
if (err) { return callback(err); }
var ps = output.split("\n").slice(1).filter(Boolean).map(function (line) {
var entry = {};
line.split(/\s+/).filter(Boolean).forEach(function (field, i) {
if (+field == field) {
field = +field;
}
entry[fields[i]] = field;
})
return entry;
});
callback(null, ps);
});
}
function getProcessChildren(parentPID, options, callback) {
var usePGID = options.usePGID;
if (usePGID && process.platform == "win32") {
if (options.debug) {
debug("Can't use PGID on Windows " + r(".usePGID = false") + " for getting children");
}
usePGID = false;
}
var execStart = Date.now();
getProcessesList(function (err, ps) {
if (err) { return callback(err); }
if (options.debug) {
debug("getProcessesList exec() took " + cy((Date.now() - execStart) + "ms"));
}
var parentEntryIndex = ps.findIndex(function (entry) { return entry.pid == parentPID; });
if (parentEntryIndex != -1) {
var parentEntry = ps[parentEntryIndex];
ps.splice(parentEntryIndex, 1);
}
if (usePGID && (!parentEntry || parentEntry.pgid != parentPID)) {
if (options.debug && usePGID) {
if (!parentEntry) {
debug("Parent " + cy("pid=" + parentPID) + " is dead " + r(".usePGID = false") + " for getting children");
}
if (parentEntry && parentEntry.pgid != parentPID) {
debug("Parent " + cy("pid=" + parentPID) + " pid !== pgid " + r(".usePGID = false") + " for getting children");
}
}
usePGID = false;
}
var children = {};
ps.sort(function (a, b) { return a.pid - b.pid; }).forEach(function (entry) {
if (entry.ppid == parentPID || children[entry.ppid]) {
children[entry.pid] = entry;
}
if (usePGID && entry.pgid == parentPID) {
children[entry.pid] = entry;
}
});
callback(null, Object.keys(children).map(Number));
});
}
function checkDead(pid, callback) {
getProcessesList(function (err, ps) {
if (err) { return callback(err); }
var isDead = !ps.find(function (entry) { return entry.pid == pid;});
callback(null, isDead);
});
}
function sendSignal(pid, signal) {
try {
process.kill(pid, signal);
} catch (err) {}
}
function tryKillParent(pid, timeoutDate, options, callback) {
var tryIndex = 0;
setTimeout(function retry() {
if (timeoutDate <= Date.now()) { return; }
var index = tryIndex;
var retryStart = Date.now();
var signal = options.signal[index];
if (options.debug && index > 0) {
debug("Retry to kill " + cy("pid=" + pid));
}
if (options.debug) {
debug("Send " + cy("signal=" + signal) + " to " + cy("pid=" + pid));
}
sendSignal(pid, signal);
tryIndex += 1;
setTimeout(function check() {
if (timeoutDate <= Date.now()) { return; }
var checkStart = Date.now();
if (options.debug) { debug("Check " + cy("pid=" + pid)); }
checkDead(pid, function (err, isDead) {
if (options.debug) {
debug("checkDead exec() took " + cy((Date.now() - checkStart) + "ms"));
debug(cy("pid=" + pid) + " " + (isDead ? cy("is dead") : r("is alive")));
}
if (err) { return callback(err); }
if (timeoutDate <= Date.now()) { return; }
if (isDead) {
if (options.debug) {
debug("Killed " + gr("pid=" + pid));
}
return callback();
}
var nextCheck = Math.max(Date.now(), checkStart + options.checkInterval);
if (index < options.retryCount) {
var nextRetry = retryStart + options.retryInterval[index];
if (nextCheck < nextRetry) {
setTimeout(check, nextCheck - Date.now());
} else {
setTimeout(retry, Math.max(0, nextRetry - Date.now()));
}
} else {
if (nextCheck < timeoutDate) {
setTimeout(check, nextCheck - Date.now());
}
}
});
}, Math.min(options.checkInterval, options.retryInterval));
}, 0);
}
function kill(pid, options, callback) {
if (arguments.length == 2) {
callback = arguments[1];
options = {}
}
options = normalizeOptions(options);
callback = callback || function () {};
if (options.debug) {
debug("kill(" + cy(pid) + ", " + JSON.stringify(options)) + ")";
}
var timeoutDate = Date.now() + options.timeout;
/*
.usePGID = true, parent is detached pid == pgid
Parents children got by PGID may contain sub-children
After parent is killed getting children of children may result in duplicate PIDs
Filter by pidsScheduled to prevent trying to kill the same process twice.
*/
var pidsScheduled = [];
function tryKillParentWithChildren(pid, callback) {
pidsScheduled.push(pid);
if (options.debug) {
debug("Get children for " + cy("pid=" + pid));
}
getProcessChildren(pid, options, function (err, children) {
if (err) { return callback(err); }
children = children.filter(function (c) { return pidsScheduled.indexOf(c) == -1; });
if (options.debug) {
debug("Try to kill parent " + cy("pid=" + pid) + (children.length ? " with children " + cy(children.join(", ")) : ""));
}
if (options.killChildrenImmediately) {
async.parallel([
tryKillParent.bind(null, pid, timeoutDate, options),
tryKillChildren.bind(null, children)
], callback);
} else {
tryKillParent(pid, timeoutDate, options, function (err) {
if (err) { return callback(err); }
tryKillChildren(children, callback);
});
}
});
}
function tryKillChildren(children, callback) {
if (options.debug) {
var checkStart = Date.now();
debug("Try to kill children of " + cy("pid=" + pid));
}
async.each(children, function (pid, callback) {
if (options.debug) { debug("Check " + cy("pid=" + pid)); }
checkDead(pid, function (err, isDead) {
if (err) { return callback(err); }
if (options.debug) {
debug("checkDead exec() took " + cy((Date.now() - checkStart) + "ms"));
debug(cy("pid=" + pid) + " " + (isDead ? cy("is dead") : r("is alive")));
}
if (isDead) {
callback();
} else {
tryKillParentWithChildren(pid, callback);
}
});
}, callback);
}
var timeoutTimeout = setTimeout(function () {
if (options.debug) {
debug(r("Timedout") + " killing " + cy("pid=" + pid));
}
callback(new Error("Timeout. Can't kill process with pid = " + pid));
}, options.timeout);
tryKillParentWithChildren(pid, function () {
clearTimeout(timeoutTimeout);
callback.apply(null, arguments);
});
}
kill._normalizeOptions = normalizeOptions;
module.exports = kill;