Comparing version 0.5.8 to 0.6.0
@@ -17,2 +17,13 @@ var Terror = require('terror'), | ||
* @constructor | ||
* @class LusterWorkerError | ||
* @augments LusterError | ||
*/ | ||
errors.LusterWorkerError = LusterError.create('LusterWorkerError', | ||
{ | ||
ALREADY_READY : | ||
'Worker#ready() called when worker is is "ready" state already' | ||
}); | ||
/** | ||
* @constructor | ||
* @class LusterConfigurationError | ||
@@ -19,0 +30,0 @@ * @augments LusterError |
var Objex = require('objex'), | ||
util = require('util'), | ||
EventEmitter = require('events').EventEmitter, | ||
@@ -12,2 +13,6 @@ | ||
function inspect(val) { | ||
return util.inspect(val, { depth : 1 }).replace(/^\s+/mg, ' ').replace(/\n/g, ''); | ||
} | ||
// add 'luster:eex' to the `NODE_DEBUG` environment variable to enable events logging | ||
@@ -17,6 +22,9 @@ if (process.env.NODE_DEBUG && /luster:eex/i.test(process.env.NODE_DEBUG)) { | ||
var iid = this.wid || this.id; | ||
var args = Array.prototype | ||
.slice.call(arguments, 0) | ||
.map(inspect); | ||
iid = typeof iid === 'undefined' ? '' : '(' + iid + ')'; | ||
console.log('%s%s.emit(%s)', this.constructor.name || 'EventEmitterEx', iid, Array.prototype.join.call(arguments, ', ')); | ||
console.log('%s%s.emit(%s)', this.constructor.name || 'EventEmitterEx', iid, args.join(', ')); | ||
@@ -23,0 +31,0 @@ return EventEmitterEx.__super.prototype.emit.apply(this, arguments); |
@@ -69,2 +69,5 @@ var cluster = require('cluster'), | ||
/** @type {WorkerWrapper} */ | ||
self = this, | ||
/** @type {Number|String} */ | ||
@@ -231,8 +234,16 @@ _port; | ||
master.on('worker initialized', this._onWorkerInitialized.bind(this)); | ||
this.on('initialized', this._onInitialized.bind(this)); | ||
WorkerWrapper._RPC_EVENTS.forEach(function(event) { | ||
master.on('received worker ' + event, WorkerWrapper.createEventTranslator(event).bind(this)); | ||
}, this); | ||
this.on('ready', this._onReady.bind(this)); | ||
}); | ||
WorkerWrapper.createEventTranslator = function(event) { | ||
return /** @this {WorkerWrapper} */function(worker) { | ||
if (this._worker && worker.id === this._worker.id) { | ||
this.emit(event); | ||
} | ||
}; | ||
}; | ||
WorkerWrapper.prototype._onReady = function() { | ||
@@ -243,6 +254,2 @@ this.ready = true; | ||
/** | ||
* @event Master#'worker initialized' | ||
*/ | ||
/** | ||
* Broadcast an event received by IPC from worker as 'worker <event>'. | ||
@@ -254,3 +261,3 @@ * @this {Master} | ||
WorkerWrapper.broadcastWorkerEvent = function(worker, event) { | ||
var args = [ 'worker ' + event, worker ], | ||
var args = [ 'received worker ' + event, worker ], | ||
i = 2, | ||
@@ -267,16 +274,2 @@ len = arguments.length; | ||
/** | ||
* @event WorkerWrapper#initalized | ||
*/ | ||
/** | ||
* @param {cluster.Worker} worker | ||
* @private | ||
*/ | ||
WorkerWrapper.prototype._onWorkerInitialized = function(worker) { | ||
if (this._worker && worker.id === this._worker.id) { | ||
this.emit('initialized'); | ||
} | ||
}; | ||
/** | ||
* Possible WorkerWrapper instance states. | ||
@@ -341,10 +334,2 @@ * @property {Object} STATES | ||
/** | ||
* event:WorkerWrapper#initalized handler | ||
* @private | ||
*/ | ||
WorkerWrapper.prototype._onInitialized = function() { | ||
this.emit('ready'); | ||
}; | ||
/** | ||
* event:cluster#fork handler | ||
@@ -459,2 +444,20 @@ * @private | ||
/** | ||
* Events received from workers via IPC | ||
* @memberOf WorkerWrapper | ||
* @property {String[]} _RPC_EVENTS | ||
* @static | ||
* @private | ||
*/ | ||
Object.defineProperty(WorkerWrapper, '_RPC_EVENTS', { | ||
value : Object.freeze([ | ||
'configured', | ||
'extension loaded', | ||
'initialized', | ||
'loaded', | ||
'ready' | ||
]), | ||
enumerable : true | ||
}); | ||
/** | ||
* All events which can be emitted by WorkerWrapper | ||
@@ -466,3 +469,7 @@ * @memberOf WorkerWrapper | ||
Object.defineProperty(WorkerWrapper, 'EVENTS', { | ||
value : Object.freeze([ 'error', 'state', 'ready', 'fork' ].concat(WorkerWrapper._PROXY_EVENTS)), | ||
value : Object.freeze( | ||
[ 'error', 'state', 'fork' ] | ||
.concat(WorkerWrapper._PROXY_EVENTS) | ||
.concat(WorkerWrapper._RPC_EVENTS) | ||
), | ||
enumerable : true | ||
@@ -627,2 +634,6 @@ }); | ||
WorkerWrapper.prototype.inspect = function() { | ||
return 'WW{ id:' + this.wid + ', state: ' + this.state + '}'; | ||
}; | ||
module.exports = WorkerWrapper; |
var cluster = require('cluster'), | ||
RPC = require('./rpc'), | ||
ClusterProcess = require('./cluster_process'), | ||
LusterWorkerError = require('./errors').LusterWorkerError, | ||
legacy = require('./legacy'), | ||
@@ -32,6 +33,10 @@ Worker; | ||
this.on('configured', broadcastEvent.bind(this, 'configured')); | ||
this.on('extension loaded', broadcastEvent.bind(this, 'extension loaded')); | ||
this.on('configured', broadcastEvent.bind(this, 'configured')); | ||
this.on('initialized', broadcastEvent.bind(this, 'initialized')); | ||
this.on('loaded', broadcastEvent.bind(this, 'loaded')); | ||
this.on('ready', broadcastEvent.bind(this, 'ready')); | ||
this._ready = false; | ||
this.registerRemoteCommand(RPC.fns.worker.applyForeignProperties, this.applyForeignProperties.bind(this)); | ||
@@ -48,3 +53,3 @@ }); | ||
/* jshint unused:false */ | ||
var args = [ RPC.fns.master.broadcastWorkerEvent], | ||
var args = [ RPC.fns.master.broadcastWorkerEvent ], | ||
i = 0, | ||
@@ -103,4 +108,8 @@ len = arguments.length; | ||
require(workerBase); | ||
this.emit('worker loaded', workerBase); | ||
this.emit('loaded', workerBase); | ||
if (!this.config.get('control.triggerReadyStateManually', false)) { | ||
legacy.setImmediate(this.ready.bind(this)); | ||
} | ||
return this; | ||
@@ -126,2 +135,19 @@ })); | ||
/** | ||
* Turns worker to `ready` state. Must be called by worker | ||
* if option `control.triggerReadyStateManually` set `true`. | ||
* @returns {Worker} self | ||
* @public | ||
*/ | ||
Worker.prototype.ready = function () { | ||
if (this._ready) { | ||
throw new LusterWorkerError(LusterWorkerError.ALREADY_READY); | ||
} | ||
this._ready = true; | ||
this.emit('ready'); | ||
return this; | ||
}; | ||
module.exports = Worker; |
{ | ||
"name" : "luster", | ||
"version" : "0.5.8", | ||
"version" : "0.6.0", | ||
"description" : "Node.js cluster wrapper", | ||
@@ -5,0 +5,0 @@ "main" : "./lib/luster.js", |
@@ -87,3 +87,11 @@ Luster [![NPM version][npm-image]][npm-link] | ||
// be marked as `dead` and no more automatic restarts will follow. | ||
allowedSequentialDeaths : 10 | ||
allowedSequentialDeaths : 10, | ||
// if falsy, worker is considered ready after 'online' event | ||
// it happens between forking worker and executing it | ||
// if truly, worker is considered ready | ||
// when you call require('luster').ready inside of it | ||
// notice that it's only affect startup/restart logic | ||
// worker will start handling requests right after you call 'listen' inside of it | ||
triggerReadyStateManually : false | ||
}, | ||
@@ -132,4 +140,4 @@ | ||
// if extensions' modules can't be resolved as related to | ||
// luster module or worker path, then absolute path | ||
// to the directory, which contains extensions modules | ||
// luster module or worker path, then absolute path | ||
// to the directory, which contains extensions modules | ||
// must be declared here: | ||
@@ -136,0 +144,0 @@ extensionsPath : "/usr/local/luster-extensions", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
83143
2047
227