Skip to content

Commit

Permalink
multiple call support
Browse files Browse the repository at this point in the history
  • Loading branch information
7nights committed May 14, 2015
1 parent 1a370bf commit 909f71d
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,19 @@ module.exports = function(file, opt) {
// Fork a new worker. Give it a recluster ID and
// also redirect all its messages to the cluster.
function fork(wid) {
cluster.setupMaster({exec: file});
cluster.settings.args = opt.args;
var w = cluster.fork({WORKER_ID: wid});
w._rc_wid = wid;
w._rc_isReplaced = false;
w.on('message', function(message) {
emit('message', w, message);
});
w.on('exit', function () {emit('exit', w);});
w.on('disconnect', function () {emit('disconnect', w);});
w.on('listening', function (addr) {emit('listening', w, addr);});
w.on('online', function () {emit('online', w);});

w.process.on('exit', function() {
var windex = self.workers.indexOf(w);
if (windex >= 0)
Expand Down Expand Up @@ -186,15 +193,8 @@ module.exports = function(file, opt) {

self.run = function() {
if (!cluster.isMaster) return;
cluster.setupMaster({exec: file});
cluster.settings.args = opt.args;
for (var i = 0; i < opt.workers; i++) fork(i);

cluster.on('exit', workerEmitExit);
cluster.on('disconnect', workerDisconnect);
cluster.on('listening', workerListening);
cluster.on('online', workerOnline);

channel.on(readyEvent, function workerReady(w, arg) {
// ignore unrelated messages when readyEvent = message
if (readyEvent === 'message'
Expand Down Expand Up @@ -261,10 +261,6 @@ module.exports = function(file, opt) {

self.stop = function() {
if (!cluster.isMaster) return;
cluster.removeListener('exit', workerEmitExit);
cluster.removeListener('disconnect', workerDisconnect);
cluster.removeListener('listening', workerListening);
cluster.removeListener('online', workerOnline);
respawners.cancel();

channel.removeAllListeners();
Expand Down

0 comments on commit 909f71d

Please sign in to comment.