worker debug.

This commit is contained in:
Christopher Jeffrey 2016-05-06 23:28:14 -07:00
parent 9b2d80098a
commit 6fb0689ce4
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 45 additions and 19 deletions

View File

@ -15,10 +15,14 @@ if (typeof importScripts !== 'undefined') {
penv = JSON.parse(event.data);
env = self.bcoin.env(penv.BCOIN_WORKER_NETWORK);
env.workers.listen(+penv.BCOIN_WORKER_ID);
env.workers.listen(+penv.BCOIN_WORKER_ID, {
debug: +penv.BCOIN_WORKER_DEBUG === 1
});
};
} else {
penv = process.env;
env = require('./env')(penv.BCOIN_WORKER_NETWORK);
env.workers.listen(+penv.BCOIN_WORKER_ID);
env.workers.listen(+penv.BCOIN_WORKER_ID, {
debug: +penv.BCOIN_WORKER_DEBUG === 1
});
}

View File

@ -215,7 +215,7 @@ Workers.prototype.mine = function mine(attempt, callback) {
function Worker(id) {
var self = this;
var cp;
var penv, cp;
if (!(this instanceof Worker))
return new Worker(id);
@ -227,6 +227,12 @@ function Worker(id) {
this.parser = new Parser();
this.setMaxListeners(utils.MAX_SAFE_INTEGER);
penv = {
BCOIN_WORKER_ID: id + '',
BCOIN_WORKER_NETWORK: network.type,
BCOIN_WORKER_DEBUG: (bcoin.debugLogs || bcoin.debugFile) ? '1' : '0'
};
if (bcoin.isBrowser) {
this.child = new global.Worker('/bcoin-worker.js');
@ -246,19 +252,13 @@ function Worker(id) {
self.emit('data', data);
};
this.child.postMessage(JSON.stringify({
BCOIN_WORKER_ID: id + '',
BCOIN_WORKER_NETWORK: network.type
}));
this.child.postMessage(JSON.stringify(penv));
} else {
cp = require('child_' + 'process');
this.child = cp.spawn(process.argv[0], [__dirname + '/worker.js'], {
stdio: 'pipe',
env: utils.merge({}, process.env, {
BCOIN_WORKER_ID: id + '',
BCOIN_WORKER_NETWORK: network.type
})
env: utils.merge({}, process.env, penv)
});
this.child.on('error', function(err) {
@ -279,7 +279,7 @@ function Worker(id) {
this.child.stderr.setEncoding('utf8');
this.child.stderr.on('data', function(data) {
bcoin.debug(data);
bcoin.debug(data.trim());
});
}
@ -403,10 +403,11 @@ utils.inherits(Worker, EventEmitter);
* @exports Master
* @constructor
* @param {Number} id - Worker ID.
* @param {Object?} options
* @property {Number} id
*/
function Master(id) {
function Master(id, options) {
var self = this;
if (!(this instanceof Master))
@ -417,6 +418,7 @@ function Master(id) {
this.id = id;
this.framer = new Framer();
this.parser = new Parser();
this.options = options || {};
if (bcoin.isBrowser) {
global.onerror = function onerror(err) {
@ -503,10 +505,25 @@ Master.prototype.sendEvent = function sendEvent() {
*/
Master.prototype.log = function log() {
if (bcoin.isBrowser)
return console.error.apply(console.error, arguments);
process.stderr.write('Worker ' + this.id + ': ');
return console.error.apply(console.error, arguments);
var args, msg;
if (!this.options.debug)
return;
args = Array.prototype.slice.call(arguments);
msg = typeof args[0] !== 'object'
? utils.format(args, false)
: args[0];
msg = utils.format(['Worker %d: %s', this.id, msg], false);
if (bcoin.isBrowser) {
console.error(msg);
return;
}
process.stderr.write(msg + '\n');
};
/**
@ -522,11 +539,12 @@ Master.prototype.destroy = function destroy() {
/**
* Listen for messages from master process (only if worker).
* @param {Number} id - Worker id.
* @param {Object?} options
* @returns {Master}
*/
Master.listen = function listen(id) {
var master = new Master(id);
Master.listen = function listen(id, options) {
var master = new Master(id, options);
var log = master.log.bind(master);
bcoin.debug = log;

View File

@ -11,6 +11,8 @@ describe('Chain', function() {
var chain, wallet, miner;
var competingTip, oldTip, ch1, ch2, cb1, cb2;
this.timeout(5000);
chain = new bcoin.chain({ name: 'chain-test', db: 'memory' });
wallet = new bcoin.wallet();
miner = new bcoin.miner({

View File

@ -6,6 +6,8 @@ var assert = require('assert');
var opcodes = constants.opcodes;
describe('Mempool', function() {
this.timeout(5000);
var chain = new bcoin.chain({
name: 'mp-chain',
db: 'memory'