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 10, 2019
1 parent 09820c7 commit d53ab37
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 31 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
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,18 +228,18 @@ function setKillTimeout(kill, signal, options, killResult) {
setTimeout(() => kill('SIGKILL'), 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}) {
if (Number.isInteger(forceKillAfter)) {
return forceKillAfter;
function getForceKillAfterTimeout({forceKillAfterTimeout}) {
if (Number.isInteger(forceKillAfterTimeout)) {
return forceKillAfterTimeout;
}

return DEFAULT_FORCE_KILL_TIMEOUT;
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
5 changes: 2 additions & 3 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,7 +155,7 @@ if (process.platform !== 'win32') {
await pEvent(subprocess, 'message');

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

const {signal} = await t.throwsAsync(subprocess);
Expand Down

0 comments on commit d53ab37

Please sign in to comment.