Skip to content

Commit

Permalink
Implement worker-specific timeouts
Browse files Browse the repository at this point in the history
Also added gitignore and a test
  • Loading branch information
felixge committed Jul 19, 2011
1 parent 23a2ff8 commit 4f1f262
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
*.un~
/node_modules
5 changes: 3 additions & 2 deletions index.js
@@ -1,8 +1,9 @@
module.exports = function isolatable() {
function isolatable(master) {
if (master.isWorker) {
master.isolateWorker = function() {
master.isolateWorker = function(timeout) {
try {
this.worker.timeout = timeout || this.worker.timeout;
this.server.close();
this.call('isolateWorker');
} catch (e) {
Expand All @@ -19,10 +20,10 @@ module.exports = function isolatable() {
return;
}

master.spawn(1);
worker.proc.kill('SIGQUIT');
master.removeWorker(worker.id);
master.emit('worker removed', worker);
master.spawnWorker(worker.id);
};
};

Expand Down
7 changes: 5 additions & 2 deletions package.json
Expand Up @@ -13,5 +13,8 @@
"node": "*"
},
"dependencies": {},
"devDependencies": {}
}
"devDependencies": {
"cluster": "0.6.4",
"fast-or-slow": "0.0.0"
}
}
63 changes: 63 additions & 0 deletions test.js
@@ -0,0 +1,63 @@
var test = require('fast-or-slow').slowTestCase();
var clusterIsolatable = require('./');
var cluster = require('cluster');
var http = require('http');
var assert = require('assert');

var options = {
host: 'localhost',
port: 3000,
};

test('isolateWorker with custom timeout', function(done) {
var server = http.createServer(function(req, res) {
var timeout = parseInt(req.url.substr(1), 10);
master.isolateWorker(timeout);

res.writeHead(200, {'X-Pid': process.pid});
res.write('Hi');
});

var master = cluster(server)
.set('workers', 1)
.set('timeout', 1500)
.use(clusterIsolatable())
.listen(options.port);


var timeouts = [500, 1000];
var expectedTimeouts = [];

master.on('worker connected', function() {
var timeout = timeouts.shift();
if (!timeout) {
return;
}

expectedTimeouts.push(timeout);
options.path = '/' + timeout;

http.get(options, function(res) {
var pid = res.headers['x-pid'];
process.kill(pid, 'SIGQUIT');
});
});

master.on('worker timeout', function(worker, timeout) {
var expected = expectedTimeouts.shift();
assert.equal(timeout, expected);

if (expectedTimeouts.length) {
return;
}

assert.equal(master.children.length, 1);
master.close();
});

process.on('exit', function() {
assert.strictEqual(expectedTimeouts.length, 0);
});
});


0 comments on commit 4f1f262

Please sign in to comment.