From b84c1dc178480963ed8ccb9b6be4084efd1ef6cf Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Tue, 21 Apr 2015 14:43:35 -0300 Subject: [PATCH] async initialize --- bws.js | 35 ++++++++++++++++++++--------------- lib/expressapp.js | 9 ++++++--- lib/server.js | 37 +++++++++++++++++++++++++++++++++---- lib/storage.js | 12 +++++++++++- test/integration/server.js | 27 ++++++++------------------- 5 files changed, 78 insertions(+), 42 deletions(-) diff --git a/bws.js b/bws.js index b0e5247..3b166fb 100755 --- a/bws.js +++ b/bws.js @@ -28,30 +28,35 @@ if (config.https) { serverOpts.cert = fs.readFileSync(config.certificateFile || './ssl/certificate.pem'); } -var start = function() { +var start = function(cb) { var server; if (config.cluster) { server = sticky(clusterInstances, function() { - var app = ExpressApp.start(config); - var server = config.https ? serverModule.createServer(serverOpts, app) : + ExpressApp.start(config, function(err, app) { + var server = config.https ? serverModule.createServer(serverOpts, app) : + serverModule.Server(app); + WsApp.start(server, config); + return server; + }); + }); + return cb(server); + } else { + ExpressApp.start(config, function(err, app) { + server = config.https ? serverModule.createServer(serverOpts, app) : serverModule.Server(app); WsApp.start(server, config); - return server; + return cb(server); }); - } else { - var app = ExpressApp.start(config); - server = config.https ? serverModule.createServer(serverOpts, app) : - serverModule.Server(app); - WsApp.start(server, config); - } - server.listen(port, function(err) { - if (err) console.log('ERROR: ', err); - log.info('Bitcore Wallet Service running on port ' + port); - }); + }; }; if (config.cluster && !config.lockOpts.lockerServer) throw 'When running in cluster mode, locker server need to be configured'; -start(); +start(function(server) { + server.listen(port, function(err) { + if (err) console.log('ERROR: ', err); + log.info('Bitcore Wallet Service running on port ' + port); + }); +}); diff --git a/lib/expressapp.js b/lib/expressapp.js index 3e6bbe4..318387e 100644 --- a/lib/expressapp.js +++ b/lib/expressapp.js @@ -22,11 +22,11 @@ var ExpressApp = function() {}; * @param opts.WalletService options for WalletService class * @param opts.basePath * @param opts.disableLogs + * @param {Callback} cb */ -ExpressApp.start = function(opts) { +ExpressApp.start = function(opts, cb) { opts = opts || {}; - WalletService.initialize(opts); var app = express(); app.use(function(req, res, next) { res.setHeader('Access-Control-Allow-Origin', '*'); @@ -316,7 +316,10 @@ ExpressApp.start = function(opts) { app.use(opts.basePath || '/bws/api', router); - return app; + + WalletService.initialize(opts, function(err) { + return cb(err,app); + }); }; module.exports = ExpressApp; diff --git a/lib/server.js b/lib/server.js index 2d5a8c6..71caba6 100644 --- a/lib/server.js +++ b/lib/server.js @@ -53,15 +53,44 @@ WalletService.onNotification = function(func) { * @param {Object} opts * @param {Storage} [opts.storage] - The storage provider. * @param {Storage} [opts.blockchainExplorer] - The blockchainExporer provider. + * @param {Callback} cb */ -WalletService.initialize = function(opts) { +WalletService.initialize = function(opts, cb) { + $.shouldBeFunction(cb); + + opts = opts || {}; lock = opts.lock || new Lock(opts.lockOpts); - // TODO: This method needs to be async - storage = opts.storage || new Storage().connect(opts.storageOpts, function() {}); blockchainExplorer = opts.blockchainExplorer; blockchainExplorerOpts = opts.blockchainExplorerOpts; - initialized = true; + + if (initialized) + return cb(); + + if (opts.storage) { + storage = opts.storage; + initialized = true; + return cb(); + } else { + var newStorage = new Storage(); + newStorage.connect(opts.storageOpts, function(err) { + if (err) return cb(err); + storage = newStorage; + initialized = true; + return cb(); + }); + } +}; + + +WalletService.shutDown = function(cb) { + if (initialized) { + storage.disconnect(function(err) { + if (err) return cb(err); + initialized = false; + return cb(); + }); + } }; WalletService.getInstance = function() { diff --git a/lib/storage.js b/lib/storage.js index c9c5180..350fb9a 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -29,7 +29,7 @@ Storage.prototype.connect = function(opts, cb) { opts = opts || {}; - if (this.db) return cb(); + if (this.db) return cb(null); var config = opts.mongoDb || {}; var url = 'mongodb://' + (config.host || 'localhost') + ':' + (config.port ||  27017) + '/bws'; @@ -40,6 +40,16 @@ Storage.prototype.connect = function(opts, cb) { } self.db = db; console.log('Connection established to ', url); + return cb(null); + }); +}; + + +Storage.prototype.disconnect = function(cb) { + var self = this; + this.db.close(true, function(err) { + if (err) return cb(err); + self.db = null; return cb(); }); }; diff --git a/test/integration/server.js b/test/integration/server.js index b51e49e..086970a 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -227,15 +227,6 @@ function resetDb(cb) { }); }; -function closeDb(cb) { - if (!db) return cb(); - db.close(true, function(err) { - should.not.exist(err); - db = null; - return cb(); - }); -}; - describe('Wallet service', function() { before(function(done) { @@ -249,21 +240,20 @@ describe('Wallet service', function() { beforeEach(function(done) { resetDb(function() { blockchainExplorer = sinon.stub(); - WalletService.initialize({ storage: storage, blockchainExplorer: blockchainExplorer, + }, function() { + helpers.offset = 0; + done(); }); - helpers.offset = 0; - done(); }); }); after(function(done) { - closeDb(done); + WalletService.shutDown(done); }); describe('#getInstanceWithAuth', function() { - beforeEach(function() {}); it('should get server instance for existing copayer', function(done) { @@ -3066,21 +3056,20 @@ describe('Blockchain monitor', function() { resetDb(function() { blockchainExplorer = sinon.stub(); - WalletService.initialize({ storage: storage, blockchainExplorer: blockchainExplorer, + }, function() { + helpers.offset = 0; + done(); }); - helpers.offset = 0; - - done(); }); }); afterEach(function() { BlockchainMonitor.prototype._getAddressSubscriber.restore(); }); after(function(done) { - closeDb(done); + WalletService.shutDown(done); }); it('should subscribe wallet', function(done) {