Wednesday, 26 April 2017

Asynchronous tasks when node server.close() invoked

I'm working on an node+express application and want to prepare it to make a graceful. The goal is to make deployments in the "right" way, that is:

  • Avoid accepting requests
  • Finish the current ones
  • Terminate node process
  • Put new code
  • Restart server-

For the graceful shutdown I'm using a code similar to:

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.
     //
     // How to ensure no tasks is running in the thread pool ???
   });

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

});

My doubt is to know if inside server.close() can I be sure no task is running or pending execution ?

What happens if a job in the event loop started reading a big file asynchronously and is working in the thread pool? Does server.close warranties no tasks are running in the thread pool too?



via EricSonaron

No comments:

Post a Comment