Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
Merge pull request #601 from matiu/ref/rm-websockets
Browse files Browse the repository at this point in the history
rm websockets
  • Loading branch information
matiu authored Nov 24, 2016
2 parents 06a3aa4 + fbe7dc4 commit 17fb1bc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 194 deletions.
12 changes: 1 addition & 11 deletions bitcorenode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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;
Expand Down Expand Up @@ -110,7 +109,6 @@ Service.prototype._getConfiguration = function() {
Service.prototype._startWalletService = function(config, next) {
var self = this;
var expressApp = new ExpressApp();
var wsApp = new WsApp();

if (self.https) {
var serverOpts = self._readHttpsOptions();
Expand All @@ -119,15 +117,7 @@ Service.prototype._startWalletService = function(config, next) {
self.server = http.Server(expressApp.app);
}

async.parallel([

function(done) {
expressApp.start(config, done);
},
function(done) {
wsApp.start(self.server, config, done);
},
], function(err) {
expressApp.start(config, function(err){
if (err) {
return next(err);
}
Expand Down
95 changes: 44 additions & 51 deletions bws.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ var async = require('async');
var fs = require('fs');

var ExpressApp = require('./lib/expressapp');
var WsApp = require('./lib/wsapp');
var config = require('./config');
var sticky = require('sticky-session');
var log = require('npmlog');
log.debug = log.verbose;
log.disableColor();
Expand Down Expand Up @@ -41,60 +39,55 @@ if (config.https) {
};
}

var start = function(cb) {
var expressApp = new ExpressApp();
var wsApp = new WsApp();

function doStart(cb) {
var server = config.https ? serverModule.createServer(serverOpts, expressApp.app) : serverModule.Server(expressApp.app);

server.on('connection', function(socket) {
socket.setTimeout(300 * 1000);
})

async.parallel([

function(done) {
expressApp.start(config, done);
},
function(done) {
wsApp.start(server, config, done);
},
], function(err) {
if (err) {
log.error('Could not start BWS instance', err);
}
if (cb) return cb(err);
});

return server;
};

if (config.cluster) {
var server = sticky(clusterInstances, function() {
return doStart();
});
return cb(null, server);
} else {
var server = doStart(function(err) {
return cb(err, server);
});
}
};

if (config.cluster && !config.lockOpts.lockerServer)
throw 'When running in cluster mode, locker server need to be configured';

if (config.cluster && !config.messageBrokerOpts.messageBrokerServer)
throw 'When running in cluster mode, message broker server need to be configured';

start(function(err, server) {
if (err) {
console.log('Could not start BWS:', err);
process.exit(0);
var expressApp = new ExpressApp();

function startInstance(cb) {
var server = config.https ? serverModule.createServer(serverOpts, expressApp.app) : serverModule.Server(expressApp.app);

server.on('connection', function(socket) {
socket.setTimeout(300 * 1000);
})

expressApp.start(config, function(err) {
if (err) {
log.error('Could not start BWS instance', err);
return;
}

server.listen(port);

var instanceInfo = cluster.worker ? ' [Instance:' + cluster.worker.id + ']' : '';
log.info('BWS running ' + instanceInfo);
return;
});
};

if (config.cluster && cluster.isMaster) {

// Count the machine's CPUs
var instances = config.clusterInstances || require('os').cpus().length;

log.info('Starting ' + instances + ' instances');

// Create a worker for each CPU
for (var i = 0; i < instances; i += 1) {
cluster.fork();
}
server.listen(port, function(err) {
if (err) console.log('ERROR: ', err);
log.info('Bitcore Wallet Service running on port ' + port);

// Listen for dying workers
cluster.on('exit', function(worker) {
// Replace the dead worker,
log.error('Worker ' + worker.id + ' died :(');
cluster.fork();
});
});
// Code to run if we're in a worker process
} else {
log.info('Listening on port: ' + port);
startInstance();
};
4 changes: 3 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ var config = {
basePath: '/bws/api',
disableLogs: false,
port: 3232,

// Uncomment to make BWS a forking server
// cluster: true,
// Uncomment to use the nr of availalbe CPUs

// Uncomment to set the number or process (will use the nr of availalbe CPUs by default)
// clusterInstances: 4,

// https: true,
Expand Down
65 changes: 0 additions & 65 deletions lib/wsapp.js

This file was deleted.

66 changes: 0 additions & 66 deletions test/bitcorenode.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,45 +138,6 @@ describe('Bitcore Node Service', function() {
});
});
describe('#_startWalletService', function() {
it('will start express and web socket servers', function(done) {
function TestExpressApp() {}
TestExpressApp.prototype.start = sinon.stub().callsArg(1);
function TestWSApp() {}
TestWSApp.prototype.start = sinon.stub().callsArg(2);
var listen = sinon.stub().callsArg(1);
var TestService = proxyquire('../bitcorenode', {
'../lib/expressapp': TestExpressApp,
'../lib/wsapp': TestWSApp,
'http': {
Server: sinon.stub().returns({
listen: listen
})
}
});
var options = {
node: {
bwsPort: 3232
}
};
var service = new TestService(options);
var config = {};
service._startWalletService(config, function(err) {
if (err) {
throw err;
}
TestExpressApp.prototype.start.callCount.should.equal(1);
TestExpressApp.prototype.start.args[0][0].should.equal(config);
TestExpressApp.prototype.start.args[0][1].should.be.a('function');
TestWSApp.prototype.start.callCount.should.equal(1);
TestWSApp.prototype.start.args[0][0].should.equal(service.server);
TestWSApp.prototype.start.args[0][1].should.equal(config);
TestWSApp.prototype.start.args[0][2].should.be.a('function');
listen.callCount.should.equal(1);
listen.args[0][0].should.equal(3232);
listen.args[0][1].should.be.a('function');
done();
});
});
it('error from express', function(done) {
function TestExpressApp() {}
TestExpressApp.prototype.start = sinon.stub().callsArgWith(1, new Error('test'));
Expand Down Expand Up @@ -204,33 +165,6 @@ describe('Bitcore Node Service', function() {
done();
});
});
it('error from web socket', function(done) {
function TestExpressApp() {}
TestExpressApp.prototype.start = sinon.stub().callsArg(1);
function TestWSApp() {}
TestWSApp.prototype.start = sinon.stub().callsArgWith(2, new Error('test'));
var listen = sinon.stub().callsArg(1);
var TestService = proxyquire('../bitcorenode', {
'../lib/expressapp': TestExpressApp,
'../lib/wsapp': TestWSApp,
'http': {
Server: sinon.stub().returns({
listen: listen
})
}
});
var options = {
node: {
bwsPort: 3232
}
};
var service = new TestService(options);
var config = {};
service._startWalletService(config, function(err) {
err.message.should.equal('test');
done();
});
});
it('error from server.listen', function(done) {
var app = {};
function TestExpressApp() {
Expand Down

0 comments on commit 17fb1bc

Please sign in to comment.