workers: refactor.

This commit is contained in:
Christopher Jeffrey 2017-01-09 14:59:35 -08:00
parent 9e4ea75198
commit 6cd9e4d687
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -91,7 +91,8 @@ WorkerPool.cleanup = function cleanup() {
WorkerPool._exitBound = false; WorkerPool._exitBound = false;
/** /**
* Bind to process events in order to cleanup listeners. * Bind to process events in
* order to cleanup listeners.
* @private * @private
*/ */
@ -104,36 +105,40 @@ WorkerPool._bindExit = function _bindExit() {
WorkerPool._exitBound = true; WorkerPool._exitBound = true;
function onExit(err) { function onSignal() {
WorkerPool.cleanup(); WorkerPool.cleanup();
if (err) {
util.error(err.stack + '');
process.exit(1);
return;
}
process.exit(0); process.exit(0);
} }
function onError(err) {
WorkerPool.cleanup();
if (err && err.stack)
util.error(err.stack + '');
process.exit(1);
}
process.once('exit', function() { process.once('exit', function() {
WorkerPool.cleanup(); WorkerPool.cleanup();
}); });
if (process.listeners('SIGINT').length === 0) if (process.listeners('SIGINT').length === 0)
process.once('SIGINT', onExit); process.once('SIGINT', onSignal);
if (process.listeners('SIGTERM').length === 0) if (process.listeners('SIGTERM').length === 0)
process.once('SIGTERM', onExit); process.once('SIGTERM', onSignal);
if (process.listeners('uncaughtException').length === 0) if (process.listeners('uncaughtException').length === 0)
process.once('uncaughtException', onExit); process.once('uncaughtException', onError);
process.on('newListener', function(name) { process.on('newListener', function(name) {
if (name === 'SIGINT' switch (name) {
|| name === 'SIGTERM' case 'SIGINT':
|| name === 'uncaughtException') { case 'SIGTERM':
process.removeListener(name, onExit); 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() { WorkerPool.prototype.sendEvent = function sendEvent() {
var i, child;
var result = true; var result = true;
var i, child;
for (i = 0; i < this.children.length; i++) { for (i = 0; i < this.children.length; i++) {
child = this.children[i]; child = this.children[i];
@ -229,7 +234,6 @@ WorkerPool.prototype.destroy = function destroy() {
* @param {Packet} packet * @param {Packet} packet
* @param {Number} timeout * @param {Number} timeout
* @returns {Promise} * @returns {Promise}
* the worker method specifies.
*/ */
WorkerPool.prototype.execute = function execute(packet, timeout) { 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} p
* @param {Number} len * @param {Number} len
* @returns {Promise} * @returns {Promise}
* @returns {Buffer}
*/ */
WorkerPool.prototype.scrypt = co(function* scrypt(passwd, salt, N, r, p, len) { WorkerPool.prototype.scrypt = co(function* scrypt(passwd, salt, N, r, p, len) {
@ -401,10 +404,11 @@ function Worker(id) {
this.framer = new Framer(); this.framer = new Framer();
this.parser = new Parser(); this.parser = new Parser();
this.setMaxListeners(util.MAX_SAFE_INTEGER);
this.id = id != null ? id : -1; this.id = id != null ? id : -1;
this.child = null; this.child = null;
this.pending = {}; this.pending = {};
this.env = { this.env = {
BCOIN_WORKER_NETWORK: Network.type, BCOIN_WORKER_NETWORK: Network.type,
BCOIN_WORKER_ISTTY: process.stdout BCOIN_WORKER_ISTTY: process.stdout