Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

isolated exit from disconnect

  • Loading branch information...
commit 70f1c972a8d21eca05e8fcc2d6ef3e25ad846c32 1 parent c7b6a18
@AndreasMadsen AndreasMadsen authored
Showing with 28 additions and 34 deletions.
  1. +28 −34 lib/cluster.js
View
62 lib/cluster.js
@@ -217,7 +217,7 @@ function handleCallback(outMessage, outHandle, inWrap, inHandle, worker) {
queryCallbacks[inWrap._queryEcho](inWrap.content, inHandle);
delete queryCallbacks[inWrap._queryEcho];
}
-
+
//Send if outWrap do contain something useful
if (!(outWrap.content === undefined && outWrap._queryEcho === undefined)) {
worker.send(outWrap, outHandle);
@@ -315,8 +315,9 @@ if (cluster.isMaster) {
send();
worker.process._channel.close();
- worker.emit('disconnect', worker);
- cluster.emit('disconnect', worker);
+
+ //Prepear worker to die and emit events
+ prepearDeath(worker, 'disconnect', ['disconnect', 'disconnect']);
}
};
@@ -417,45 +418,38 @@ function Worker(env, workerID) {
self.process.on('exit', function() {
debug('worker id=' + self.uniqueID + ' died');
- //set state to dead so the user can check for old worker objects
- self.state = 'dead';
-
- //Make suicide a boolean
- self.suicide = !!self.suicide;
-
- //Remove from workers in the master
- if (cluster.isMaster) {
- delete cluster.workers[self.uniqueID];
- }
-
- //Emit exit and death
- self.emit('exit', self);
- cluster.emit('death', self);
+ //Prepear worker to die and emit events
+ prepearDeath(self, 'dead', ['exit', 'death']);
});
//Handle disconnect
self.on('disconnect', function() {
debug('worker id=' + self.uniqueID + ' disconnect');
-
- //set state to disconnect
- self.disconnect = 'disconnect';
-
- //Make suicide a boolean
- self.suicide = !!self.suicide;
-
- //Remove from workers in the master
- if (cluster.isMaster) {
- delete cluster.workers[self.uniqueID];
- }
-
- //Emit exit and death
- self.emit('exit', self);
- cluster.emit('death', self);
+
+ //prepearDeath will be called by the messageHandler
});
}
util.inherits(Worker, EventEmitter);
+function prepearDeath(worker, state, events) {
+
+ //set state to disconnect
+ worker.state = state;
+
+ //Make suicide a boolean
+ worker.suicide = !!worker.suicide;
+
+ //Remove from workers in the master
+ if (cluster.isMaster) {
+ delete cluster.workers[worker.uniqueID];
+ }
+
+ //Emit events
+ worker.emit(events[0], worker);
+ cluster.emit(events[1], worker);
+}
+
//Send message to worker or master
Worker.prototype.send = function(/*message, handler, callback*/) {
@@ -483,10 +477,10 @@ Worker.prototype.send = function(/*message, handler, callback*/) {
wrap._requestEcho = this.uniqueID + ':' + (++queryIds);
queryCallbacks[wrap._requestEcho] = callback;
}
-
+
//Save wrap
args[0] = wrap;
-
+
// Send message
if (this.process._channel !== null) {
this.process.send.apply(this.process, args);
Please sign in to comment.
Something went wrong with that request. Please try again.