148 lines
3.8 KiB
JavaScript
148 lines
3.8 KiB
JavaScript
'use strict';
|
|
|
|
const { execFileSync } = require('node:child_process');
|
|
|
|
const isWin = process.platform === 'win32';
|
|
const appRoot = process.cwd();
|
|
|
|
function sleep(ms) {
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
}
|
|
|
|
function listNodeProcesses() {
|
|
if (isWin) {
|
|
try {
|
|
const output = execFileSync('wmic', [ 'Path', 'win32_process', 'Where', "Name = 'node.exe'", 'Get', 'CommandLine,ProcessId' ], {
|
|
encoding: 'utf8',
|
|
stdio: [ 'ignore', 'pipe', 'pipe' ],
|
|
});
|
|
|
|
return output
|
|
.split(/\r?\n/)
|
|
.map(line => line.trim())
|
|
.filter(Boolean)
|
|
.map(line => {
|
|
const match = line.match(/^(.*)\s+(\d+)$/);
|
|
if (!match) {
|
|
return null;
|
|
}
|
|
return {
|
|
pid: Number(match[2]),
|
|
cmd: match[1],
|
|
};
|
|
})
|
|
.filter(Boolean);
|
|
} catch (error) {
|
|
if (error.code !== 'ENOENT') {
|
|
throw error;
|
|
}
|
|
|
|
const output = execFileSync('powershell', [
|
|
'-NoProfile',
|
|
'-Command',
|
|
"Get-CimInstance Win32_Process -Filter \"Name='node.exe'\" | Select-Object ProcessId, CommandLine | ConvertTo-Json -Compress",
|
|
], {
|
|
encoding: 'utf8',
|
|
stdio: [ 'ignore', 'pipe', 'pipe' ],
|
|
}).trim();
|
|
|
|
if (!output) {
|
|
return [];
|
|
}
|
|
|
|
const parsed = JSON.parse(output);
|
|
const items = Array.isArray(parsed) ? parsed : [ parsed ];
|
|
return items
|
|
.map(item => ({
|
|
pid: Number(item.ProcessId),
|
|
cmd: String(item.CommandLine || ''),
|
|
}))
|
|
.filter(item => item.pid && item.cmd);
|
|
}
|
|
}
|
|
|
|
const output = execFileSync('ps', [ '-eo', 'pid=,args=' ], {
|
|
encoding: 'utf8',
|
|
stdio: [ 'ignore', 'pipe', 'pipe' ],
|
|
});
|
|
|
|
return output
|
|
.split(/\r?\n/)
|
|
.map(line => line.trim())
|
|
.filter(Boolean)
|
|
.map(line => {
|
|
const match = line.match(/^(\d+)\s+(.*)$/);
|
|
if (!match) {
|
|
return null;
|
|
}
|
|
return {
|
|
pid: Number(match[1]),
|
|
cmd: match[2],
|
|
};
|
|
})
|
|
.filter(Boolean);
|
|
}
|
|
|
|
function isMasterProcess(cmd) {
|
|
return cmd.includes('start-cluster');
|
|
}
|
|
|
|
function isWorkerProcess(cmd) {
|
|
return cmd.includes('egg-cluster/lib/app_worker.js')
|
|
|| cmd.includes('egg-cluster/lib/agent_worker.js')
|
|
|| cmd.includes('egg-cluster\\lib\\app_worker.js')
|
|
|| cmd.includes('egg-cluster\\lib\\agent_worker.js');
|
|
}
|
|
|
|
function matchesCurrentApp(processInfo) {
|
|
if (!processInfo || !processInfo.cmd || !processInfo.pid) {
|
|
return false;
|
|
}
|
|
|
|
if (processInfo.pid === process.pid) {
|
|
return false;
|
|
}
|
|
|
|
return processInfo.cmd.includes(appRoot) && (isMasterProcess(processInfo.cmd) || isWorkerProcess(processInfo.cmd));
|
|
}
|
|
|
|
function killPids(pids, signal) {
|
|
for (const pid of pids) {
|
|
try {
|
|
process.kill(pid, signal);
|
|
} catch (error) {
|
|
if (error.code !== 'ESRCH') {
|
|
throw error;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
async function main() {
|
|
const processList = listNodeProcesses().filter(matchesCurrentApp);
|
|
const masterPids = processList.filter(item => isMasterProcess(item.cmd)).map(item => item.pid);
|
|
const workerPids = processList.filter(item => isWorkerProcess(item.cmd)).map(item => item.pid);
|
|
|
|
if (masterPids.length > 0) {
|
|
console.log(`stopping current egg masters: ${masterPids.join(', ')}`);
|
|
killPids(masterPids, 'SIGTERM');
|
|
await sleep(5000);
|
|
} else {
|
|
console.log('no current egg master process found');
|
|
}
|
|
|
|
const remainingWorkers = listNodeProcesses()
|
|
.filter(matchesCurrentApp)
|
|
.filter(item => isWorkerProcess(item.cmd))
|
|
.map(item => item.pid);
|
|
|
|
if (remainingWorkers.length > 0) {
|
|
console.log(`killing remaining current egg workers: ${remainingWorkers.join(', ')}`);
|
|
killPids(remainingWorkers, 'SIGKILL');
|
|
}
|
|
}
|
|
|
|
main().catch(error => {
|
|
console.error(error.message || error);
|
|
process.exitCode = 1;
|
|
}); |