Innovenergy_trunk/frontend/node_modules/testcafe/lib/utils/process.js

132 lines
16 KiB
JavaScript
Raw Permalink Normal View History

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.killBrowserProcess = void 0;
const child_process_1 = require("child_process");
const os_family_1 = __importDefault(require("os-family"));
const promisify_event_1 = __importDefault(require("promisify-event"));
const delay_1 = __importDefault(require("../utils/delay"));
const CHECK_KILLED_DELAY = 2000;
const HARD_KILL_FLAG = 'SIGKILL';
const NEW_LINE_SEPERATOR_RE = /(\r\n)|(\n\r)|\n|\r/g;
const cannotGetListOfProcessError = 'Cannot get list of processes';
const killProcessTimeoutError = 'Kill process timeout';
function getProcessOutputUnix() {
const error = new Error(cannotGetListOfProcessError);
return new Promise((resolve, reject) => {
const child = (0, child_process_1.spawn)('ps', ['-eo', 'pid,command']);
let stdout = '';
let stderr = '';
child.stdout.on('data', data => {
stdout += data.toString();
});
child.stderr.on('data', data => {
stderr += data.toString();
});
child.on('exit', () => {
if (stderr)
reject(error);
else
resolve(stdout);
});
child.on('error', () => {
reject(error);
});
});
}
function findProcessIdUnix(browserId, psOutput) {
const processIdRegex = new RegExp('^\\s*(\\d+)\\s+.*' + browserId);
const lines = psOutput.split(NEW_LINE_SEPERATOR_RE);
for (let i = 0; i < lines.length; i++) {
const match = processIdRegex.exec(lines[i]);
if (match)
return parseInt(match[1], 10);
}
return null;
}
function isUnixProcessExist(processId, psOutput) {
const processIdRegex = new RegExp('^\\s*' + processId + '\\s+.*');
const lines = psOutput.split(NEW_LINE_SEPERATOR_RE);
return lines.some(line => processIdRegex.test(line));
}
async function findProcessUnix(browserId) {
const output = await getProcessOutputUnix();
return findProcessIdUnix(browserId, output);
}
async function isUnixProcessKilled(processId) {
const output = await getProcessOutputUnix();
return !isUnixProcessExist(processId, output);
}
async function killUnixProcessSoft(processId) {
process.kill(processId);
}
async function killUnixProcessHard(processId) {
process.kill(processId, HARD_KILL_FLAG);
}
async function killProcessUnix(processId) {
const maxSoftTries = 2;
let softTries = 0;
let unixProcessKilled = false;
do {
await killUnixProcessSoft(processId);
softTries++;
await (0, delay_1.default)(CHECK_KILLED_DELAY);
unixProcessKilled = await isUnixProcessKilled(processId);
} while (!unixProcessKilled && softTries < maxSoftTries);
unixProcessKilled = await isUnixProcessKilled(processId);
if (unixProcessKilled)
return;
await killUnixProcessHard(processId);
await (0, delay_1.default)(CHECK_KILLED_DELAY);
unixProcessKilled = await isUnixProcessKilled(processId);
if (unixProcessKilled)
return;
// NOTE: if 2 soft-kill and 1 hard-kill with "SIGKILL"-flag didn't work - throw error
throw new Error(killProcessTimeoutError);
}
async function runWMIC(args) {
const wmicProcess = (0, child_process_1.spawn)('wmic.exe', args, { detached: true });
let wmicOutput = '';
wmicProcess.stdout.on('data', data => {
wmicOutput += data.toString();
});
try {
await Promise.race([
(0, promisify_event_1.default)(wmicProcess.stdout, 'end'),
(0, promisify_event_1.default)(wmicProcess, 'error'),
]);
return wmicOutput;
}
catch (e) {
return '';
}
}
async function findProcessWin(browserId) {
const wmicArgs = ['process', 'where', `commandline like '%${browserId}%' and name <> 'cmd.exe' and name <> 'wmic.exe'`, 'get', 'processid'];
const wmicOutput = await runWMIC(wmicArgs);
let processList = wmicOutput.split(/\s*\n/);
processList = processList
// NOTE: remove list's header and empty last element, caused by trailing newline
.slice(1, -1)
.map(pid => ({ pid: Number(pid) }));
return processList[0] ? processList[0].pid : null;
}
async function killBrowserProcess(browserId) {
const processId = os_family_1.default.win ? await findProcessWin(browserId) : await findProcessUnix(browserId);
if (!processId)
return true;
try {
if (os_family_1.default.win)
process.kill(processId);
else
await killProcessUnix(processId);
return true;
}
catch (e) {
return false;
}
}
exports.killBrowserProcess = killBrowserProcess;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHJvY2Vzcy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbI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