Module to handle applications’ graceful shutdowns.
This super simple module helps shutting down servers, database handles, etc. in NodeJS applications. It allows to register handlers for certain shutdown signals/events in order to attempt a graceful shutdown (clean-ups etc.)
NOTE: it removes any previous registered listeners for the given signals!
By default it listens to: SIGTERM
SIGHUP
SIGINT
It also listens to process.exit
but keep in mind that exit
does not allow asynchrounous listeners’ operations to complete (see process.exit on NodeJS.org)
It is also possible to add (or remove) other shutdown signals/events.
npm i -S @hypercliq/shutdown-cleanup
import { ShutdownCleanup } from 'shutdown-cleanup'
// const ShutdownCleanup = require('shutdown-cleanup').ShutdownCleanup
ShutdownCleanup.registerHandler(() => console.log('This is printed on exit :)'))
ShutdownCleanup.addSignal('uncaughtException')
ShutdownCleanup.removeSignal('SIGHUP')
ShutdownCleanup.listSignals()
TypeScript types are included.
It is possible to listen to the uncaughtException
event, but no error message will be displayed if the handle function does not explicitly ask for it or we don’t enable debug
(this is also true for other events such as unhandledRejection
.)
ShutdownCleanup.registerHandler((codeOrError) =>
console.log('This what we got back:', codeOrError)
)
By accepting a parameter (in this case codeOrError
) we can get back from the module either a code/signal or an error.
Another way to see what’s going on is to turn debug
on:
DEBUG=shutdown-cleanup npm start
or Windows
set DEBUG=shutdown-cleanup & npm start
Author: hypercliq
Source Code: https://github.com/hypercliq/shutdown-cleanup
#nodejs #node #javascript