Skip to content

Commit

Permalink
Merge branch 'features/maintain-worker-count'
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Feb 20, 2011
2 parents f1e0ad1 + 29aaee7 commit e6959f4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/reload.js
Expand Up @@ -8,7 +8,7 @@ var cluster = require('../')

// try loading, and changing "Hello", to "Hello World"

var body = 'Hello'
var body = 'Hello World'
, len = body.length;
var server = http.createServer(function(req, res){
res.writeHead(200, { 'Content-Length': len });
Expand Down
28 changes: 25 additions & 3 deletions lib/master.js
Expand Up @@ -48,7 +48,8 @@ var node = process.execPath
* - 'working directory` Working directory defaulting to the script's dir
* - 'backlog` Connection backlog, defaulting to 128
* - 'socket path` Master socket path defaulting to `./`
* - 'timeout` Worker shutdown timeout in milliseconds, defaulting to 60000
* - 'timeout` Worker shutdown timeout in milliseconds, defaulting to 60,000
* - 'ping interval` Interval at which master pings workers, defaulting to 10,000
* - 'user` User id / name
* - 'group` Group id / name
*
Expand Down Expand Up @@ -95,6 +96,7 @@ var Master = module.exports = function Master(server) {
'backlog': 128
, 'working directory': this.dir
, 'socket path': this.dir
, 'ping interval': 10000
, 'timeout': 60000
};

Expand Down Expand Up @@ -127,6 +129,9 @@ var Master = module.exports = function Master(server) {
sock.setEncoding('ascii');
sock.on('data', self.frame.bind(self));
});

// maintain worker count
this.on('listening', this.maintainWorkerCount.bind(this));
};

/**
Expand Down Expand Up @@ -177,8 +182,7 @@ Master.prototype.__defineGetter__('socketPath', function(){

Master.prototype.__defineGetter__('environmentMatches', function(){
if (this._env)
return this.env == this._env
|| 'all' == this._env;
return this.env == this._env || 'all' == this._env;
return true;
});

Expand Down Expand Up @@ -367,6 +371,24 @@ Master.prototype.start = function(){
});
};

/**
* Maintain worker count, re-spawning if necessary.
*
* @api private
*/

Master.prototype.maintainWorkerCount = function(){
// check workers
this.children.forEach(function(worker){
var pid = worker.proc.pid;
if (!pid) this.spawnWorker(worker.id);
}, this);

// check again
setTimeout(this.maintainWorkerCount.bind(this)
, this.options['ping interval']);
};

/**
* Spawn `n` workers.
*
Expand Down

0 comments on commit e6959f4

Please sign in to comment.