Browse Source

Single process when running as a Bitcore Node service.

activeAddress
Braydon Fuller 9 years ago
parent
commit
8ea3e6c278
  1. 4
      bcmonitor/bcmonitor.js
  2. 162
      bitcorenode/index.js
  3. 3
      bws.js
  4. 3
      emailservice/emailservice.js
  5. 217
      test/bitcorenode.js

4
bcmonitor/bcmonitor.js

@ -7,10 +7,6 @@ var log = require('npmlog');
log.debug = log.verbose;
var config = require('../config');
if (process.argv[2]) {
config = JSON.parse(process.argv[2]);
}
var BlockchainMonitor = require('../lib/blockchainmonitor');
var bcm = new BlockchainMonitor();

162
bitcorenode/index.js

@ -2,11 +2,19 @@
var util = require('util');
var fs = require('fs');
var io = require('socket.io');
var https = require('https');
var http = require('http');
var async = require('async');
var path = require('path');
var bitcore = require('bitcore');
var Networks = bitcore.Networks;
var mkdirp = require('mkdirp');
var Locker = require('locker-server');
var BlockchainMonitor = require('../lib/blockchainmonitor');
var EmailService = require('../lib/emailservice');
var ExpressApp = require('../lib/expressapp');
var WsApp = require('../lib/wsapp');
var child_process = require('child_process');
var spawn = child_process.spawn;
var EventEmitter = require('events').EventEmitter;
@ -16,19 +24,48 @@ var Service = function(options) {
EventEmitter.call(this);
this.node = options.node;
this.children = [];
this.https = options.https || this.node.https;
this.httpsOptions = options.httpsOptions || this.node.httpsOptions;
this.bwsPort = options.bwsPort || Service.BWS_PORT;
this.messageBrokerPort = options.messageBrokerPort || Service.MESSAGE_BROKER_PORT;
this.lockerPort = options.lockerPort || Service.LOCKER_PORT;
};
util.inherits(Service, EventEmitter);
Service.dependencies = ['bitcoind', 'db', 'address', 'insight-api'];
Service.BWS_PORT = 3232;
Service.MESSAGE_BROKER_PORT = 3380;
Service.LOCKER_PORT = 3231;
Service.prototype.blockHandler = function(block, add, callback) {
setImmediate(function() {
callback(null, []);
});
Service.dependencies = ['insight-api'];
/**
* This method will read `key` and `cert` files from disk based on `httpsOptions` and
* return `serverOpts` with the read files.
*/
Service.prototype.readHttpsOptions = function() {
if(!this.httpsOptions || !this.httpsOptions.key || !this.httpsOptions.cert) {
throw new Error('Missing https options');
}
var serverOpts = {};
serverOpts.key = fs.readFileSync(this.httpOptions.key);
serverOpts.cert = fs.readFileSync(this.httpsOptions.cert);
// This sets the intermediate CA certs only if they have all been designated in the config.js
if (this.httpsOptions.CAinter1 && this.httpsOptions.CAinter2 && this.httpsOptions.CAroot) {
serverOpts.ca = [
fs.readFileSync(this.httpsOptions.CAinter1),
fs.readFileSync(this.httpsOptions.CAinter2),
fs.readFileSync(this.httpsOptions.CAroot)
];
}
return serverOpts;
};
/**
* Called by the node to start the service
*/
Service.prototype.start = function(done) {
var self = this;
@ -53,68 +90,70 @@ Service.prototype.start = function(done) {
return done(new Error('Unknown network'));
}
var services = [
['locker.log', 'locker/locker.js'],
['messagebroker.log', 'messagebroker/messagebroker.js'],
['bcmonitor.log', 'bcmonitor/bcmonitor.js', JSON.stringify(baseConfig)],
['emailservice.log', 'emailservice/emailservice.js', JSON.stringify(baseConfig)],
['bws.log', 'bws.js', JSON.stringify(baseConfig)],
];
var basePath = path.resolve(__dirname, '..');
var logBasePath = path.resolve(self.node.datadir, './bws-logs/');
// Make sure that the logs directory exists
if (!fs.existsSync(logBasePath)) {
mkdirp.sync(logBasePath);
}
async.eachSeries(
services,
function(service, next) {
async.series([
function(next) {
// Locker Server
var locker = new Locker();
locker.listen(self.lockerPort);
// Message Broker
var messageServer = io(self.messageBrokerPort);
messageServer.on('connection', function(s) {
s.on('msg', function(d) {
messageServer.emit('msg', d);
});
});
// Blockchain Monitor
var blockChainMonitor = new BlockchainMonitor();
blockChainMonitor.start(baseConfig, next);
},
function(next) {
if (baseConfig.emailOpts) {
var emailService = new EmailService();
emailService.start(baseConfig, next);
} else {
setImmediate(next);
}
},
function(next) {
var logPath = path.resolve(logBasePath, service[0]);
var servicePath = path.resolve(basePath, service[1]);
var config = service[2];
var expressApp = new ExpressApp();
var wsApp = new WsApp();
var stderr = fs.openSync(logPath, 'a+');
var stdout = stderr;
if (self.https) {
var serverOpts = self.readHttpsOptions();
self.server = https.createServer(serverOpts, expressApp.app);
} else {
self.server = http.Server(expressApp.app);
}
var options = {
stdio: ['ignore', stdout, stderr],
cwd: basePath,
env: process.env
};
async.parallel([
function(done) {
expressApp.start(baseConfig, done);
},
function(done) {
wsApp.start(self.server, baseConfig, done);
},
], function(err) {
if (err) {
return next(err);
}
self.server.listen(self.bwsPort, next);
});
var child = spawn('node', [servicePath, config], options);
self.children.push(child);
next();
},
function(err) {
if (err) {
return done(err);
}
done();
}
);
], done);
};
/**
* Called by node to stop the service
*/
Service.prototype.stop = function(done) {
var self = this;
async.eachSeries(
self.children,
function(child, next) {
child.kill();
next();
},
function(err) {
if (err) {
return done(err);
}
done();
}
);
setImmediate(function() {
done();
});
};
Service.prototype.getAPIMethods = function() {
@ -125,9 +164,4 @@ Service.prototype.getPublishEvents = function() {
return [];
};
Service.prototype.setupRoutes = function(app) {
// TODO: Run bws express/websocket app (setup routes and events on the web service)
};
module.exports = Service;

3
bws.js

@ -6,9 +6,6 @@ var fs = require('fs');
var ExpressApp = require('./lib/expressapp');
var WsApp = require('./lib/wsapp');
var config = require('./config');
if (process.argv[2]) {
config = JSON.parse(process.argv[2]);
}
var sticky = require('sticky-session');
var log = require('npmlog');
log.debug = log.verbose;

3
emailservice/emailservice.js

@ -7,9 +7,6 @@ var log = require('npmlog');
log.debug = log.verbose;
var config = require('../config');
if (process.argv[2]) {
config = JSON.parse(process.argv[2]);
}
var EmailService = require('../lib/emailservice');
var emailService = new EmailService();

217
test/bitcorenode.js

@ -1,217 +0,0 @@
'use strict';
var should = require('chai').should();
var sinon = require('sinon');
var bitcore = require('bitcore');
var path = require('path');
var proxyquire = require('proxyquire');
describe('Bitcore Node Service', function() {
describe('#start', function() {
it('will create a log directory if it doesn\'t exist', function(done) {
var node = {
network: bitcore.Networks.testnet,
datadir: './testdir',
port: 3001
};
var mkdirpSync = sinon.stub();
var Service = proxyquire('../bitcorenode', {
fs: {
existsSync: sinon.stub().returns(false),
openSync: sinon.stub()
},
child_process: {
spawn: sinon.stub()
},
mkdirp: {
sync: mkdirpSync
}
});
var node = {
network: bitcore.Networks.testnet,
datadir: './testdir',
port: 3001
};
var service = new Service({node: node});
service.start(function() {
mkdirpSync.callCount.should.equal(1);
mkdirpSync.args[0][0].should.equal(path.resolve(node.datadir, './bws-logs'));
done();
});
});
it('will call spawn with the correct arguments', function(done) {
var spawnCallCount = 0;
var basePath = path.resolve(__dirname, '../');
var expectedScripts = [
path.resolve(basePath, 'locker/locker.js'),
path.resolve(basePath, 'messagebroker/messagebroker.js'),
path.resolve(basePath, 'bcmonitor/bcmonitor.js'),
path.resolve(basePath, 'emailservice/emailservice.js'),
path.resolve(basePath, 'bws.js'),
];
var baseConfig = require('../config');
baseConfig.blockchainExplorerOpts = {
testnet: {
provider: 'insight',
url: 'http://localhost:3001',
apiPrefix: '/insight-api'
}
};
var expectedArgs = [
undefined,
undefined,
JSON.stringify(baseConfig),
JSON.stringify(baseConfig),
JSON.stringify(baseConfig)
];
var spawn = function(program, args, options) {
program.should.equal('node');
args.length.should.equal(2);
args[0].should.equal(expectedScripts[spawnCallCount]);
should.equal(args[1], expectedArgs[spawnCallCount]);
options.stdio.length.should.equal(3);
options.stdio[0].should.equal('ignore');
options.stdio[1].should.equal(fileStream);
options.stdio[2].should.equal(fileStream);
options.cwd.should.equal(path.resolve(__dirname, '..'));
should.equal(process.env, options.env);
spawnCallCount++;
};
var mkdirpSync = sinon.stub().returns(true);
var existsSync = sinon.stub();
var fileStream = {};
var openSync = sinon.stub().returns(fileStream);
var Service = proxyquire('../bitcorenode', {
fs: {
existsSync: existsSync,
openSync: openSync
},
child_process: {
spawn: spawn
},
mkdirp: {
sync: mkdirpSync
}
});
var node = {
network: bitcore.Networks.testnet,
datadir: './testdir',
port: 3001
};
var service = new Service({node: node});
service.start(function() {
service.children.length.should.equal(5);
done();
});
});
it('will give an error with unknown network', function(done) {
var Service = proxyquire('../bitcorenode', {
fs: {
existsSync: sinon.stub(),
openSync: sinon.stub()
},
child_process: {
spawn: sinon.stub()
},
mkdirp: {
sync: sinon.stub()
}
});
var node = {
network: 'unknown',
datadir: './testdir',
port: 3001
};
var service = new Service({node: node});
service.start(function(err) {
err.message.should.equal('Unknown network');
done();
});
});
it('will use livenet', function(done) {
var baseConfig = require('../config');
baseConfig.blockchainExplorerOpts = {
livenet: {
provider: 'insight',
url: 'http://localhost:3001',
apiPrefix: '/insight-api'
}
};
var spawnCallCount = 0;
var expectedArgs = [
undefined,
undefined,
JSON.stringify(baseConfig),
JSON.stringify(baseConfig),
JSON.stringify(baseConfig)
];
var spawn = function(program, args, options) {
should.equal(args[1], expectedArgs[spawnCallCount]);
spawnCallCount++;
};
var Service = proxyquire('../bitcorenode', {
fs: {
existsSync: sinon.stub(),
openSync: sinon.stub()
},
child_process: {
spawn: spawn
},
mkdirp: {
sync: sinon.stub()
}
});
var node = {
network: bitcore.Networks.livenet,
datadir: './testdir',
port: 3001
};
var service = new Service({node: node});
service.start(function() {
spawnCallCount.should.equal(5);
done();
});
});
});
describe('#stop', function() {
it('will call kill an each process', function() {
var Service = proxyquire('../bitcorenode', {});
var node = {
network: bitcore.Networks.testnet,
datadir: './testdir',
port: 3001
};
var service = new Service({node: node});
var childProcess = {
kill: sinon.stub()
};
service.children = [childProcess];
service.stop(function() {
childProcess.kill.callCount.should.equal(1);
});
});
});
});
Loading…
Cancel
Save