Skip to content

Commit

Permalink
change activeWorkers API
Browse files Browse the repository at this point in the history
  • Loading branch information
spion committed Oct 24, 2015
1 parent be803ae commit 2196173
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -110,11 +110,13 @@ Terminates the entire cluster and removes all listeners.

### cluster.activeWorkers()

Returns a list of the workers that are currently serving requests
Returns a hash of all worker slots (0 <= WORKER_ID < N). If a worker isn't
available at that slot, the value in the hash is null or undefined. Otherwise,
the value will be a worker object that is ready to serve requests.

### cluster.workers()

Returns a list of all the workers, including those that are not
Returns an array of all the workers, including those that are not
yet ready or those that will be replaced.

# worker cleanup
Expand Down
15 changes: 10 additions & 5 deletions index.js
Expand Up @@ -41,7 +41,12 @@ module.exports = function(file, opt) {

var workers = [];

var activeWorkers = [];
var activeWorkers = {length: opt.workers};
function deactivate(w) {
if (activeWorkers[w._rc_wid] == w) {
activeWorkers[w._rc_wid] = null;
}
}

function emit() {
channel.emit.apply(self, arguments);
Expand All @@ -67,7 +72,7 @@ module.exports = function(file, opt) {
});
w.process.on('exit', function() {
utils.removeFrom(workers, w);
utils.removeFrom(activeWorkers, w)
deactivate(w);
});
workers.push(w);
return w;
Expand All @@ -78,7 +83,7 @@ module.exports = function(file, opt) {
if (worker._rc_isReplaced) return;
worker._rc_isReplaced = true;

utils.removeFrom(activeWorkers, worker)
deactivate(worker);

var now = Date.now()
var time = backoff(now)
Expand Down Expand Up @@ -133,7 +138,7 @@ module.exports = function(file, opt) {
process.nextTick(trykillfn);
}

utils.removeFrom(activeWorkers, worker)
deactivate(worker);
}


Expand Down Expand Up @@ -171,7 +176,7 @@ module.exports = function(file, opt) {
});
// When a worker becomes ready, add it to the active list
channel.on('ready', function workerReady(w) {
activeWorkers.push(w);
activeWorkers[w._rc_wid] = w;
})

}
Expand Down
13 changes: 5 additions & 8 deletions test/lib/server.js
Expand Up @@ -6,16 +6,13 @@ if (null == process.env.WORKER_ID)
var s = http.createServer(function(req, res) {
var params = req.url.split('/').slice(1);
setTimeout(function() {
if (process.argv[2] === 'fail') {
// we can only communicate with the test through an error. 404 is as good as any.
res.writeHead(404);
res.end('FAIL');
} else {
res.writeHead(200);
res.end("hello world\n");
}
res.writeHead(200);
res.end("hello world\n");
}, params[0] || 1);
});

s.listen(8000);

setTimeout(function() {
throw new Error("Unclean exit!");
}, 500);
9 changes: 8 additions & 1 deletion test/termination.js
Expand Up @@ -35,7 +35,14 @@ function pids() {
}

function activePids() {
return lib.balancer.activeWorkers().map(function(w) { return w.process.pid; });
var pids = [];
var aw = lib.balancer.activeWorkers();
for (var key in aw) {
if (key != 'length' && aw[key]) {
pids.push(aw[key].process.pid);
}
}
return pids;
}
function activeCount() {
return Object.keys(require('cluster').workers).length
Expand Down

0 comments on commit 2196173

Please sign in to comment.