Module to handle graceful shutdowns

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.

Install

npm i -S @hypercliq/shutdown-cleanup

Usage

Register a handler

import { ShutdownCleanup } from 'shutdown-cleanup'
// const ShutdownCleanup = require('shutdown-cleanup').ShutdownCleanup

ShutdownCleanup.registerHandler(() => console.log('This is printed on exit :)'))

Add a signal or event to listen to

ShutdownCleanup.addSignal('uncaughtException')

Remove a signal or an event

ShutdownCleanup.removeSignal('SIGHUP')

List signals and events listened to

ShutdownCleanup.listSignals()

TypeScript

TypeScript types are included.

Uncaught Exceptions & other similar events

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.)

Handle parameter
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.

Debug

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

Download Details:

Author: hypercliq

Source Code: https://github.com/hypercliq/shutdown-cleanup

#nodejs #node #javascript

Module to handle graceful shutdowns
9.75 GEEK