Package detail

express-graceful-exit

mathrawka34.5kMIT0.5.2

Allow graceful exits for express apps, supporting zero downtime deploys

express, graceful, exit, shutdown

readme

express-graceful-exit

Gracefully decline new requests while shutting down your application. A component that helps support zero downtime deploys for Node.js with Express.

The project was originally developed for Express v3.X, but is used in production with Express v4.X. Please write up an issue or submit a PR if you find bugs using express-graceful-exit with Express v4.X and higher.

Installation

$ cd /path/to/your/project
$ npm install express-graceful-exit

Compatibility

v0.X.X versions are backwards API compatible, with these minor behavior changes:

  1. Process exit is called in a setTimeout block from v0.2.0 forward, so the timing is slightly different between v0.1.0 to v0.2.x+.
  2. After exit was triggered, incoming requests were mismanaged prior to v0.5.0.
    As of v0.5.0 incoming requests are dropped cleanly by default, with new options such as responding with a custom error and/or performing one last request per connection.

Usage

The following two components must both be used to enable clean server shutdown, where incoming requests are gracefully declined.

There are multiple exit options for how in-flight requests are handled, ranging from forced exist after a specified deadline to waiting indefinitely for processing to complete.

middleware

This middleware should be the very first middleware that gets setup with your Express app.

var express = require('express');
var app = express();
var gracefulExit = require('express-graceful-exit');

var server = app.listen(port)

gracefulExit.init(server) // use init() if configured to exit the process after timeout
app.use(gracefulExit.middleware(app));

gracefulExitHandler

This function tells express to accept no new requests and gracefully closes the http server. It can be attached to a signal, or used as a normal function call if another tool is used (such as naught).

// Example for naught
process.on('message', function(message) {
  if (message === 'shutdown') {
    gracefulExit.gracefulExitHandler(app, server, {
        <see options below>
    });
  }
});

Options

Middleware

There are no options available currently.

Exit Handler

The following options are available:

Option Description Default
log Print status messages and errors to the logger false
logger Function that accepts a string to output a log message console.log
callback Optional function that is called with the exit status code once express has shutdown, gracefully or not
Use in conjunction with exitProcess: false when the caller handles process shutdown
no-op
performLastRequest Process the first request received per connection after exit starts, and include a connection close header in the response for the caller and/or load balancer.
The current default is false, but will default to true in the next major release, false is deprecated as of v0.5.0
false, true is recommended
errorDuringExit When requests are refused during graceful exit, respond with an error instead of silently dropping them.
The current default is false, but will default to true in the next major release, false is deprecated as of v0.5.0
false, true is recommended
getRejectionError Function returning rejection error for incoming requests during graceful exit function () { return new Error('Server unavailable, no new requests accepted during shutdown') }
exitProcess If true, the module calls process.exit() when express has shutdown, gracefully or not true
exitDelay Wait timer duration in the final internal callback (triggered either by gracefulExitHandler or the hard exit handler) if exitProcess: true 10ms
suicideTimeout How long to wait before giving up on graceful shutdown, then returns exit code of 1 2m 10s (130s)
socketio An instance of socket.io, used to close all open connections after timeout none
force Instructs the module to forcibly close sockets once the suicide timeout elapses.
For this option to work you must call gracefulExit.init(server) when initializing the HTTP server
false

Details

To gracefully exit this module does the following things:

  1. Closes the http server so no new connections are accepted
  2. Sets connection close header for Keep-Alive connections, if configured for responses
    If errorDuringExit is true, HTTP status code 502 is returned by default, so nginx, ELB, etc will resend to an active server
    If errorDuringExit and/or performLastRequest are set to true, a response is sent with a Connection: close header
  3. If a socket.io instance is passed in the options, all connected clients are immediately disconnected (socket.io v0.X through v1.4.x support)
    The client should have code to reconnect on disconnect
  4. Once the server fully disconnects or the hard exit timer runs
    1. If all in-flight requests have resolved and/or disconnected, the exit handler returns 0
    2. OR if any connections remain after suicideTimeout ms, the handler returns 1
  5. In either case, if exitProcess is set to true the hard exit handler waits exitDelay ms and calls process.exit(x), this allows the logger time to flush and the app's callback to complete, if any

Zero Downtime Deploys

This module does not give you zero downtime deploys on its own. It enables the http server to exit gracefully, which when used with a module like naught can provide zero downtime deploys.

Author: Jon Keating

This module was originally developed for Frafty (formerly www.frafty.com), a Daily Fantasy Sports site.

Maintainer: Ivo Havener

changelog

0.5.2 / 2021-03-29

  • Fix registry entry

0.5.1 / 2021-03-29

Thank you @techmunk and @kevynb for the typescript work

  • Typescript definitions
  • Security patch addressing vulnerability with update to underscore version

0.5.0 / 2019-10-21

Changes reflected in the below release candidate versions

0.5.0-rc.2 / 2019-10-18

Thank you hhunt for additional testing and the fix PR

  • Fix errors in new option to handle a last request, including a crasher

0.5.0-rc.1 / 2019-10-15

Thank you hhunt for finding this bug, as well as for the fix PR and test code

Issue #14 fixes, and configuration options for an improved graceful exit:

  • Fix side effects from handling of rejected incoming requests
    • Connections are no longer closed prematurely during request processing
    • Rejected requests during graceful exit end cleanly
  • Return connection close header with response(s), if any
  • Add option to perform one last request per connection
  • Add option to respond with default or custom http error for rejected requests

0.4.2 / 2018-09-30

  • Fix undefined socket array error
  • Use intended exit code upon forced exit after timeout

0.4.1 / 2018-01-15

  • Names for anonymous functions, for better stack traces

0.4.0 / 2017-03-19

  • Support disconnect for more socket.io versions

0.3.2 / 2016-08/05

  • Released version to npm
  • This version entry, npm keeping me honest

0.3.1 / 2016-08/05

  • Doc format fixes

0.3.0 / 2016-08/05

  • Released version to npm
  • Configurable delay for timer that calls process exit
  • Hard exit function now obeys exitProcess option
  • Doc updates, options in table format

0.2.1 / 2016-07-27

  • Released version to npm
  • Updated package metadata, version string
  • Code style overhaul, many semicolons

0.2.0 / 2016-07-26

Thanks to shaharke for the majority of these changes.

  • Delay process exit to allow any streams to flush, etc.
  • Option to force close sockets on timeout
  • Minor doc and logging improvements

    Issue #1 feature request and fixes:

  • Exit handler callback when done or on timeout
  • Option for exit handler to not exit process itself
  • Clear hard exit timeout on successful server close
  • Avoid duplicate callback invocation

0.1.0 / 2013-03-28

  • Released version to npm
  • Don't keep track of Keep-Alive connections
  • Switch to not catching the exit message on our own

0.0.2 / 2013-03-26

  • Typo fix in README

0.0.1 / 2013-03-26

  • Initial Release