Skip to content

Commit

Permalink
send disconnect message
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed May 29, 2014
1 parent 80b1170 commit 01de616
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -2,7 +2,7 @@ TESTS = test/*.test.js
REPORTER = spec
TIMEOUT = 5000
MOCHA_OPTS =
NPM_INSTALL = npm install --registry=http://registry.cnpmjs.org --cache=${HOME}/.npm/.cache/cnpm --disturl=http://dist.u.qiniudn.com
NPM_INSTALL = npm install --registry=http://registry.npm.taobao.org --cache=${HOME}/.npm/.cache/cnpm --disturl=http://dist.u.qiniudn.com
install:
@$(NPM_INSTALL)

Expand Down
2 changes: 1 addition & 1 deletion example/connect_with_cluster/app.js
Expand Up @@ -16,7 +16,7 @@ var connect = require('connect');
var app = connect(
function (req, res, next) {
req.on('end', function () {
if (req.url === '/asycerror') {
if (req.url === '/asyncerror') {
setTimeout(function () {
foo.bar();
}, 10);
Expand Down
21 changes: 18 additions & 3 deletions example/connect_with_cluster/dispatch.js
Expand Up @@ -33,13 +33,28 @@ cluster.setupMaster({
cluster.fork();
cluster.fork();

// when worker disconnect, fork a new one
cluster.on('disconnect', function (worker) {
var w = cluster.fork();
console.error('[%s] [master:%s] wroker:%s disconnect! new worker:%s fork',
var w = cluster.fork();
console.error('[%s] [master:%s] wroker:%s disconnect! new worker:%s fork',
new Date(), process.pid, worker.process.pid, w.process.pid);
});

// if you do not want every disconnect fork a new worker.
// you can listen worker's message.
// graceful will send `graceful:disconnect` message when disconnect.

// cluster.on('fork', function(worker) {
// worker.on('message', function (msg) {
// if (msg === 'graceful:disconnect') {
// var w = cluster.fork();
// console.error('[%s] [master:%s] wroker:%s disconnect! new worker:%s fork',
// new Date(), process.pid, worker.process.pid, w.process.pid);
// }
// });
// });

cluster.on('exit', function (worker) {
console.error('[%s] [master:%s] wroker:%s exit!',
console.error('[%s] [master:%s] wroker:%s exit!',
new Date(), process.pid, worker.process.pid);
});
15 changes: 8 additions & 7 deletions lib/graceful.js
Expand Up @@ -15,10 +15,10 @@ var cluster = require('cluster');

/**
* graceful, please use with `cluster` in production env.
*
*
* @param {Object} options
* - {HttpServer} server, we need to close it and stop taking new requests.
* - {Function(err, throwErrorCount)} [error], when uncaughtException emit, error(err, count).
* - {Function(err, throwErrorCount)} [error], when uncaughtException emit, error(err, count).
* You can log error here.
* - {Number} [killTimeout], worker suicide timeout, default is 30 seconds.
* - {Object} [worker], worker contains `disconnect()`.
Expand Down Expand Up @@ -80,31 +80,32 @@ module.exports = function graceful(options) {
if (worker) {
try {
// stop taking new requests.
// because server could already closed, need try catch the error: `Error: Not running`
// because server could already closed, need try catch the error: `Error: Not running`
for (var i = 0; i < servers.length; i++) {
var server = servers[i];
server.close();
}
console.warn('[%s] [worker:%s] close %d servers!',
console.warn('[%s] [worker:%s] close %d servers!',
new Date(), process.pid, servers.length);
} catch (er1) {
// Usually, this error throw cause by the active connections after the first domain error,
// oh well, not much we can do at this point.
console.error('[%s] [worker:%s] Error on server close!\n%s',
console.error('[%s] [worker:%s] Error on server close!\n%s',
new Date(), process.pid, er1.stack);
}

try {
// Let the master know we're dead. This will trigger a
// 'disconnect' in the cluster master, and then it will fork
// a new worker.
worker.send('graceful:disconnect');
worker.disconnect();
console.warn('[%s] [worker:%s] worker disconnect!',
console.warn('[%s] [worker:%s] worker disconnect!',
new Date(), process.pid);
} catch (er2) {
// Usually, this error throw cause by the active connections after the first domain error,
// oh well, not much we can do at this point.
console.error('[%s] [worker:%s] Error on worker disconnect!\n%s',
console.error('[%s] [worker:%s] Error on worker disconnect!\n%s',
new Date(), process.pid, er2.stack);
}
}
Expand Down

0 comments on commit 01de616

Please sign in to comment.