1.1.8 • Published 3 years ago

shutdown-hook v1.1.8

Weekly downloads
313
License
MIT
Repository
github
Last release
3 years ago

Shutdown-Hook Build Status

Shutdown-Hook is an injectable shutdown hook (well dah..) module for Node.js applications.

Installation

npm install shutdown-hook

Usage

Create a new instance:

const ShutdownHook = require('shutdown-hook')
let shutdownHook = new ShutdownHook(options)

Constructor Options:

PropertyDescriptionDefault Value
timeoutSets a timeout in ms for the shutdown operation to complete. If the shutdown operations exceed the timeout, the process will exit with code 110000ms
lifoReverses the execution order of the shutdown functionsfalse

Add shutdown functions:

shutdownHook.add(_ => doSomething(), options)

Shutdown functions are executed in the order they were added unless:

  1. options.order was specified.
  2. lifo: true was specified when instantiating the hook.

Shutdown function can return nothing, a value, a Promise, or throw an error. A rejected promise or error will stop the shutdown sequence (subsequent functions will not be run) and exit the process with code 1.

You can also name shutdown functions:

shutdownHook.add('database', _ => doSomething(), {name: "foo"})

This might be useful when listening to events (see below). If no name was given, the library auto-generates a name for consistency.

Options:

PropertyDescriptionDefault Value
namethe name of the shutdown function. will be used when emitting events
orderthe order of the function is the shutdown sequence. functions are ordered in ascending order before execution0

Register to termination signals:

shutdownHook.register()

Registers the shutdown hook to trigger the shutdown sequence when receiving SIGTERM, SIGINT or "shutdown" messages sent through the process event emitter.

Listen to shutdown sequence events:

shutdownHook.on('ShutdownStarted', (e) => log.info('it has began'))
shutdownHook.on('ComponentShutdown', (e) => log.info('shutting down one component'))
shutdownHook.on('ShutdownEnded', (e) => log.info('it has ended'))

Events structure:

EventPropertyTypeDescriptionOptional
ComponentShutdownnameStringName of the shutdown functions that's being executedNo
orderNumberthe order of the shutdown function in the sequenceNo
indexNumberthe index of the shutdown function in the sequenceNo
ShutdownEndedcodeNumberThe exit code the library used when calling process.exit()No
errorErrorThe error the library caught in case the sequence failed to runYes
1.1.8

3 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago