Skip to content

Commit

Permalink
Merge forceKill and forceKillAfter options
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jun 11, 2019
1 parent 1a0031d commit 54486b3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 36 deletions.
11 changes: 3 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,18 +281,13 @@ declare namespace execa {

interface KillOptions {
/**
If the first signal does not terminate the child process after a specified timeout, a `SIGKILL` signal will be sent to the process.
How many milliseconds to wait for the child process to terminate before sending `SIGKILL`.
@default true
*/
forceKill?: boolean;

/**
Milliseconds to wait for the child process to terminate before sending a `SIGKILL` signal.
Can be disabled with `false`.
@default 5000
*/
forceKillAfter?: number;
forceKillAfterTimeout?: boolean | number;
}

interface ExecaChildPromise<StdoutErrorType> {
Expand Down
16 changes: 10 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,25 @@ function setKillTimeout(kill, signal, options, killResult) {
}, timeout).unref();
}

function shouldForceKill(signal, {forceKill}, killResult) {
return isSigterm(signal) && forceKill !== false && killResult;
function shouldForceKill(signal, {forceKillAfterTimeout}, killResult) {
return isSigterm(signal) && forceKillAfterTimeout !== false && killResult;
}

function isSigterm(signal) {
return signal === os.constants.signals.SIGTERM ||
(typeof signal === 'string' && signal.toUpperCase() === 'SIGTERM');
}

function getForceKillAfterTimeout({forceKillAfter = DEFAULT_FORCE_KILL_TIMEOUT}) {
if (!Number.isInteger(forceKillAfter) || forceKillAfter < 0) {
throw new TypeError(`Expected the \`forceKillAfter\` option to be a non-negative integer, got \`${forceKillAfter}\` (${typeof forceKillAfter})`);
function getForceKillAfterTimeout({forceKillAfterTimeout = true}) {
if (forceKillAfterTimeout === true) {
return DEFAULT_FORCE_KILL_TIMEOUT;
}

return forceKillAfter;
if (!Number.isInteger(forceKillAfterTimeout) || forceKillAfterTimeout < 0) {
throw new TypeError(`Expected the \`forceKillAfterTimeout\` option to be a non-negative integer, got \`${forceKillAfterTimeout}\` (${typeof forceKillAfterTimeout})`);
}

return forceKillAfterTimeout;
}

const execa = (file, args, options) => {
Expand Down
8 changes: 4 additions & 4 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ execa('unicorns').kill();
execa('unicorns').kill('SIGKILL');
execa('unicorns').kill(undefined);
execa('unicorns').kill('SIGKILL', {});
execa('unicorns').kill('SIGKILL', {forceKill: true});
execa('unicorns').kill('SIGKILL', {forceKill: false});
execa('unicorns').kill('SIGKILL', {forceKillAfter: 42});
execa('unicorns').kill('SIGKILL', {forceKillAfter: undefined});
execa('unicorns').kill('SIGKILL', {forceKillAfterTimeout: true});
execa('unicorns').kill('SIGKILL', {forceKillAfterTimeout: false});
execa('unicorns').kill('SIGKILL', {forceKillAfterTimeout: 42});
execa('unicorns').kill('SIGKILL', {forceKillAfterTimeout: undefined});

expectType<ExecaChildProcess<string>>(execa('unicorns'));
expectType<ExecaReturnValue<string>>(await execa('unicorns'));
Expand Down
17 changes: 6 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ try {
const subprocess = execa('node');
setTimeout(() => {
subprocess.kill('SIGTERM', {
forceKillAfter: 2000
forceKillAfterTimeout: 2000
});
}, 1000);
```
Expand All @@ -140,19 +140,14 @@ Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#c

Same as the original [`child_process#kill()`](https://nodejs.org/api/child_process.html#child_process_subprocess_kill_signal) except: if `signal` is `SIGTERM` (the default value) and the child process is not terminated after 5 seconds, force it by sending `SIGKILL`.

##### options.forceKill
##### options.forceKillAfterTimeout

Type: `boolean`<br>
Default: `true`

If the first signal does not terminate the child process after a specified timeout, a `SIGKILL` signal will be sent to the process.

##### options.forceKillAfter

Type: `string`<br>
Type: `boolean | number`<br>
Default: `5000`

Milliseconds to wait for the child process to terminate before sending `SIGKILL`.
How many milliseconds to wait for the child process to terminate before sending `SIGKILL`.

Can be disabled with `false`.

#### cancel()

Expand Down
26 changes: 19 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ if (process.platform !== 'win32') {
await pEvent(subprocess, 'message');

subprocess.kill('SIGTERM', {
forceKill: false,
forceKillAfter: 50
forceKillAfterTimeout: false
});

t.true(isRunning(subprocess.pid));
Expand All @@ -156,13 +155,26 @@ if (process.platform !== 'win32') {
await pEvent(subprocess, 'message');

subprocess.kill('SIGTERM', {
forceKillAfter: 50
forceKillAfterTimeout: 50
});

const {signal} = await t.throwsAsync(subprocess);
t.is(signal, 'SIGKILL');
});

test('.kill() `forceKillAfterTimeout` can be `true`', async t => {
const subprocess = execa('node', ['fixtures/no-killable'], {
stdio: ['ipc']
});

await pEvent(subprocess, 'message');

subprocess.kill('SIGTERM', {forceKillAfterTimeout: true});

const {signal} = await t.throwsAsync(subprocess);
t.is(signal, 'SIGKILL');
});

test('execa() with .kill() after it with nothing (undefined) should kill after 50 ms with SIGKILL', async t => {
const subprocess = execa('node', ['fixtures/no-killable'], {
stdio: ['ipc']
Expand All @@ -176,15 +188,15 @@ if (process.platform !== 'win32') {
t.is(signal, 'SIGKILL');
});

test('.kill() `forceKillAfter` should not be a float', t => {
test('.kill() `forceKillAfterTimeout` should not be a float', t => {
t.throws(() => {
execa('noop').kill('SIGTERM', {forceKillAfter: 0.5});
execa('noop').kill('SIGTERM', {forceKillAfterTimeout: 0.5});
}, {instanceOf: TypeError, message: /non-negative integer/});
});

test('.kill() `forceKillAfter` should not be negative', t => {
test('.kill() `forceKillAfterTimeout` should not be negative', t => {
t.throws(() => {
execa('noop').kill('SIGTERM', {forceKillAfter: -1});
execa('noop').kill('SIGTERM', {forceKillAfterTimeout: -1});
}, {instanceOf: TypeError, message: /non-negative integer/});
});
}
Expand Down

0 comments on commit 54486b3

Please sign in to comment.