@flerokoo/graceful-shutdown-handler
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-development • Public • Published

Graceful Shutdown Handler

This is an utility package for shutting down gracefully virtually anything: servers, database connections, workers, etc.

GracefulShutdownHandler waits for certain signal from user, uncaught error or unhandler rejection and, instead of exiting right away, it allows all provided callbacks to complete.

Usage example

npm i @flerokoo/graceful-shutdown-handler
import GracefulShutdownHandler from "graceful-shutdown-handler";

const handler = new GracefulShutdownHandler({
  // events that trigger shutdown (default: ['SIGINT', 'SIGTERM', 'uncaughtException', 'unhandledRejection'])
  events: ['SIGINT', 'SIGTERM'], 
  // maximum amount of time that callbacks are allowed to run (default: 30)
  timeout: 15, 
  // exit code to use when timeout happens (default: 1)
  timeoutExitCode: 0, 
  // extra exit delay, may useful for logging (default: 0.1)
  exitDelay: 1
});

handler.addCallback(() => {
  cleanupOrSomething();
  console.log('cleaned up')
})

// async callbacks are supported too
handler.addCallback(async () => {
  console.log("performing async operation...");
  await someAsyncOperation();
  console.log("async operation performed");
}, {
  // When true: handler will wait for this async operation to complete before starting next one (default: false)
  blocking: true,
  // Order of execution (default: 0)
  order: -1 
});

More examples here

Readme

Keywords

Package Sidebar

Install

npm i @flerokoo/graceful-shutdown-handler

Weekly Downloads

1

Version

1.0.0-development

License

MIT

Unpacked Size

34.9 kB

Total Files

23

Last publish

Collaborators

  • flerokoo