node-graceful
node-graceful is a small helper module without dependencies that aims to ease graceful exit
of complex node programs including async waiting on multiple independent modules.
Installation:
npm i -S node-graceful
yarn add node-graceful
Had any problem? open an issue
Quick example
const Graceful = require('node-graceful');
Graceful.captureExceptions = true;
Graceful.on('exit', async () => {
console.log(`Received ${signal} - Exiting gracefully`);
await webServer.close();
});
Graceful.on('exit', (signal) => {
return new Promise((resolve) => {
console.log("Another independent listener!");
setTimeout(() => resolve(), 1000);
});
});
Typescript
import Graceful from 'node-graceful';
Graceful.captureExceptions = true;
Graceful.on('exit', async () => {
await server.close();
});
Quick Docs
interface Graceful {
on(signal: 'exit', listener: GracefulListener): GracefulSubscription;
off(signal: 'exit', listener: GracefulListener): void;
clear(): void;
exit(): void;
exit(exitCode: number): void;
exit(exitSignal: string): void;
exit(exitCode: number, exitSignal: string): void;
exitOnDouble: boolean;
timeout: number;
captureExceptions: boolean;
captureRejections: boolean;
}
type GracefulListener = (signal: string, details?: object) => (void | any | Promise<any> | Promise<Error>);
type GracefulSubscription = () => void;
Read bellow for full API reference.
API Reference
Graceful.on('exit', {Function} listener)
Add exit listener to be called when process exit is triggered.
Graceful
listens on all terminating signals and triggers exit
accordingly.
Terminating events: SIGTERM
SIGINT
SIGBREAK
SIGHUP
Options
listener(signal, details?)
- listener function
signal
- the signal that triggered the exit. example: 'SIGTERM'details
- optional details provided by the trigger. for example in case of unhandledException
this will be an error object. on external signal it will be undefined.
Examples
The listener function can return a promise that will delay the process exit until it's fulfilment.
Graceful.on('exit', () => Promise.resolve('I Am A Promise!'));
Graceful.on('exit', async () => {
await webServer.close();
return Promise.all([
controller.close(),
dbClient.close()
]);
});
if old style callback is needed, wrap the logic with a promise
const server = require('http').createServer(function (req, res) {
res.write('ok');
res.end()
})
Graceful.on('exit', () => {
return new Promise((resolve, reject) => {
server.close((err) => {
if (err) return reject(err);
resolve();
});
});
});
Return value
the method returns a function that when invoked, removes the listener subscription.
the function is a shorthand for .off
method
example
const removeListener = Graceful.on('exit', () => {});
removeListener();
Graceful.off('exit', {Function} listener)
Remove a previously subscribed listener.
example
const gracefulExit = () => {
console.log("exiting!");
};
let removeListener = Graceful.on('SIGTERM', gracefulExit);
Graceful.off('SIGTERM', gracefulExit);
Graceful.clear()
Unsubscribe all exit
listeners.
example
Graceful.on('exit', () => {
console.log("Received some exit signal!");
return Promise.resolve("A promise to be waited on before dying");
});
Graceful.on('exit', (done) => {
console.log("Another listener");
done();
});
Graceful.clear();
Graceful.exit({Number} [code], {String} [signal])
Trigger graceful process exit.
This method is meant to be a substitute command for process.exit()
to allow other modules to exit gracefully in case of error.
code
- (optional) exit code to be used. default - process.exitCode
signal
- (optional) signal to be simulating for listeners. default - SIGTERM
example
server.listen(3333)
.on('listening', function () {
console.log('Yay!')
})
.on('error', function (err) {
if (err.code === 'EADDRINUSE') {
console.error("Damn, Port is already in use...");
Graceful.exit();
}
});
Options
Options are global and shared, any change will override previous values.
Graceful.exitOnDouble = true {boolean}
Whether to exit immediately when a second deadly event is received,
For example when Ctrl-C is pressed twice etc..
When exiting due to double event, exit code will be process.exitCode
or 1
(necessarily a non-zero)
Graceful.timeout = 30000 {number}
Maximum time to wait for exit listeners in ms
.
After exceeding the time, the process will force exit
and the exit code will be process.exitCode
or 1
(necessarily a non-zero)
Setting the timeout to 0
will disable timeout functionality (will wait indefinitely)
Graceful.captureExceptions = false {boolean}
Whether to treat uncaughtException
event as a terminating event and trigger graceful shutdown.
Graceful.captureExceptions = true;
throw new Error('DANG!');
Graceful.captureExceptions = false {boolean}
Whether to treat unhandledRejection
event as a terminating event and trigger graceful shutdown.
On newer node
versions unhandledRejection
is in-fact a terminating event
Graceful.captureRejections = true;
Promise.reject(new Error('DANG!'));
exitCode
Graceful
will obey process.exitCode
property value when exiting unless the exit is forced (double signal, timeout) in which case the exit code must be non-zero.