Skip to content

Commit 8e822ba

Browse files
vernak2539gergelyke
authored andcommitted
feat(signal): expose an option to change the signal the process listens on for shutdown
1 parent 4b731fa commit 8e822ba

File tree

9 files changed

+84
-16
lines changed

9 files changed

+84
-16
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ terminus(server, {
3333

3434
// cleanup options
3535
timeout: 1000, // [optional = 5000] number of milliseconds before forcefull exiting
36-
onSigterm, // [optional] cleanup function, returning a promise
36+
signal, // [optional = 'SIGTERM'] what signal to listen for relative to shutdown
37+
onSignal, // [optional] cleanup function, returning a promise (used to be onSigterm)
3738
onShutdown, // [optional] called right before exiting
3839

3940
// both
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
const http = require('http');
3+
const server = http.createServer((req, res) => res.end('hello'));
4+
5+
const terminus = require('../terminus');
6+
const SIGNAL = 'SIGINT';
7+
8+
terminus(server, {
9+
signal: SIGNAL,
10+
onSignal: () => {
11+
console.log('on-sigint-runs');
12+
return Promise.resolve();
13+
},
14+
onShutdown: () => {
15+
console.log('on-shutdown-runs');
16+
}
17+
});
18+
19+
server.listen(8000, () => {
20+
process.kill(process.pid, SIGNAL);
21+
});

lib/standalone-tests/terminus.onshutdown.js renamed to lib/standalone-tests/terminus.onshutdown.sigterm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const server = http.createServer((req, res) => res.end('hello'));
55
const terminus = require('../terminus');
66

77
terminus(server, {
8-
onSigterm: () => {
8+
onSignal: () => {
99
console.log('on-sigterm-runs');
1010
return Promise.resolve();
1111
},
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
const http = require('http');
3+
const server = http.createServer((req, res) => res.end('hello'));
4+
5+
const terminus = require('../terminus');
6+
const SIGNAL = 'SIGINT';
7+
8+
terminus(server, {
9+
signal: SIGNAL,
10+
onSignal: () => {
11+
console.log('on-sigint-runs');
12+
return Promise.resolve();
13+
}
14+
});
15+
16+
server.listen(8000, () => {
17+
process.kill(process.pid, SIGNAL);
18+
});

lib/standalone-tests/terminus.onsigterm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const server = http.createServer((req, res) => res.end('hello'));
55
const terminus = require('../terminus');
66

77
terminus(server, {
8-
onSigterm: () => {
8+
onSignal: () => {
99
console.log('on-sigterm-runs');
1010
return Promise.resolve();
1111
}

lib/terminus.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,35 @@ function decorateWithHealthCheck(server, options) {
4040
});
4141
}
4242

43-
function decorateWithSigtermHandler(server, options) {
44-
const { onSigterm, onShutdown, timeout, logger } = options;
43+
function decorateWithSignalHandler(server, options) {
44+
const { signal, onSignal, onShutdown, timeout, logger } = options;
4545

4646
stoppable(server, timeout);
4747

4848
const asyncServerStop = promisify(server.stop).bind(server);
4949

5050
function cleanup() {
5151
asyncServerStop()
52-
.then(() => onSigterm())
52+
.then(() => onSignal())
5353
.then(() => onShutdown())
5454
.then(() => {
55-
process.removeListener('SIGTERM', cleanup);
56-
process.kill(process.pid, 'SIGTERM');
55+
process.removeListener(signal, cleanup);
56+
process.kill(process.pid, signal);
5757
})
5858
.catch((error) => {
5959
logger('error happened during shutdown', error);
6060
process.exit(1);
6161
});
6262
}
63-
process.on('SIGTERM', cleanup);
63+
process.on(signal, cleanup);
6464
}
6565

6666
function terminus(server, options = {}) {
6767
const healthChecks = options.healthChecks || {};
6868

69+
const signal = options.signal || 'SIGTERM';
6970
const timeout = options.timeout || 1000;
70-
const onSigterm = options.onSigterm || noopResolves;
71+
const onSignal = options.onSignal || options.onSigterm || noopResolves;
7172
const onShutdown = options.onShutdown || noopResolves;
7273

7374
const logger = options.logger || noop;
@@ -77,8 +78,9 @@ function terminus(server, options = {}) {
7778
logger
7879
});
7980

80-
decorateWithSigtermHandler(server, {
81-
onSigterm,
81+
decorateWithSignalHandler(server, {
82+
signal,
83+
onSignal,
8284
onShutdown,
8385
timeout,
8486
logger

lib/terminus.spec.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,34 @@ describe('Terminus', () => {
9595

9696
it('runs onShutdown after onSigterm', () => {
9797
try {
98-
execFileSync('node', ['lib/standalone-tests/terminus.onshutdown.js']);
98+
execFileSync('node', ['lib/standalone-tests/terminus.onshutdown.sigterm.js']);
9999
} catch (ex) {
100100
expect(ex.stdout.toString().trim()).to.eql('on-sigterm-runs\non-shutdown-runs');
101101
return;
102102
}
103103

104104
throw new Error('running the test should throw, as the exitcode is not 0');
105105
});
106+
107+
it('runs onSigint when getting SIGINT signal', () => {
108+
try {
109+
execFileSync('node', ['lib/standalone-tests/terminus.onsigint.js']);
110+
} catch (ex) {
111+
expect(ex.stdout.toString().trim()).to.eql('on-sigint-runs');
112+
return;
113+
}
114+
115+
throw new Error('running the test should throw, as the exitcode is not 0');
116+
});
117+
118+
it('runs onShutdown after onSigint', () => {
119+
try {
120+
execFileSync('node', ['lib/standalone-tests/terminus.onshutdown.sigint.js']);
121+
} catch (ex) {
122+
expect(ex.stdout.toString().trim()).to.eql('on-sigint-runs\non-shutdown-runs');
123+
return;
124+
}
125+
126+
throw new Error('running the test should throw, as the exitcode is not 0');
127+
});
106128
});

typings/index.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ declare module "@godaddy/terminus" {
99
interface TerminusOptions {
1010
healthChecks?: HealthCheckMap;
1111
timeout?: number;
12-
onSigterm?: () => Promise<any>;
12+
signal?: string;
13+
onSignal?: () => Promise<any>;
1314
onShutdown?: () => Promise<any>;
1415
logger?: (msg: string, err: Error) => void;
16+
17+
/** Deprecated. */
18+
onSigterm?: () => Promise<any>;
1519
}
1620

1721
type Terminus = <T>(server: T, options?: TerminusOptions) => T;

typings/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as http from "http";
22
import * as terminus from "@godaddy/terminus";
33

4-
async function onSigterm () {
4+
async function onSignal () {
55
console.log('server is starting cleanup');
66
return Promise.all([
77
// your clean logic, like closing database connections
@@ -22,7 +22,7 @@ terminus(server, {
2222
"/healthcheck": () => Promise.resolve()
2323
},
2424
timeout: 1000,
25-
onSigterm,
25+
onSignal,
2626
onShutdown,
2727
logger: console.log
2828
});

0 commit comments

Comments
 (0)