Socket
Socket
Sign inDemoInstall

express-graceful-exit

Package Overview
Dependencies
Maintainers
2
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-graceful-exit

Allow graceful exits for express apps, supporting zero downtime deploys


Version published
Weekly downloads
7.4K
increased by7.28%
Maintainers
2
Weekly downloads
 
Created
Source

express-graceful-exit

A component in having zero downtime deploys for Node.js with Express. It was developed for Express 3.X, so it may need work to be compatible with Express 2.X

This module was originally developed for Frafty, a Daily Fantasy Sports site.

Installation

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

Compatibility

v0.X.X versions are backwards API compatible, with the caveate that 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+.

Usage

The following two components must both be used to enable fully graceful exits.

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');

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, {
        socketio: app.settings.socketio
    });
  }
});

Options

Middleware

There are no options available currently.

Exit Handler

The following options are available:

OptionDescriptionDefault
logPrint status messages and errors to the loggerfalse
loggerFunction that accepts a string to output a log messageconsole.log
callbackOptional 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
exitProcessIf true, the module calls process.exit() when express has shutdown, gracefully or nottrue
exitDelayWait timer duration in the final internal callback (triggered either by gracefulExitHandler or the suicideTimeout) if exitProcess: true10ms
suicideTimeoutHow long to wait before giving up on graceful shutdown, then returns exit code of 12m 10s (130s)
socketioAn instance of socket.io, used to close all open connections after timeoutnone
forceInstructs 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 will do the following things:

  1. Close the http server so no new connections are accepted
  2. Mark that the server will gracefully exit, so if a connection that is using the Keep-Alive header is still active, it will be told to close the connection
    The HTTP status code of 502 is returned, so nginx, ELB, etc will try again with a working server
  3. If a socket.io instance is passed in the options, it enumerates all connected clients and disconnects them
    The client should have code to reconnect on disconnect
  4. Server fully disconnects or the hard exit timer runs
    1. Once all connected clients are disconnected, the exit handler returns 0
    2. OR If there are any remaining connections after suicideTimeout ms, the handler returns 1
  5. In either case, if exitProcess is set to true the exit handler waits exitDelay ms and calls process.exit

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
Maintainer: Ivo Havener

Keywords

FAQs

Package last updated on 05 Aug 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc