Home

Awesome

http-graceful-shutdown

  _   _   _                                  __      _        _        _      _
 | |_| |_| |_ _ __ ___ __ _ _ _ __ _ __ ___ / _|_  _| |___ __| |_ _  _| |_ __| |_____ __ ___ _
 | ' \  _|  _| '_ \___/ _` | '_/ _` / _/ -_)  _| || | |___(_-< ' \ || |  _/ _` / _ \ V  V / ' \
 |_||_\__|\__| .__/   \__, |_| \__,_\__\___|_|  \_,_|_|   /__/_||_\_,_|\__\__,_\___/\_/\_/|_||_|
             |_|      |___/

Gracefully shuts down node.js http server. More than 10 Mio downloads overall.

NPM Version NPM Downloads Git Issues Closed Issues deps status Caretaker MIT license

Version 3.0 just released. This version is fully backwards compatible to version 2.x but adds much better handling under the hood. More that 10 Mio downloads.

Features

http-graceful-shutdown manages a secure and save shutdown of your http server application:

Quick Start

Installation

$ npm install http-graceful-shutdown

Basic Usage

const gracefulShutdown = require('http-graceful-shutdown');
...
// app: can be http, https, express, koa, fastity, ...
server = app.listen(...);
...

// this enables the graceful shutdown
gracefulShutdown(server);

Explanation

Functionality

                                                                    PARENT Process (e.g. nodemon, shell, kubernetes, ...)
─────────────────────────┬───────────────────────────────────────────────────────────────────────────────────────────────
                         │ Signal (SIGINT, SIGTERM, ...)
                         │
                         │
           (1)       (2) v                                                 NODE SERVER (HTTP, Express, koa, fastity, ...)
    ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇
           │             │ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ │ <─ shutdown procedure
           │             │ shutdown initiated           │ │                                            │
           │             │                              │ │                                            │
           │             │                              │ │    (8) shutdown function    (9) finally fn │
           │             │ ▄▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄             │ │ ▄▄ ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄▄▄▄ │
           │             └ (3)          (4) close       │ └ (7) destroy                                │
           │               preShutdown  idle sockets    │   remaining sockets                          │
           │                                            │                                              │ (10)
     serve │      serving req. (open connection)        │          (5)                                 └ SERVER terminated
        ▄▄▄│      ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄┤           ^ blocked
        ^  │      ^  last request before                │           │
        │  │      │  receiving shutdown signal          │           │
        │  │      │                                     │           │
        │  │      │                                     │           │
        │  │      │                                     │           │
        │  │      │ Long request                        │           │
Request │  V Resp │                                     V Resp.     │
        │         │                                                 │                                                   CLIENT
────────┴─────────┴─────────────────────────────────────────────────┴─────────────────────────────────────────────────────────
  1. usually, your NODE http server (the black bar in the middle) replies to client requests and sends responses
  2. if your server receives a termination signal (e.g. SIGINT - Ctrl-C) from its parent, http-graceful-shutdown starts the shutdown procedure
  3. first, http-graceful-shutdown will run the "preShutdown" (async) function. Place your own function here (passed to the options object), if you need to have all HTTP sockets available and untouched.
  4. then all empty connections are closed and destroyed and
  5. http-graceful-shutdown will block any new requests
  6. if possible, http-graceful-shutdown communicates to the clients that the server is about to close (connection close header)
  7. http-graceful-shutdown now tries to wait till all sockets are finished, then destroys the all remaining sockets
  8. now it is time to run the "onShutdown" (async) function (if such a function is passed to the options object)
  9. as soon as this onShutdown function has ended, the "finally" (sync) function is executed (if passed to the options)
  10. now the event loop is cleared up OR process.exit() is triggered (can be defined in the options) and the server process ends.

Options

optiondefaultComments
timeout30000timeout till forced shutdown (in milliseconds)
signals'SIGINT SIGTERM'define the signals, that should be handled (separated by SPACE)
developmentfalseif set to true, no graceful shutdown is proceeded to speed up dev-process
preShutdown-not time-consuming callback function. Needs to return a promise.<br>Here, all HTTP sockets are still available and untouched
onShutdown-not time-consuming callback function. Needs to return a promise.
forceExittrueforce process.exit - otherwise just let event loop clear
finally-small, not time-consuming function, that will<br>be handled at the end of the shutdown (not in dev-mode)

Option Explanation

Advanced Options Example

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

The following example uses all possible options:

const gracefulShutdown = require('http-graceful-shutdown');
...
// app: can be http, https, express, koa, fastity, ...
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 shutdownFunction(signal) {
  return new Promise((resolve) => {
    console.log('... called signal: ' + signal);
    console.log('... in cleanup')
    setTimeout(function() {
      console.log('... cleanup finished');
      resolve();
    }, 1000)
  });
}

// finally function
// -- sync function
// -- should be very short (not time consuming)
function finalFunction() {
  console.log('Server gracefulls shutted down.....')
}

// this enables the graceful shutdown with advanced options
gracefulShutdown(server,
  {
    signals: 'SIGINT SIGTERM',
    timeout: 10000,                   // timeout: 10 secs
    development: false,               // not in dev mode
    forceExit: true,                  // triggers process.exit() at the end of shutdown process
    preShutdown: preShutdownFunction, // needed operation before httpConnections are shutted down
    onShutdown: shutdownFunction,     // shutdown function (async) - e.g. for cleanup DB, ...
    finally: finalFunction            // finally function (sync) - e.g. for logging
  }
);

Trigger shutdown manually

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

let shutdown
beforeAll(() => {
  shutdown = gracefulShutdown(...)
})

afterAll(async () => {
  await shutdown()
})

Do not force process.exit()

With the forceExit option, you can define how your node server process ends: when setting forceExit to false, you just let the event loop clear and then the proccess ends automatically:

const gracefulShutdown = require('http-graceful-shutdown');
...
// app: can be http, https, express, koa, fastity, ...
server = app.listen(...);
...

// enable graceful shutdown with options:
// this option lets the event loop clear to end your node server
// no explicit process.exit() will be triggered.

gracefulShutdown(server, {
  forceExit: false
});

If you want an explicit process.exit() at the end, set forceExit to true (which is the default).

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=http-graceful-shutdown

OR on Windows:

set DEBUG=http-graceful-shutdown

Examples

You can find examples how to use http-graceful-shutdown with Express, Koa, http, http2, fastify in the examples directory. To run the examples, be sure to install debug and express, koa or fastify.

npm install debug express koa fastify

Version history

VersionDateComment
3.1.132023-02-11fix forceExit default value
3.1.122022-12-04changed lgtm to github scanning
3.1.112022-11-18updated examples
3.1.102022-11-17forceExit handling adapted
3.1.92022-10-24updated docs, code cleanup
3.1.82022-07-27updated docs, fixed typos
3.1.72022-03-18updated dependencies, updated docs
3.1.62022-02-27updated dependencies
3.1.52021-11-08updated docs
3.1.42021-08-27updated docs
3.1.32021-08-03fixed handle events once (thanks to Igor Basov)
3.1.22021-06-15fixed cleanupHttp() no timeout
3.1.12021-05-13updated docs
3.1.02021-05-08refactoring, added preShutdown
3.0.22021-04-08updated docs
3.0.12021-02-26code cleanup
3.0.02021-02-25version 3.0 release
2.4.02021-02-15added forceExit option (defaults to true)
2.3.22019-06-14typescript typings fix
2.3.12019-05-31updated docs, added typescript typings
2.3.02019-05-30added manual shutdown (for tests) see docs below
2.2.32019-02-01updated docs, debug
2.2.22018-12-28updated docs, keywords
2.2.12018-11-20updated docs
2.2.02018-11-19added (optional) signal type to shutdown function - see example
2.1.32018-11-06updated docs
2.1.22018-11-03updated dependencies (version bump), updated docs
2.1.12018-02-28extended isFunction to support e.g. AsyncFunctions
2.1.02018-02-11bug fixing onShutdown method was called before server.close
2.0.62017-11-06updated docs, code cleanup
2.0.52017-11-06updated dependencies, modifications gitignore, added docs
2.0.42017-09-21updated dependencies, modifications gitignore
2.0.32017-06-18updated dependencies
2.0.22017-05-27fixed return value 0
2.0.12017-04-24modified documentation
2.0.02017-04-24added 'onShutdown' option, renamed 'callback' option to 'finally'
1.0.62016-02-03adding more explicit debug information and documentation
1.0.52016-02-01better handling of closing connections
1.0.42015-10-01small fixes
1.0.32015-09-15updated docs
1.0.12015-09-14updated docs, reformated code
1.0.02015-09-14initial release

Comments

If you have ideas, comments or questions, please do not hesitate to contact me.

Sincerely,

Sebastian Hildebrandt, +innovations

Credits

Written by Sebastian Hildebrandt sebhildebrandt

Contributors

License MIT license

The MIT License (MIT)

Copyright © 2015-2023 Sebastian Hildebrandt, +innovations.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.