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

Refactor kill() #272

Merged
merged 2 commits into from Jun 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 19 additions & 14 deletions index.js
Expand Up @@ -213,6 +213,23 @@ function joinCommand(file, args = []) {
return [file, ...args].join(' ');
}

function spawnedKill(kill, signal = 'SIGTERM', options = {}) {
const killResult = kill(signal);
setKillTimeout(kill, signal, options, killResult);
return killResult;
}

function setKillTimeout(kill, signal, options, killResult) {
if (!shouldForceKill(signal, options, killResult)) {
return;
}

const forceKillAfter = Number.isInteger(options.forceKillAfter) ?
options.forceKillAfter :
5000;
setTimeout(() => kill('SIGKILL'), forceKillAfter).unref();
}

function shouldForceKill(signal, options, killResult) {
return ((typeof signal === 'string' &&
signal.toUpperCase() === 'SIGTERM') ||
Expand All @@ -239,20 +256,8 @@ const execa = (file, args, options) => {
}));
}

const originalKill = spawned.kill.bind(spawned);
spawned.kill = (signal = 'SIGTERM', options = {}) => {
const killResult = originalKill(signal);
if (shouldForceKill(signal, options, killResult)) {
const forceKillAfter = Number.isInteger(options.forceKillAfter) ?
options.forceKillAfter :
5000;
setTimeout(() => {
originalKill('SIGKILL');
}, forceKillAfter).unref();
}

return killResult;
};
const kill = spawned.kill.bind(spawned);
spawned.kill = spawnedKill.bind(null, kill);

// #115
let removeExitHandler;
Expand Down