Comparing version 0.3.1 to 0.4.0
@@ -139,2 +139,8 @@ var cluster = require('cluster'), | ||
if (wait === 0) { | ||
self._initialized = true; | ||
self.emit('initialized'); | ||
return; | ||
} | ||
extensions.forEach(function(name) { | ||
@@ -163,3 +169,3 @@ this.loadExtension(name, function(error) { | ||
LusterClusterProcessError.CODES.EXTENSIONS_LOAD_TIMEOUT, | ||
{ timeouted: timeouted, timeout: loadTimeout }); | ||
{ timeouted : timeouted, timeout : loadTimeout }); | ||
@@ -215,2 +221,11 @@ self.emit('error', error); | ||
/** | ||
* Checks is remote command registered. | ||
* @param {String} name | ||
* @returns {Boolean} | ||
*/ | ||
ClusterProcess.prototype.hasRegisteredRemoteCommand = function(name) { | ||
return has(this._remoteCommands, name); | ||
}; | ||
/** | ||
* @abstract | ||
@@ -217,0 +232,0 @@ * @throws LusterClusterProcessError if method is not overriden in the inheritor of ClusterProcess |
@@ -56,3 +56,8 @@ var os = require('os'), | ||
/** | ||
* Allows same object structure as cluster.setupMaster(). | ||
* This function must be used instead of cluster.setupMaster(), | ||
* because all calls of cluster.setupMaster() ignored, except first one. | ||
* An instance of Master will call it, when running. | ||
* @param {Object} opts | ||
* @see {@link http://nodejs.org/api/cluster.html#cluster_cluster_setupmaster_settings} | ||
*/ | ||
@@ -235,3 +240,3 @@ Master.prototype.setup = function(opts) { | ||
while (count > i++) { | ||
this.add(new WorkerWrapper({ | ||
this.add(new WorkerWrapper(this, { | ||
forkTimeout : forkTimeout, | ||
@@ -238,0 +243,0 @@ stopTimeout : stopTimeout, |
@@ -43,2 +43,14 @@ /** | ||
} | ||
}, | ||
/** | ||
* Core remote functions dictionaries | ||
*/ | ||
fns : { | ||
worker : { | ||
applyForeignProperties : 'core.worker.applyForeignProperties' | ||
}, | ||
master : { | ||
broadcastWorkerEvent : 'core.master.broadcastWorkerEvent' | ||
} | ||
} | ||
@@ -45,0 +57,0 @@ }; |
@@ -44,2 +44,3 @@ var cluster = require('cluster'), | ||
* @augments EventEmitterEx | ||
* @param {Master} master | ||
* @param {WorkerWrapperOptions} options | ||
@@ -52,4 +53,4 @@ * | ||
*/ | ||
WorkerWrapper = EventEmitterEx.create(function WorkerWrapper(options) { | ||
WorkerWrapper.__super.apply(this, arguments); | ||
WorkerWrapper = EventEmitterEx.create(function WorkerWrapper(master, options) { | ||
WorkerWrapper.__super.call(this); | ||
@@ -202,5 +203,49 @@ if (options && | ||
this.on('exit', this._onExit.bind(this)); | ||
// register global remote command in the context of master to receive events from master | ||
if ( ! master.hasRegisteredRemoteCommand(RPC.fns.master.broadcastWorkerEvent)) { | ||
master.registerRemoteCommand(RPC.fns.master.broadcastWorkerEvent, WorkerWrapper.broadcastWorkerEvent.bind(master)); | ||
} | ||
master.on('worker initialized', this._onWorkerInitialized.bind(this)); | ||
this.on('initialized', this._onInitialized.bind(this)); | ||
}); | ||
/** | ||
* @event Master#'worker initialized' | ||
*/ | ||
/** | ||
* Broadcast an event received by IPC from worker as 'worker <event>'. | ||
* @this {Master} | ||
* @param {WorkerWrapper} worker | ||
* @param {String} event | ||
*/ | ||
WorkerWrapper.broadcastWorkerEvent = function(worker, event) { | ||
var args = [ 'worker ' + event, worker ], | ||
i = 2, | ||
len = arguments.length; | ||
for (; i < len; i++) { | ||
args.push(arguments[i]); | ||
} | ||
this.emit.apply(this, args); | ||
}; | ||
/** | ||
* @event WorkerWrapper#initalized | ||
*/ | ||
/** | ||
* @param {cluster.Worker} worker | ||
* @private | ||
*/ | ||
WorkerWrapper.prototype._onWorkerInitialized = function(worker) { | ||
if (worker.id === this._worker.id) { | ||
this.emit('initialized'); | ||
} | ||
}; | ||
/** | ||
* Possible WorkerWrapper instance states. | ||
@@ -256,7 +301,12 @@ * @property {Object} STATES | ||
// pass some of the {WorkerWrapper} properties to {Worker} | ||
// @todo: kaero: move RPC functions names to dictionary in the shared file | ||
this.remoteCall('core.worker.applyForeignProperties', { | ||
this.remoteCall(RPC.fns.worker.applyForeignProperties, { | ||
wid : this.wid | ||
}); | ||
}; | ||
/** | ||
* event:WorkerWrapper#initalized handler | ||
* @private | ||
*/ | ||
WorkerWrapper.prototype._onInitialized = function() { | ||
if (typeof this.options.port === 'undefined') { | ||
@@ -263,0 +313,0 @@ this.emit('ready'); |
@@ -26,7 +26,31 @@ var cluster = require('cluster'), | ||
// @todo: kaero: move RPC functions names to dictionary in the shared file | ||
this.registerRemoteCommand('core.worker.applyForeignProperties', this.applyForeignProperties.bind(this)); | ||
var broadcastEvent = this._broadcastEvent; | ||
this.on('extension loaded', broadcastEvent.bind(this, 'extension loaded')); | ||
this.on('configured', broadcastEvent.bind(this, 'configured')); | ||
this.on('initialized', broadcastEvent.bind(this, 'initialized')); | ||
this.registerRemoteCommand(RPC.fns.worker.applyForeignProperties, this.applyForeignProperties.bind(this)); | ||
}); | ||
/** | ||
* Transmit worker event to master, which plays as relay, | ||
* retransmitting it as 'worker <event>' to all master-side listeners. | ||
* @param {String} event Event name | ||
* @private | ||
*/ | ||
Worker.prototype._broadcastEvent = function(event) { | ||
/* jshint unused:false */ | ||
var args = [ RPC.fns.master.broadcastWorkerEvent], | ||
i = 0, | ||
len = arguments.length; | ||
for (; i < len; i++) { | ||
args.push(arguments[i]); | ||
} | ||
this.remoteCall.apply(this, args); | ||
}; | ||
/** | ||
* Extend {Worker} properties with passed by {Master}. | ||
@@ -37,3 +61,3 @@ * @param {ClusterProcess} proc | ||
Worker.prototype.applyForeignProperties = function(proc, props) { | ||
Object.keys( props) | ||
Object.keys(props) | ||
.forEach(function(propName) { | ||
@@ -40,0 +64,0 @@ Object.defineProperty(this, propName, { |
{ | ||
"name" : "luster", | ||
"version" : "0.3.1", | ||
"version" : "0.4.0", | ||
"description" : "Node.js cluster wrapper", | ||
@@ -10,3 +10,3 @@ "main" : "./lib/luster.js", | ||
"scripts" : { | ||
"test" : "./node_modules/.bin/jshint --config ./.jshintrc ./lib ./test && ./node_modules/.bin/jscs ./lib ./test" | ||
"test" : "./node_modules/.bin/jshint --config ./.jshintrc ./lib && ./node_modules/.bin/jscs ./lib" | ||
}, | ||
@@ -38,7 +38,8 @@ "repository" : { | ||
"objex" : "0.3.x", | ||
"terror" : "0.4.x" | ||
"terror" : "0.4.x", | ||
"extend": "2.0.0" | ||
}, | ||
"devDependencies" : { | ||
"jshint" : "2.1.x", | ||
"jscs" : ">=1.0.5" | ||
"jscs" : "1.4.x" | ||
}, | ||
@@ -45,0 +46,0 @@ "engines" : { |
101292
1955
3
+ Addedextend@2.0.0
+ Addedextend@2.0.0(transitive)