From 6cd9e4d6875cc1f49962535dce209027f07ba74a Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 9 Jan 2017 14:59:35 -0800 Subject: [PATCH] workers: refactor. --- lib/workers/workerpool.js | 44 +++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/lib/workers/workerpool.js b/lib/workers/workerpool.js index 595ffc32..88a9072b 100644 --- a/lib/workers/workerpool.js +++ b/lib/workers/workerpool.js @@ -91,7 +91,8 @@ WorkerPool.cleanup = function cleanup() { WorkerPool._exitBound = false; /** - * Bind to process events in order to cleanup listeners. + * Bind to process events in + * order to cleanup listeners. * @private */ @@ -104,36 +105,40 @@ WorkerPool._bindExit = function _bindExit() { WorkerPool._exitBound = true; - function onExit(err) { + function onSignal() { WorkerPool.cleanup(); - - if (err) { - util.error(err.stack + ''); - process.exit(1); - return; - } - process.exit(0); } + function onError(err) { + WorkerPool.cleanup(); + if (err && err.stack) + util.error(err.stack + ''); + process.exit(1); + } + process.once('exit', function() { WorkerPool.cleanup(); }); if (process.listeners('SIGINT').length === 0) - process.once('SIGINT', onExit); + process.once('SIGINT', onSignal); if (process.listeners('SIGTERM').length === 0) - process.once('SIGTERM', onExit); + process.once('SIGTERM', onSignal); if (process.listeners('uncaughtException').length === 0) - process.once('uncaughtException', onExit); + process.once('uncaughtException', onError); process.on('newListener', function(name) { - if (name === 'SIGINT' - || name === 'SIGTERM' - || name === 'uncaughtException') { - process.removeListener(name, onExit); + switch (name) { + case 'SIGINT': + case 'SIGTERM': + process.removeListener(name, onSignal); + break; + case 'uncaughtException': + process.removeListener(name, onError); + break; } }); }; @@ -191,8 +196,8 @@ WorkerPool.prototype.alloc = function alloc() { */ WorkerPool.prototype.sendEvent = function sendEvent() { - var i, child; var result = true; + var i, child; for (i = 0; i < this.children.length; i++) { child = this.children[i]; @@ -229,7 +234,6 @@ WorkerPool.prototype.destroy = function destroy() { * @param {Packet} packet * @param {Number} timeout * @returns {Promise} - * the worker method specifies. */ WorkerPool.prototype.execute = function execute(packet, timeout) { @@ -377,7 +381,6 @@ WorkerPool.prototype.mine = co(function* mine(data, target, min, max) { * @param {Number} p * @param {Number} len * @returns {Promise} - * @returns {Buffer} */ WorkerPool.prototype.scrypt = co(function* scrypt(passwd, salt, N, r, p, len) { @@ -401,10 +404,11 @@ function Worker(id) { this.framer = new Framer(); this.parser = new Parser(); - this.setMaxListeners(util.MAX_SAFE_INTEGER); + this.id = id != null ? id : -1; this.child = null; this.pending = {}; + this.env = { BCOIN_WORKER_NETWORK: Network.type, BCOIN_WORKER_ISTTY: process.stdout