refactor services monitor to use events instead of passing addCheck around

This commit is contained in:
Iuri Matias 2018-06-01 11:58:11 -04:00
parent 0804779a20
commit 37d54e22db
9 changed files with 30 additions and 50 deletions

View File

@ -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) => {

View File

@ -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

View File

@ -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();

View File

@ -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){

View File

@ -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'){

View File

@ -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;

View File

@ -10,8 +10,9 @@ 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);
@ -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'});

View File

@ -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');

View File

@ -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();
}