nodejs-graceful-shutdown
TypeScript icon, indicating that this package has built-in type declarations

1.0.7 • Public • Published

nodejs-graceful-shutdown

Gracefully shuts down node.js http server.

NPM Version NPM Downloads Git Issues Closed Issues MIT license

  • Simple to use
  • Configurable to your needs
  • Add your own cleanup function
  • The project referred to "https://github.com/sebhildebrandt/http-graceful-shutdown" and tried to improve it further.
  • If you use pm2 in a container environment, consider pm2-runtime

Quick Start

Installation

$ npm install nodejs-graceful-shutdown

Basic Usage

var gracefulShutdown = require('nodejs-graceful-shutdown');
...
// app: can be http, https, express, koa
server = app.listen(...);
...
 
// this enables the graceful shutdown
gracefulShutdown(server);

Advanced Options

You can pass an options-object to specify your specific options for the graceful shutdown

The following example uses all possible options (using more or less the default settings):

const gracefulShutdown = require('nodejs-graceful-shutdown');
...
// app: can be http, https, express, koa
server = app.listen(...);
...
 
// your personal cleanup function
// - must return a promise
// - the input parameter is optional (only needed if you want to
//   access the signal type inside this function)
// - this function here in this example takes one second to complete
function cleanup(signal) {
  return new Promise((resolve) => {
    console.log('... called signal: ', signal);
   console.log('... in cleanup')
   setTimeout(function() {
   console.log('... cleanup finished');
   resolve();
   }, 1000)
  });
}
 
// this enables the graceful shutdown with advanced options
gracefulShutdown(server,
    {
        signals: 'SIGINT SIGTERM',
        timeout: 30000,
        development: false,
        onShutdown: cleanup,
        finally: function() {
            console.log('Server gracefulls shutted down.....')
        }
    }
);

Trigger shutdown manually

You can now trigger gracefulShutdown programatically (e.g. for tests) like so:

let shutdown
beforeAll(() => {
  shutdown = gracefulShutdown(...)
})
 
afterAll(async () => {
  await shutdown()
})

Option Reference

option default Comments
signals 'SIGINT SIGTERM' define the signals, that should be handled (separated by SPACE)
timeout 30000 timeout till forced shutdown (in milli seconds)
development false if set to true, no graceful shutdown is proceeded to speed up dev-process
onShutdown - place your (not time consuming) callback function, that will
handle your additional cleanup things. Needs to return a promise.

If you add an input parameter to your cleanup function (optional),
the signal type that caused the shutdown is passed to your
cleanup function - example.
finally - here you can place a small (not time consuming) function, that will
be handled at the end of the shutdown (not in dev-mode)

Debug

If you want to get debug notes (debug is a dependency of this module), just set the DEBUG environment variable to enable debugging:

export DEBUG=nodejs-graceful-shutdown

OR on Windows:

set DEBUG=nodejs-graceful-shutdown

Version history

Version Date Comment
1.0.0 2019-09-21 initial release

Package Sidebar

Install

npm i nodejs-graceful-shutdown

Weekly Downloads

2

Version

1.0.7

License

MIT

Unpacked Size

13.4 kB

Total Files

5

Last publish

Collaborators

  • silvernine