Express 3 has no "app.close()" #1366

Closed
glynnbird opened this Issue Oct 13, 2012 · 6 comments

Comments

Projects
None yet
8 participants

Imagine you want to be able to terminate your Node.js app and drain all existing connections before quitting, you might come up with a solution as outlined here:

http://blog.argteam.com/coding/hardening-node-js-for-production-part-3-zero-downtime-deployments-with-nginx

e.g.

  • respond to SIGTERM or SIGINT events
  • tell your Express app to stop accepting new connections
  • get called back by Express when the last connection has been serviced

This would all you to do graceful deployments of new code without terminating existing requests (assuming your load balancer sends traffic to other servers).

As of Express 3, the app.close() method seems to have disappeared, which means Express users have no means of gracefully stopping an application as far as I can see.

Contributor

langpavel commented Oct 13, 2012

Whole express is in fact stuff around one function; vice versa express app is requestListener for http.createServer(). That is.
Can you look at mechanism you starting your server? This will hint you what you must do.

Contributor

aheckmann commented Oct 13, 2012

Yes express is really a listener to http request events now which means you can do this:

var app = express();
var server = require('http').createServer(app);
server.listen(1337, function () {
server.close()
})

On Oct 13, 2012, at 1:38 AM, Glynn Bird notifications@github.com wrote:

Imagine you want to be able to terminate your Node.js app and drain all existing connections before quitting, you might come up with a solution as outlined here:

http://blog.argteam.com/coding/hardening-node-js-for-production-part-3-zero-downtime-deployments-with-nginx

e.g.

respond to SIGTERM or SIGINT events
tell your Express app to stop accepting new connections
get called back by Express when the last connection has been serviced
This would all you to do graceful deployments of new code without terminating existing requests (assuming your load balancer sends traffic to other servers).

As of Express 3, the app.close() method seems to have disappeared, which means Express users have no means of gracefully stopping an application as far as I can see.


Reply to this email directly or view it on GitHub.

Owner

tj commented Oct 15, 2012

yup, if you happen to use the "shortcut" app.listen() it now returns an http server wrapped like aaron is showing, var server = app.listen(3000); server.close()

tj closed this Oct 15, 2012

da99 commented Nov 19, 2012

So the final full code example for cleaning up resources in Express 3 would be the following?:

var express = require('express');
var app = express();
var server = app.listen(1337);

process.on( 'SIGTERM', function () {

   server.close(function () {
     console.log( "Closed out remaining connections.");
     // Close db connections, etc.
   });

   setTimeout( function () {
     console.error("Could not close connections in time, forcefully shutting down");
     process.exit(1); 
   }, 30*1000);

});

I doubt setTimeout works here after a SIGTERM event because the main event loop won't accept any new events anymore.

prikha referenced this issue in Kreees/muon Dec 16, 2013

Closed

Muon server could not be closed #6

If anyone ever reaches this issue via Google, the following snippet works with Express 4:

var app = express();

var server = app.listen(8080, function() {
  console.log('Listening :)');
  server.close(function() { console.log('Doh :('); });
});

dougwilson added the question label Jan 5, 2015

dougwilson locked and limited conversation to collaborators Jan 5, 2015

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.