Skip to content

ivolucien/express-graceful-exit

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

express-graceful-exit

Please use the original repo

I'm now the maintainer of that repo and this one is out of date.

See https://github.com/emostar/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 developed for Frafty, a Daily Fantasy Sports site.

Installation

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

Usage

The following two components must be setup for this to work as planned.

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

Graceful Exit Handler

This function will cleanup the server and get it ready for shutting down. 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.

Process Handler

The following options are available:

  • log: Print status messages and errors to the logger (default false).
  • logger: Function that accepts a string to output a log message (default console.log).
  • suicideTimeout: How long to wait before giving up on graceful shutdown, then returns exit code of 1 (default 2m 10s).
  • socketio: An instance of socket.io, used to close all open connections after timeout (default none)
  • exitProcess: If true, the module calls process.exit() when express has shutdown, gracefully or not (default true).
  • 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 (default none)
  • force: Instructs the module to forcibly close sockets once the suicide timeout elapses. Requires that gracefulExit.init(server) be called when initializing the HTTP server (default: 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. Once all connected clients are disconnected, the server exits with an error code of 0
  5. If there are still some remaining connections after the suicideTimeout, it stops waiting and returns exit code of 1

Getting zero downtime deploys

This module does not give you zero downtime deploys automatically, but provides a server that is capable of exiting gracefully, which can then be used by a module like naught to provide zero downtime deploys.

Author: Jon Keating

Maintainer: Ivo Havener

About

(out of date fork) Gracefully exit Express in order to get zero downtime deploys

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%