-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow master disable worker refork (#110)
using `worker.disableRefork = true`
- Loading branch information
Showing
4 changed files
with
133 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use strict'; | ||
|
||
var path = require('path'); | ||
var util = require('util'); | ||
var cfork = require('../../'); | ||
|
||
cfork({ | ||
exec: path.join(__dirname, '../worker.js'), | ||
slaves: [ | ||
path.join(__dirname, '../slave.js') | ||
], | ||
args: [ 1984 ], | ||
limit: 4, | ||
count: 2, | ||
duration: 60000, | ||
autoCoverage: true, | ||
}) | ||
.on('fork', function (worker) { | ||
console.warn('[%s] [worker:%d] new worker start', Date(), worker.process.pid); | ||
}) | ||
.on('listening', function (worker, address) { | ||
console.warn('[%s] [worker:%d] listening on %j', Date(), worker.process.pid, address.port); | ||
process.send('listening'); | ||
}) | ||
.on('disconnect', function (worker) { | ||
var propertyName = worker.hasOwnProperty('exitedAfterDisconnect') ? 'exitedAfterDisconnect' : 'suicide'; | ||
console.warn('[%s] [master:%s] worker:%s disconnect, %s: %s, state: %s.', | ||
Date(), process.pid, worker.process.pid, propertyName, worker[propertyName], worker.state); | ||
}) | ||
.on('exit', function (worker, code, signal) { | ||
var exitCode = worker.process.exitCode; | ||
var propertyName = worker.hasOwnProperty('exitedAfterDisconnect') ? 'exitedAfterDisconnect' : 'suicide'; | ||
var err = new Error(util.format('worker %s died (code: %s, signal: %s, %s: %s, state: %s)', | ||
worker.process.pid, exitCode, signal, propertyName, worker[propertyName], worker.state)); | ||
err.name = 'WorkerDiedError'; | ||
console.error('[%s] [master:%s] worker exit: %s', Date(), process.pid, err.stack); | ||
}) | ||
.on('reachReforkLimit', function () { | ||
process.send('reach refork limit'); | ||
}); | ||
|
||
process.once('SIGTERM', function () { | ||
process.exit(0); | ||
}); | ||
|
||
var cluster = require('cluster'); | ||
var http = require('http'); | ||
var port = 1986; | ||
|
||
http.createServer(function (req, res) { | ||
// kill worker | ||
var count = 0; | ||
for (var id in cluster.workers) { | ||
var worker = cluster.workers[id]; | ||
worker.disableRefork = true; | ||
worker.process.kill('SIGTERM'); | ||
count++; | ||
} | ||
res.end('kill ' + count + ' workers'); | ||
}).listen(port); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict'; | ||
|
||
var assert = require('assert'); | ||
var urllib = require('urllib'); | ||
var childprocess = require('childprocess'); | ||
var path = require('path'); | ||
|
||
describe('kill_worker.test.test.js', function() { | ||
var child; | ||
var messages = []; | ||
|
||
before(function(done) { | ||
var workerNum = 2; | ||
var slaveNum = 1; | ||
var listeningCount = 0; | ||
child = childprocess.fork(path.join(__dirname, '..', 'fixtures', 'kill_worker', 'master.js')); | ||
child.on('message', function (m) { | ||
messages.push(m); | ||
console.log(m, listeningCount); | ||
if (m === 'listening') { | ||
++listeningCount; | ||
if (listeningCount === (workerNum + slaveNum)) { | ||
done(); | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
after(function(done) { | ||
setTimeout(function() { | ||
child.kill('SIGTERM'); | ||
setTimeout(done, 1000); | ||
}, 1000); | ||
}); | ||
|
||
it('should kill all workers', function(done) { | ||
urllib.request('http://localhost:1986/', function (err, body) { | ||
console.log(err, body.toString()); | ||
assert(!err); | ||
assert(body.toString() === 'kill 3 workers'); | ||
done(); | ||
}); | ||
}); | ||
}); |