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

Comments

Projects
None yet
8 participants
@glynnbird

glynnbird commented Oct 13, 2012

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

This comment has been minimized.

Show comment
Hide comment
@langpavel

langpavel Oct 13, 2012

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.

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.

@aheckmann

This comment has been minimized.

Show comment
Hide comment
@aheckmann

aheckmann Oct 13, 2012

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 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.

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.

@tj

This comment has been minimized.

Show comment
Hide comment
@tj

tj Oct 15, 2012

Member

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

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 Oct 15, 2012

@da99

This comment has been minimized.

Show comment
Hide comment
@da99

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

});

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

This comment has been minimized.

Show comment
Hide comment
@binarykitchen

binarykitchen Mar 16, 2013

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

binarykitchen commented Mar 16, 2013

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

@aymericbeaumet

This comment has been minimized.

Show comment
Hide comment
@aymericbeaumet

aymericbeaumet Jan 5, 2015

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

aymericbeaumet commented Jan 5, 2015

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 dougwilson added the question label Jan 5, 2015

@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.