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

Adapting NodeJs recommended process shutdown? #16

Open
canebat opened this issue May 5, 2015 · 0 comments
Open

Adapting NodeJs recommended process shutdown? #16

canebat opened this issue May 5, 2015 · 0 comments

Comments

@canebat
Copy link

canebat commented May 5, 2015

The official Node docs say we should shutdown the process after receiving an error with domains:

  var server = require('http').createServer(function(req, res) {
    var d = domain.create();
    d.on('error', function(er) {
      console.error('error', er.stack);

      // Note: we're in dangerous territory!
      // By definition, something unexpected occurred,
      // which we probably didn't want.
      // Anything can happen now!  Be very careful!

      try {
        // make sure we close down within 30 seconds
        var killtimer = setTimeout(function() {
          process.exit(1);
        }, 30000);
        // But don't keep the process open just for that!
        killtimer.unref();

        // stop taking new requests.
        server.close();

        // Let the master know we're dead.  This will trigger a
        // 'disconnect' in the cluster master, and then it will fork
        // a new worker.
        cluster.worker.disconnect();

        // try to send an error to the request that triggered the problem
        res.statusCode = 500;
        res.setHeader('content-type', 'text/plain');
        res.end('Oops, there was a problem!\n');
      } catch (er2) {
        // oh well, not much we can do at this point.
        console.error('Error sending 500!', er2.stack);
      }
    });

    // Because req and res were created before this domain existed,
    // we need to explicitly add them.
    // See the explanation of implicit vs explicit binding below.
    d.add(req);
    d.add(res);

    // Now run the handler function in the domain.
    d.run(function() {
      handleRequest(req, res);
    });
  });
  server.listen(PORT);
}

How can this be adapted to connect-domain? I have some simple skeleton code I started with, but it is missing some critical pieces that I don't understand:

var express = require('express');
var app = express();
var connectDomain = require('connect-domain');

app.use(express.static('/public'));
app.use(connectDomain());

app.get('/', function(req, res) {
  res.send('Hello World!');
});

// This is where I would handle process shutdown
app.use(function(err, req, res, next) {
  try {
    // Adapting nodejs process shutdown here
    var killtimer = setTimeout(function() {
      process.exit(1);
    }, 30000);
    // But don't keep the process open just for that!
    killtimer.unref();


    // ??? How to close server ???
    // server.close();


    // try to send an error to the request that triggered the problem
    res.statusCode = 500;
    res.setHeader('content-type', 'text/plain');
    res.end('Oops, there was a problem!\n');

  } catch (er2) {
    console.log('Error sending 500!, er2.stack);
  }
});

// ??? How would the following be adapted ???
/*----------------
d.add(req);
d.add(res);

// Now run the handler function in the domain.
d.run(function() {
  handleRequest(req, res);
});
---------------*/

// Start application
app.listen(3000, function() {
  console.log("Express app started!");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant