From 37d54e22dbb58e488dedda8d32a6432228cc9823 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 1 Jun 2018 11:58:11 -0400 Subject: [PATCH] refactor services monitor to use events instead of passing addCheck around --- lib/contracts/blockchain.js | 3 +-- lib/core/engine.js | 28 ++++++---------------------- lib/core/services_monitor.js | 11 +++++++++++ lib/index.js | 2 +- lib/modules/ipfs/index.js | 9 ++------- lib/modules/specialconfigs/index.js | 1 - lib/modules/swarm/index.js | 19 +++++++------------ lib/modules/webserver/index.js | 4 +--- lib/modules/whisper/index.js | 3 +-- 9 files changed, 30 insertions(+), 50 deletions(-) diff --git a/lib/contracts/blockchain.js b/lib/contracts/blockchain.js index 999ae142..fee3a22f 100644 --- a/lib/contracts/blockchain.js +++ b/lib/contracts/blockchain.js @@ -18,7 +18,6 @@ class Blockchain { this.web3 = options.web3; this.locale = options.locale; this.isDev = options.isDev; - this.addCheck = options.addCheck; this.web3Endpoint = ''; this.isWeb3Ready = false; this.web3StartedInProcess = false; @@ -111,7 +110,7 @@ class Blockchain { const self = this; const NO_NODE = 'noNode'; - this.addCheck('Ethereum', function (cb) { + this.events.request("services:register", 'Ethereum', function (cb) { async.waterfall([ function checkNodeConnection(next) { self.assertNodeConnection(true, (err) => { diff --git a/lib/core/engine.js b/lib/core/engine.js index f1fb3342..6722c485 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -40,11 +40,6 @@ class Engine { this.config.loadConfigFiles({embarkConfig: this.embarkConfig, interceptLogs: this.interceptLogs}); this.plugins = this.config.plugins; - this.servicesMonitor = new ServicesMonitor({events: this.events, logger: this.logger}); - this.servicesMonitor.addCheck('embarkVersion', function (cb) { - return cb({name: 'Embark ' + self.version, status: 'on'}); - }, 0); - if (this.interceptLogs || this.interceptLogs === undefined) { this.doInterceptLogs(); } @@ -77,18 +72,11 @@ class Engine { } startMonitor() { - let self = this; - if (this.plugins) { - // -------- - // TODO: this only works for services done on startup - // -------- - let servicePlugins = this.plugins.getPluginsFor('serviceChecks'); - servicePlugins.forEach(function (plugin) { - plugin.serviceChecks.forEach(function (pluginCheck) { - self.servicesMonitor.addCheck(pluginCheck.checkName, pluginCheck.checkFn, pluginCheck.time); - }); - }); - } + const self = this; + this.servicesMonitor = new ServicesMonitor({events: this.events, logger: this.logger, plugins: this.plugins}); + this.servicesMonitor.addCheck('embarkVersion', function (cb) { + return cb({name: 'Embark ' + self.version, status: 'on'}); + }, 0); this.servicesMonitor.startMonitor(); } @@ -257,9 +245,7 @@ class Engine { } webServerService() { - this.registerModule('webserver', { - addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor) - }); + this.registerModule('webserver'); } storageService(_options) { @@ -281,7 +267,6 @@ class Engine { this.blockchain = new Blockchain({ contractsConfig: this.config.contractsConfig, blockchainConfig: this.config.blockchainConfig, - addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), events: this.events, logger: this.logger, isDev: this.isDev, @@ -290,7 +275,6 @@ class Engine { }); this.registerModule('whisper', { - addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor), // TODO: this should not be needed and should be deducted from the config instead // the eth provider is not necessary the same as the whisper one web3: this.blockchain.web3 diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index c1342212..7997859c 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -2,12 +2,18 @@ let async = require('../utils/async_extend.js'); class ServicesMonitor { constructor(options) { + const self = this; this.events = options.events; this.logger = options.logger; + this.plugins = options.plugins; this.checkList = {}; this.checkTimers = {}; this.checkState = {}; this.working = false; + + self.events.setCommandHandler("services:register", (checkName, checkFn, time) => { + self.addCheck(checkName, checkFn, time); + }); } } @@ -65,6 +71,11 @@ ServicesMonitor.prototype.startMonitor = function () { this.working = true; this.logger.trace('startMonitor'); + let servicePlugins = this.plugins.getPluginsProperty('serviceChecks', 'serviceChecks'); + servicePlugins.forEach(function (pluginCheck) { + self.addCheck(pluginCheck.checkName, pluginCheck.checkFn, pluginCheck.time); + }); + async.eachObject(this.checkList, function (checkName, check, callback) { self.initCheck(checkName); callback(); diff --git a/lib/index.js b/lib/index.js index 0a6af697..48ce9d63 100644 --- a/lib/index.js +++ b/lib/index.js @@ -300,6 +300,7 @@ class Embark { function startServices(callback) { + engine.startMonitor(); engine.startService("libraryManager"); engine.startService("codeRunner"); engine.startService("web3"); @@ -307,7 +308,6 @@ class Embark { engine.startService("deployment"); engine.startService("storage"); engine.startService("codeGenerator"); - engine.startMonitor(); callback(); }, function setupStoragePlugin(callback){ diff --git a/lib/modules/ipfs/index.js b/lib/modules/ipfs/index.js index be0eb7fe..bbeeecf5 100644 --- a/lib/modules/ipfs/index.js +++ b/lib/modules/ipfs/index.js @@ -10,11 +10,10 @@ class IPFS { this.logger = embark.logger; this.events = embark.events; this.buildDir = options.buildDir; - this.storageConfig = options.storageConfig; + this.storageConfig = embark.config.storageConfig; this.host = options.host || this.storageConfig.upload.host; this.port = options.port || this.storageConfig.upload.port; this.protocol = options.protocol || this.storageConfig.upload.protocol; - this.addCheck = options.addCheck; this.embark = embark; } @@ -48,11 +47,7 @@ class IPFS { self.logger.info(__('IPFS node is offline') + '..'); }); - if (!self.addCheck) { - return; - } - - self.addCheck('IPFS', function (cb) { + self.events.request("services:register", 'IPFS', function (cb) { self.logger.trace("Checking IPFS version..."); let url = (self.protocol || 'http') + '://' + self.host + ':' + self.port + '/api/v0/version'; if(self.protocol !== 'https'){ diff --git a/lib/modules/specialconfigs/index.js b/lib/modules/specialconfigs/index.js index b2b00a09..b829f77f 100644 --- a/lib/modules/specialconfigs/index.js +++ b/lib/modules/specialconfigs/index.js @@ -7,7 +7,6 @@ class SpecialConfigs { this.logger = embark.logger; this.events = embark.events; this.buildDir = options.buildDir; - this.addCheck = options.addCheck; this.embark = embark; this.contractsConfig = embark.config.contractsConfig; diff --git a/lib/modules/swarm/index.js b/lib/modules/swarm/index.js index 8d6b35f8..59b3b9d8 100644 --- a/lib/modules/swarm/index.js +++ b/lib/modules/swarm/index.js @@ -10,14 +10,15 @@ class Swarm { this.logger = embark.logger; this.events = embark.events; this.buildDir = options.buildDir; - this.storageConfig = options.storageConfig; - this.addCheck = options.addCheck; + this.storageConfig = embark.config.storageConfig; + this.host = options.host || this.storageConfig.host; + this.port = options.port || this.storageConfig.port; this.embark = embark; - + this.providerUrl = utils.buildUrl(options.protocol || options.storageConfig.upload.protocol, options.host || options.storageConfig.upload.host, options.port || options.storageConfig.upload.port); - + this.getUrl = options.storageConfig.upload.getUrl || this.providerUrl + '/bzz:/'; - + this.bzz = new Web3Bzz(this.providerUrl); } @@ -32,7 +33,6 @@ class Swarm { this.embark.registerUploadCommand('swarm', this.upload_swarm.deploy.bind(this.upload_swarm)); } - setServiceCheck() { let self = this; @@ -53,12 +53,7 @@ class Swarm { self.logger.info(__('Swarm node is offline...')); }); - if (!this.addCheck) { - return; - } - - // add check for console - this.addCheck('Swarm', function(cb){ + self.events.request("services:register", 'Swarm', function(cb){ self.logger.trace("Checking Swarm availability..."); self.bzz.isAvailable().then(result => { return cb({name: "Swarm ", status: result ? 'on':'off'}); diff --git a/lib/modules/webserver/index.js b/lib/modules/webserver/index.js index 63232cc3..a13fb1b7 100644 --- a/lib/modules/webserver/index.js +++ b/lib/modules/webserver/index.js @@ -7,7 +7,6 @@ class WebServer { this.embark = embark; this.logger = embark.logger; this.events = embark.events; - this.addCheck = options.addCheck; this.webServerConfig = embark.config.webServerConfig; if (!this.webServerConfig.enabled) { return; @@ -29,8 +28,7 @@ class WebServer { setServiceCheck() { let url = 'http://' + this.host + ':' + this.port; - //embark.registerServiceCheck('WebserverService', function (cb) { - this.addCheck('Webserver', function (cb) { + this.events.request("services:register", 'Webserver', function (cb) { utils.checkIsAvailable(url, function (available) { let devServer = __('Webserver') + ' (' + url + ')'; let serverStatus = (available ? 'on' : 'off'); diff --git a/lib/modules/whisper/index.js b/lib/modules/whisper/index.js index 53b80871..34552077 100644 --- a/lib/modules/whisper/index.js +++ b/lib/modules/whisper/index.js @@ -8,7 +8,6 @@ class Whisper { this.logger = embark.logger; this.events = embark.events; this.communicationConfig = embark.config.communicationConfig; - this.addCheck = options.addCheck; this.web3 = new Web3(); this.embark = embark; @@ -26,7 +25,7 @@ class Whisper { setServiceCheck() { const self = this; - self.addCheck('Whisper', function (cb) { + self.events.request("services:register", 'Whisper', function (cb) { if (!self.web3.currentProvider || self.web3.currentProvider.connection.readyState !== 1) { return self.connectToProvider(); }