-
Notifications
You must be signed in to change notification settings - Fork 119
/
unhealthy.js
43 lines (41 loc) · 1.5 KB
/
unhealthy.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
'use strict';
const runScript = require('runscript');
const isWin = process.platform === 'win32';
const REGEX = isWin ? /^(.*)\s+(\d+)\s*$/ : /^\s*(\d+)\s+(.*)/;
async function findNodeProcess(filterFn) {
const command = isWin
? 'wmic Path win32_process Where "Name = \'node.exe\'" Get CommandLine,ProcessId'
: // command, cmd are alias of args, not POSIX standard, so we use args
'ps -eo "pid,args"';
const stdio = await runScript(command, { stdio: 'pipe' });
const processList = stdio.stdout
.toString()
.split('\n')
.reduce((arr, line) => {
if (!!line && !line.includes('/bin/sh') && line.includes('node')) {
const m = line.match(REGEX);
/* istanbul ignore else */
if (m) {
const item = isWin ? { pid: m[2], cmd: m[1] } : { pid: m[1], cmd: m[2] };
if (!filterFn || filterFn(item)) {
arr.push(item);
}
}
}
return arr;
}, []);
return processList;
}
(async function() {
const title = process.argv.splice(2)[0].split('--title=')[1];
const processList = await findNodeProcess(item => {
const { cmd } = item;
return cmd.includes('app_worker.js') && cmd.includes(title);
});
console.log(`processList.length: ${processList.length}`);
for (const pro of processList) {
process.kill(pro.pid, 'SIGINT');
}
// 等待nginx移除负载,nginx设置的3秒两次失败移除,额外提供6秒的现有请求处理时间。
await new Promise(resolve => setTimeout(resolve, 1000 * 12));
})();