Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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

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

glynnbird opened this issue Oct 13, 2012 · 6 comments
Labels

Comments

@glynnbird
Copy link

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.

@langpavel
Copy link
Contributor

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.

@aheckmann
Copy link
Contributor

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 [email protected] 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.

@tj
Copy link
Member

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 tj closed this as completed Oct 15, 2012
@da99
Copy link

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

});

@binarykitchen
Copy link

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

@aymericbeaumet
Copy link

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 :('); });
});

@expressjs expressjs 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.
Labels
Projects
None yet
Development

No branches or pull requests

8 participants