From 173c5711475e6274915aa70f1147f62308e92755 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 10 Mar 2017 22:00:30 -0500 Subject: [PATCH 01/10] remove old services monitor; add new services monitor to engine; move previous checks to their modules inits --- lib/core/engine.js | 91 +++++++++++++++++++++------ lib/core/logger.js | 1 - lib/core/services.js | 119 ----------------------------------- lib/core/services_monitor.js | 84 +++++++++++++++++++++++++ lib/dashboard/dashboard.js | 1 - lib/dashboard/monitor.js | 10 ++- lib/index.js | 15 ++--- 7 files changed, 173 insertions(+), 148 deletions(-) delete mode 100644 lib/core/services.js create mode 100644 lib/core/services_monitor.js diff --git a/lib/core/engine.js b/lib/core/engine.js index ad248a7c6..8765d6d27 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -1,11 +1,13 @@ +var http = require('http'); +var utils = require('./utils.js'); + var Events = require('./events.js'); var Logger = require('./logger.js'); var Config = require('./config.js'); var DeployManager = require('../contracts/deploy_manager.js'); var ABIGenerator = require('../contracts/abi.js'); -var Dashboard = require('../dashboard/dashboard.js'); -var ServicesMonitor = require('./services.js'); +var ServicesMonitor = require('./services_monitor.js'); var Pipeline = require('../pipeline/pipeline.js'); var Server = require('../pipeline/server.js'); var Watch = require('../pipeline/watch.js'); @@ -19,24 +21,34 @@ var Engine = function(options) { }; Engine.prototype.init = function(_options) { + var self = this; var options = _options || {}; this.events = new Events(); this.logger = options.logger || new Logger({logLevel: 'debug'}); this.config = new Config({env: this.env, logger: this.logger, events: this.events}); 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: 'green'}); + }); +}; + +Engine.prototype.startMonitor = function() { + this.servicesMonitor.startMonitor(); }; Engine.prototype.startService = function(serviceName, _options) { var options = _options || {}; var services = { - "monitor": this.monitorService, "pipeline": this.pipelineService, "abi": this.abiService, "deployment": this.deploymentService, "fileWatcher": this.fileWatchService, - "webServer": this.webServerService + "webServer": this.webServerService, + "ipfs": this.ipfsService }; var service = services[serviceName]; @@ -50,18 +62,6 @@ Engine.prototype.startService = function(serviceName, _options) { return service.apply(this, [options]); }; -Engine.prototype.monitorService = function(options) { - var servicesMonitor = new ServicesMonitor({ - logger: this.logger, - config: this.config, - serverHost: options.serverHost, - serverPort: options.serverPort, - runWebserver: options.runWebserver, - version: this.version - }); - servicesMonitor.startMonitor(); -}; - Engine.prototype.pipelineService = function(options) { var self = this; this.logger.setStatus("Building Assets"); @@ -134,15 +134,68 @@ Engine.prototype.fileWatchService = function(options) { }; Engine.prototype.webServerService = function(options) { + var self = this; var webServerConfig = this.config.webServerConfig; if (!webServerConfig.enabled) { return; } + + var host = options.host || webServerConfig.host; + var port = options.port || webServerConfig.port; + this.logger.setStatus("Starting Server"); var server = new Server({ logger: this.logger, - host: options.host || webServerConfig.host, - port: options.port || webServerConfig.port + host: host, + port: port + }); + + self.servicesMonitor.addCheck('Webserver', function(cb) { + var devServer = 'Webserver (http://' + host + ':' + port + ')'; + return cb({name: devServer, status: 'green'}); + }); + + server.start(function(){ + }); +}; + +Engine.prototype.ipfsService = function(options) { + var self = this; + self.servicesMonitor.addCheck('IPFS', function(cb) { + + utils.checkIsAvailable('http://localhost:5001', function(available) { + if (available) { + //Ideally this method should be in an IPFS API JSONRPC wrapper + //The URL should also be flexible to accept non-default IPFS url + self.logger.trace("Checking IPFS version..."); + http.get('http://localhost:5001/api/v0/version', function(res) { + var body = ''; + res.on('data', function(d) { + body += d; + }); + res.on('end', function() { + try{ + var parsed = JSON.parse(body); + if(parsed.Version){ + return cb({name: ("IPFS " + parsed.Version), status: 'green'}); + } + else{ + return cb({name: "IPFS ", status: 'green'}); + } + } + catch (e){ + return cb({name: "IPFS ", status: 'red'}); + } + }); + res.on('error', function(err) { + self.logger.trace("Check IPFS version error: " + err); + return cb({name: "IPFS ", status: 'red'}); + }); + }); + } + else { + return cb({name: "IPFS ", status: 'red'}); + } + }); }); - server.start(function(){}); }; module.exports = Engine; diff --git a/lib/core/logger.js b/lib/core/logger.js index 5007977c5..442544a70 100644 --- a/lib/core/logger.js +++ b/lib/core/logger.js @@ -5,7 +5,6 @@ var Logger = function(options) { this.logLevel = options.logLevel || 'info'; this.logFunction = options.logFunction || console.log; this.contractsState = options.contractsState || function() {}; - this.availableServices = options.availableServices || function() {}; this.setStatus = options.setStatus || console.log; }; diff --git a/lib/core/services.js b/lib/core/services.js deleted file mode 100644 index 6d4cce518..000000000 --- a/lib/core/services.js +++ /dev/null @@ -1,119 +0,0 @@ -var Web3 = require('web3'); -var async = require('async'); -var http = require('http'); -var utils = require('./utils.js'); - -// TODO: needs a refactor and be done in a different way -var ServicesMonitor = function(options) { - this.logger = options.logger; - this.interval = options.interval || 5000; - this.config = options.config; - this.serverHost = options.serverHost || 'localhost'; - this.serverPort = options.serverPort || 8000; - this.runWebserver = options.runWebserver; - this.version = options.version; -}; - -ServicesMonitor.prototype.startMonitor = function() { - this.check(); - this.monitor = setInterval(this.check.bind(this), this.interval); -}; - -ServicesMonitor.prototype.stopMonitor = function() { - clearInterval(this.monitor); -}; - -ServicesMonitor.prototype.check = function() { - var self = this; - async.waterfall([ - function connectWeb3(callback) { - self.logger.trace('connectWeb3'); - var web3 = new Web3(); - var web3Endpoint = 'http://' + self.config.blockchainConfig.rpcHost + ':' + self.config.blockchainConfig.rpcPort; - web3.setProvider(new web3.providers.HttpProvider(web3Endpoint)); - callback(null, web3, []); - }, - function addEmbarkVersion(web3, result, callback) { - self.logger.trace('addEmbarkVersion'); - result.push(('Embark ' + self.version).green); - callback(null, web3, result); - }, - function checkEthereum(web3, result, callback) { - self.logger.trace('checkEthereum'); - var service; - if (web3.isConnected()) { - service = (web3.version.node.split("/")[0] + " " + web3.version.node.split("/")[1].split("-")[0] + " (Ethereum)").green; - } else { - service = "No Blockchain node found".red; - } - result.push(service); - callback(null, web3, result); - }, - function checkWhisper(web3, result, callback) { - self.logger.trace('checkWhisper'); - web3.version.getWhisper(function(err, res) { - var service = 'Whisper'; - result.push(err ? service.red : service.green); - callback(null, result); - }); - }, - function checkIPFS(result, callback) { - self.logger.trace('checkIPFS'); - - utils.checkIsAvailable('http://localhost:5001', function(available) { - if (available) { - //Ideally this method should be in an IPFS API JSONRPC wrapper - //The URL should also be flexible to accept non-default IPFS url - self.logger.trace("Checking IPFS version..."); - http.get('http://localhost:5001/api/v0/version', function(res) { - var body = ''; - res.on('data', function(d) { - body += d; - }); - res.on('end', function() { - try{ - var parsed = JSON.parse(body); - if(parsed.Version){ - result.push(("IPFS " + parsed.Version).green); - } - else{ - result.push("IPFS".green); - } - } - catch (e){ - result.push("IPFS".red); - } - callback(null, result); - }); - res.on('error', function(err) { - self.logger.trace("Check IPFS version error: " + err); - result.push("IPFS".red); - callback(null, result); - }); - }); - } - else { - result.push('IPFS'.red); - return callback(null, result); - } - }); - }, - function checkDevServer(result, callback) { - var host = self.serverHost || self.config.webServerConfig.host; - var port = self.serverPort || self.config.webServerConfig.port; - self.logger.trace('checkDevServer'); - var devServer = 'Webserver (http://' + host + ':' + port + ')'; - devServer = (self.runWebserver) ? devServer.green : devServer.red; - result.push(devServer); - callback(null, result); - } - ], function(err, result) { - if (err) { - self.logger.error(err.message); - } else { - self.logger.availableServices(result); - } - }); -}; - -module.exports = ServicesMonitor; diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js new file mode 100644 index 000000000..65673a8b3 --- /dev/null +++ b/lib/core/services_monitor.js @@ -0,0 +1,84 @@ +var Web3 = require('web3'); +var async = require('async'); +var http = require('http'); +var utils = require('./utils.js'); + +var ServicesMonitor = function(options) { + this.events = options.events; + this.logger = options.logger; + this.checkList = {}; + this.checkTimers = {}; + this.checkState = {}; +}; + +ServicesMonitor.prototype.addCheck = function(name, checkFn, time) { + this.logger.info('add check'); + // TODO: check if a service with the same name already exists + this.checkList[name] = {fn: checkFn, interval: time || 5000}; +}; + +ServicesMonitor.prototype.startMonitor = function() { + var self = this; + var checkName; + + for (checkName in this.checkList) { + var check = this.checkList[checkName]; + + self.events.on('check:' + checkName, function(obj) { + self.logger.info(JSON.stringify(obj)); + self.checkState[checkName] = obj.name[obj.status]; + self.events.emit("servicesState", self.checkState); + }); + + this.checkTimers[checkName] = setInterval(function() { + check.fn.call(check.fn, function(obj) { + self.events.emit('check:' + checkName, obj); + }); + }, check.interval); + + check.fn.call(check.fn, function(obj) { + self.events.emit('check:' + checkName, obj); + }); + } +}; + +// TODO: old checks to be moved +ServicesMonitor.prototype.check = function() { + var self = this; + async.waterfall([ + function connectWeb3(callback) { + self.logger.trace('connectWeb3'); + var web3 = new Web3(); + var web3Endpoint = 'http://' + self.config.blockchainConfig.rpcHost + ':' + self.config.blockchainConfig.rpcPort; + web3.setProvider(new web3.providers.HttpProvider(web3Endpoint)); + callback(null, web3, []); + }, + function checkEthereum(web3, result, callback) { + self.logger.trace('checkEthereum'); + var service; + if (web3.isConnected()) { + service = (web3.version.node.split("/")[0] + " " + web3.version.node.split("/")[1].split("-")[0] + " (Ethereum)").green; + } else { + service = "No Blockchain node found".red; + } + result.push(service); + callback(null, web3, result); + }, + function checkWhisper(web3, result, callback) { + self.logger.trace('checkWhisper'); + web3.version.getWhisper(function(err, res) { + var service = 'Whisper'; + result.push(err ? service.red : service.green); + callback(null, result); + }); + } + ], function(err, result) { + if (err) { + self.logger.error(err.message); + } else { + self.logger.availableServices(result); + } + }); +}; + +module.exports = ServicesMonitor; diff --git a/lib/dashboard/dashboard.js b/lib/dashboard/dashboard.js index d67b142d0..367b77b85 100644 --- a/lib/dashboard/dashboard.js +++ b/lib/dashboard/dashboard.js @@ -23,7 +23,6 @@ Dashboard.prototype.start = function(done) { monitor = new Monitor({env: self.env, console: console}); self.logger.logFunction = monitor.logEntry; self.logger.contractsState = monitor.setContracts; - self.logger.availableServices = monitor.availableServices; self.logger.setStatus = monitor.setStatus.bind(monitor); self.logger.info('========================'.bold.green); diff --git a/lib/dashboard/monitor.js b/lib/dashboard/monitor.js index 210fa7cec..c1ce4c8de 100644 --- a/lib/dashboard/monitor.js +++ b/lib/dashboard/monitor.js @@ -40,7 +40,15 @@ function Dashboard(options) { this.input.focus(); } -Dashboard.prototype.availableServices = function(services) { +Dashboard.prototype.availableServices = function(_services) { + var services = []; + var checkName; + for (checkName in _services) { + services.push(_services[checkName]); + } + + console.log(services); + this.progress.setContent(services.join('\n')); this.screen.render(); }; diff --git a/lib/index.js b/lib/index.js index 6b358d23b..926dc6e1d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -90,6 +90,12 @@ var Embark = { dashboard.console.runCode(abi); }); + engine.logger.info('dashboard start'); + engine.events.on('servicesState', function(servicesState) { + engine.logger.info('servicesState event'); + dashboard.monitor.availableServices(servicesState); + }); + callback(); }); }, @@ -99,16 +105,10 @@ var Embark = { engine.logger.info("loaded plugins: " + pluginList.join(", ")); } - if (options.useDashboard) { - engine.startService("monitor", { - serverHost: options.serverHost, - serverPort: options.serverPort, - runWebserver: options.runWebserver - }); - } engine.startService("pipeline"); engine.startService("abi"); engine.startService("deployment"); + engine.startService("ipfs"); engine.deployManager.deployContracts(function() { engine.startService("fileWatcher"); @@ -118,6 +118,7 @@ var Embark = { port: options.serverPort }); } + engine.startMonitor(); callback(); }); } From 4bf31328e2dfdb183b9adcef28349b66c57794c0 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 07:32:16 -0500 Subject: [PATCH 02/10] use eachObject to avoid async issues; support non-recurrent checks --- lib/core/engine.js | 3 +-- lib/core/services_monitor.js | 40 +++++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/lib/core/engine.js b/lib/core/engine.js index 8765d6d27..eee754a42 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -32,7 +32,7 @@ Engine.prototype.init = function(_options) { this.servicesMonitor = new ServicesMonitor({events: this.events, logger: this.logger}); this.servicesMonitor.addCheck('embarkVersion', function(cb) { return cb({name: 'Embark ' + self.version, status: 'green'}); - }); + }, 0); }; Engine.prototype.startMonitor = function() { @@ -160,7 +160,6 @@ Engine.prototype.webServerService = function(options) { Engine.prototype.ipfsService = function(options) { var self = this; self.servicesMonitor.addCheck('IPFS', function(cb) { - utils.checkIsAvailable('http://localhost:5001', function(available) { if (available) { //Ideally this method should be in an IPFS API JSONRPC wrapper diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index 65673a8b3..25e24c62f 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -3,6 +3,18 @@ var async = require('async'); var http = require('http'); var utils = require('./utils.js'); +// TODO: repeated, add this to an async extensions file +function asyncEachObject(object, iterator, callback) { + async.each( + Object.keys(object || {}), + function(key, next){ + iterator(key, object[key], next); + }, + callback + ); +} +async.eachObject = asyncEachObject; + var ServicesMonitor = function(options) { this.events = options.events; this.logger = options.logger; @@ -12,34 +24,38 @@ var ServicesMonitor = function(options) { }; ServicesMonitor.prototype.addCheck = function(name, checkFn, time) { - this.logger.info('add check'); + this.logger.info('add check: ' + name); // TODO: check if a service with the same name already exists this.checkList[name] = {fn: checkFn, interval: time || 5000}; }; ServicesMonitor.prototype.startMonitor = function() { + this.logger.info('startMonitor'); var self = this; - var checkName; - - for (checkName in this.checkList) { - var check = this.checkList[checkName]; + async.eachObject(this.checkList, function(checkName, check, callback) { self.events.on('check:' + checkName, function(obj) { - self.logger.info(JSON.stringify(obj)); + //self.logger.info('checked ' + checkName); + //self.logger.info(JSON.stringify(obj)); + //self.logger.info(JSON.stringify(self.checkState)); self.checkState[checkName] = obj.name[obj.status]; self.events.emit("servicesState", self.checkState); }); - this.checkTimers[checkName] = setInterval(function() { - check.fn.call(check.fn, function(obj) { - self.events.emit('check:' + checkName, obj); - }); - }, check.interval); + if (check.interval !== 0) { + self.checkTimers[checkName] = setInterval(function() { + check.fn.call(check.fn, function(obj) { + self.events.emit('check:' + checkName, obj); + }); + }, check.interval); + } check.fn.call(check.fn, function(obj) { self.events.emit('check:' + checkName, obj); }); - } + }, function(err) { + }); + this.logger.info(JSON.stringify(this.checkState)); }; // TODO: old checks to be moved From ea70d09e8c3be3b52fa394dce402727bdbf9e9b7 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 10:29:45 -0500 Subject: [PATCH 03/10] move ethereum and whisper checks; refactor web3 connection --- lib/contracts/deploy_manager.js | 32 ++++++++++++++------------ lib/core/engine.js | 35 +++++++++++++++++++++++++++-- lib/core/services_monitor.js | 40 --------------------------------- lib/dashboard/monitor.js | 2 -- lib/index.js | 2 +- 5 files changed, 52 insertions(+), 59 deletions(-) diff --git a/lib/contracts/deploy_manager.js b/lib/contracts/deploy_manager.js index 0293b97c2..04ce315f2 100644 --- a/lib/contracts/deploy_manager.js +++ b/lib/contracts/deploy_manager.js @@ -33,21 +33,25 @@ DeployManager.prototype.deployContracts = function(done) { }); contractsManager.build(callback); }, - function connectWithWeb3(contractsManager, callback) { - var web3; - if (self.web3) { - web3 = self.web3; - } else { - web3 = new Web3(); - var web3Endpoint = 'http://' + self.config.blockchainConfig.rpcHost + ':' + self.config.blockchainConfig.rpcPort; - web3.setProvider(new web3.providers.HttpProvider(web3Endpoint)); - if (!web3.isConnected()) { - self.logger.error(("Couldn't connect to " + web3Endpoint.underline + " are you sure it's on?").red); - self.logger.info("make sure you have an ethereum node or simulator running. e.g 'embark blockchain'".magenta); - return callback(Error("error connecting to blockchain node")); - } + function checkWeb3IsConnected(contractsManager, callback) { + if (!self.web3) { + return callback(Error("no web3 instance found")); + } + if (self.web3.currentProvider.isConnected !== undefined && !self.web3.isConnected()) { + self.logger.error(("Couldn't connect to an Ethereum node are you sure it's on?").red); + self.logger.info("make sure you have an Ethereum node or simulator running. e.g 'embark blockchain'".magenta); + return callback(Error("error connecting to blockchain node")); + } + if (self.web3.currentProvider.isConnected === undefined) { + self.web3.version.getNode(function(err, version) { + if (err) { + return callback(Error("error connecting to blockchain node")); + } + return callback(null, contractsManager, self.web3); + }); + } else { + return callback(null, contractsManager, self.web3); } - callback(null, contractsManager, web3); }, function setDefaultAccount(contractsManager, web3, callback) { web3.eth.getAccounts(function(err, accounts) { diff --git a/lib/core/engine.js b/lib/core/engine.js index eee754a42..3a821ea97 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -1,4 +1,5 @@ var http = require('http'); +var Web3 = require('web3'); var utils = require('./utils.js'); var Events = require('./events.js'); @@ -48,7 +49,8 @@ Engine.prototype.startService = function(serviceName, _options) { "deployment": this.deploymentService, "fileWatcher": this.fileWatchService, "webServer": this.webServerService, - "ipfs": this.ipfsService + "ipfs": this.ipfsService, + "web3": this.web3Service }; var service = services[serviceName]; @@ -111,7 +113,7 @@ Engine.prototype.abiService = function(options) { Engine.prototype.deploymentService = function(options) { var self = this; this.deployManager = new DeployManager({ - web3: options.web3, + web3: options.web3 || self.web3, trackContracts: options.trackContracts, config: this.config, logger: this.logger, @@ -197,4 +199,33 @@ Engine.prototype.ipfsService = function(options) { }); }; +Engine.prototype.web3Service = function(options) { + var self = this; + this.web3 = options.web3; + if (this.web3 === undefined) { + this.web3 = new Web3(); + var web3Endpoint = 'http://' + this.config.blockchainConfig.rpcHost + ':' + this.config.blockchainConfig.rpcPort; + this.web3.setProvider(new this.web3.providers.HttpProvider(web3Endpoint)); + } + + self.servicesMonitor.addCheck('Ethereum', function(cb) { + if (self.web3.isConnected()) { + return cb({name: (self.web3.version.node.split("/")[0] + " " + self.web3.version.node.split("/")[1].split("-")[0] + " (Ethereum)"), status: 'green'}); + } else { + return cb({name: "No Blockchain node found", status: 'red'}); + } + }); + + self.servicesMonitor.addCheck('Whisper', function(cb) { + self.web3.version.getWhisper(function(err, res) { + if (err) { + return cb({name: 'Whisper', status: 'red'}); + } else { + return cb({name: 'Whisper', status: 'green'}); + } + }); + }); +}; + + module.exports = Engine; diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index 25e24c62f..f0f61b769 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -55,46 +55,6 @@ ServicesMonitor.prototype.startMonitor = function() { }); }, function(err) { }); - this.logger.info(JSON.stringify(this.checkState)); -}; - -// TODO: old checks to be moved -ServicesMonitor.prototype.check = function() { - var self = this; - async.waterfall([ - function connectWeb3(callback) { - self.logger.trace('connectWeb3'); - var web3 = new Web3(); - var web3Endpoint = 'http://' + self.config.blockchainConfig.rpcHost + ':' + self.config.blockchainConfig.rpcPort; - web3.setProvider(new web3.providers.HttpProvider(web3Endpoint)); - callback(null, web3, []); - }, - function checkEthereum(web3, result, callback) { - self.logger.trace('checkEthereum'); - var service; - if (web3.isConnected()) { - service = (web3.version.node.split("/")[0] + " " + web3.version.node.split("/")[1].split("-")[0] + " (Ethereum)").green; - } else { - service = "No Blockchain node found".red; - } - result.push(service); - callback(null, web3, result); - }, - function checkWhisper(web3, result, callback) { - self.logger.trace('checkWhisper'); - web3.version.getWhisper(function(err, res) { - var service = 'Whisper'; - result.push(err ? service.red : service.green); - callback(null, result); - }); - } - ], function(err, result) { - if (err) { - self.logger.error(err.message); - } else { - self.logger.availableServices(result); - } - }); }; module.exports = ServicesMonitor; diff --git a/lib/dashboard/monitor.js b/lib/dashboard/monitor.js index c1ce4c8de..b1a243c48 100644 --- a/lib/dashboard/monitor.js +++ b/lib/dashboard/monitor.js @@ -47,8 +47,6 @@ Dashboard.prototype.availableServices = function(_services) { services.push(_services[checkName]); } - console.log(services); - this.progress.setContent(services.join('\n')); this.screen.render(); }; diff --git a/lib/index.js b/lib/index.js index 926dc6e1d..91b671af3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -92,7 +92,6 @@ var Embark = { engine.logger.info('dashboard start'); engine.events.on('servicesState', function(servicesState) { - engine.logger.info('servicesState event'); dashboard.monitor.availableServices(servicesState); }); @@ -105,6 +104,7 @@ var Embark = { engine.logger.info("loaded plugins: " + pluginList.join(", ")); } + engine.startService("web3"); engine.startService("pipeline"); engine.startService("abi"); engine.startService("deployment"); From c2c41549c17ae1ec35decd1e060dec81deecddb1 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 10:38:05 -0500 Subject: [PATCH 04/10] cleanup; remove unneded requires --- lib/contracts/deploy_manager.js | 1 - lib/core/services_monitor.js | 6 ------ lib/core/test.js | 8 -------- lib/index.js | 1 - 4 files changed, 16 deletions(-) diff --git a/lib/contracts/deploy_manager.js b/lib/contracts/deploy_manager.js index 04ce315f2..ebbe5d087 100644 --- a/lib/contracts/deploy_manager.js +++ b/lib/contracts/deploy_manager.js @@ -1,5 +1,4 @@ var async = require('async'); -var Web3 = require('web3'); var Deploy = require('./deploy.js'); var ContractsManager = require('./contracts.js'); diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index f0f61b769..402eb1e7d 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -1,7 +1,4 @@ -var Web3 = require('web3'); var async = require('async'); -var http = require('http'); -var utils = require('./utils.js'); // TODO: repeated, add this to an async extensions file function asyncEachObject(object, iterator, callback) { @@ -35,9 +32,6 @@ ServicesMonitor.prototype.startMonitor = function() { async.eachObject(this.checkList, function(checkName, check, callback) { self.events.on('check:' + checkName, function(obj) { - //self.logger.info('checked ' + checkName); - //self.logger.info(JSON.stringify(obj)); - //self.logger.info(JSON.stringify(self.checkState)); self.checkState[checkName] = obj.name[obj.status]; self.events.emit("servicesState", self.checkState); }); diff --git a/lib/core/test.js b/lib/core/test.js index ac1b5e376..fe3b04063 100644 --- a/lib/core/test.js +++ b/lib/core/test.js @@ -1,15 +1,7 @@ var async = require('async'); var Web3 = require('web3'); -var Embark = require('../index.js'); - var Engine = require('./engine.js'); - -var ABIGenerator = require('../contracts/abi.js'); -var ContractsManager = require('../contracts/contracts.js'); -var Deploy = require('../contracts/deploy.js'); - -var Config = require('./config.js'); var RunCode = require('./runCode.js'); var TestLogger = require('./test_logger.js'); diff --git a/lib/index.js b/lib/index.js index 91b671af3..6a71a03fb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,7 +2,6 @@ var async = require('async'); //require("./core/debug_util.js")(__filename, async); -var Web3 = require('web3'); var colors = require('colors'); var Engine = require('./core/engine.js'); From 92f9df411716b2bcd4ac253e3667e0eeae912787 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 10:52:02 -0500 Subject: [PATCH 05/10] remove async extension to a module --- lib/contracts/compiler.js | 13 +------------ lib/core/async_extend.js | 14 ++++++++++++++ lib/core/services_monitor.js | 14 +------------- 3 files changed, 16 insertions(+), 25 deletions(-) create mode 100644 lib/core/async_extend.js diff --git a/lib/contracts/compiler.js b/lib/contracts/compiler.js index f7a7c70ef..3d282266e 100644 --- a/lib/contracts/compiler.js +++ b/lib/contracts/compiler.js @@ -1,18 +1,7 @@ /*jshint esversion: 6, loopfunc: true */ -var async = require('async'); +var async = require('../core/async_extend.js'); var SolcW = require('./solcW.js'); -function asyncEachObject(object, iterator, callback) { - async.each( - Object.keys(object || {}), - function(key, next){ - iterator(key, object[key], next); - }, - callback - ); -} -async.eachObject = asyncEachObject; - var Compiler = function(options) { this.plugins = options.plugins; this.logger = options.logger; diff --git a/lib/core/async_extend.js b/lib/core/async_extend.js new file mode 100644 index 000000000..1c6db4f5c --- /dev/null +++ b/lib/core/async_extend.js @@ -0,0 +1,14 @@ +var async = require('async'); + +function asyncEachObject(object, iterator, callback) { + async.each( + Object.keys(object || {}), + function(key, next){ + iterator(key, object[key], next); + }, + callback + ); +} +async.eachObject = asyncEachObject; + +module.exports = async; diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index 402eb1e7d..0dd119198 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -1,16 +1,4 @@ -var async = require('async'); - -// TODO: repeated, add this to an async extensions file -function asyncEachObject(object, iterator, callback) { - async.each( - Object.keys(object || {}), - function(key, next){ - iterator(key, object[key], next); - }, - callback - ); -} -async.eachObject = asyncEachObject; +var async = require('./async_extend.js'); var ServicesMonitor = function(options) { this.events = options.events; From c514765d0bceb68a3d07c9389f14d0715d36d614 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 11:03:20 -0500 Subject: [PATCH 06/10] move misc utils modules to their own folder --- lib/cmds/template_generator.js | 2 +- lib/contracts/compiler.js | 2 +- lib/contracts/solcW.js | 2 +- lib/core/config.js | 2 +- lib/core/engine.js | 2 +- lib/core/fs.js | 2 +- lib/core/plugin.js | 2 +- lib/core/plugins.js | 2 +- lib/core/services_monitor.js | 2 +- lib/dashboard/console.js | 2 +- lib/index.js | 2 +- lib/{core => utils}/async_extend.js | 0 lib/{core => utils}/debug_util.js | 0 lib/{core => utils}/utils.js | 0 14 files changed, 11 insertions(+), 11 deletions(-) rename lib/{core => utils}/async_extend.js (100%) rename lib/{core => utils}/debug_util.js (100%) rename lib/{core => utils}/utils.js (100%) diff --git a/lib/cmds/template_generator.js b/lib/cmds/template_generator.js index a0aa482f9..42c632882 100644 --- a/lib/cmds/template_generator.js +++ b/lib/cmds/template_generator.js @@ -1,5 +1,5 @@ var fs = require('../core/fs.js'); -var utils = require('../core/utils.js'); +var utils = require('../utils/utils.js'); var TemplateGenerator = function(templateName) { this.templateName = templateName; diff --git a/lib/contracts/compiler.js b/lib/contracts/compiler.js index 3d282266e..3c2582944 100644 --- a/lib/contracts/compiler.js +++ b/lib/contracts/compiler.js @@ -1,5 +1,5 @@ /*jshint esversion: 6, loopfunc: true */ -var async = require('../core/async_extend.js'); +var async = require('../utils/async_extend.js'); var SolcW = require('./solcW.js'); var Compiler = function(options) { diff --git a/lib/contracts/solcW.js b/lib/contracts/solcW.js index eec22b10e..e9a0f9ada 100644 --- a/lib/contracts/solcW.js +++ b/lib/contracts/solcW.js @@ -1,4 +1,4 @@ -var utils = require('../core/utils.js'); +var utils = require('../utils/utils.js'); var solcProcess; var compilerLoaded = false; diff --git a/lib/core/config.js b/lib/core/config.js index 3db31a9a9..c87b6761e 100644 --- a/lib/core/config.js +++ b/lib/core/config.js @@ -1,6 +1,6 @@ var fs = require('./fs.js'); var Plugins = require('./plugins.js'); -var utils = require('./utils.js'); +var utils = require('../utils/utils.js'); // TODO: add wrapper for fs so it can also work in the browser // can work with both read and save diff --git a/lib/core/engine.js b/lib/core/engine.js index 3a821ea97..6122bf8bf 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -1,6 +1,6 @@ var http = require('http'); var Web3 = require('web3'); -var utils = require('./utils.js'); +var utils = require('../utils/utils.js'); var Events = require('./events.js'); var Logger = require('./logger.js'); diff --git a/lib/core/fs.js b/lib/core/fs.js index dbbf74808..a88816e4d 100644 --- a/lib/core/fs.js +++ b/lib/core/fs.js @@ -1,5 +1,5 @@ var fs = require('fs-extra'); -var utils = require('./utils.js'); +var utils = require('../utils/utils.js'); function mkdirpSync() { return fs.mkdirpSync.apply(fs.mkdirpSync, arguments); diff --git a/lib/core/plugin.js b/lib/core/plugin.js index 32765ea3e..9d6ff37ac 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -1,6 +1,6 @@ /*jshint esversion: 6, loopfunc: true */ var fs = require('./fs.js'); -var utils = require('./utils.js'); +var utils = require('../utils/utils.js'); // TODO: pass other params like blockchainConfig, contract files, etc.. var Plugin = function(options) { diff --git a/lib/core/plugins.js b/lib/core/plugins.js index 4132a2803..c3319183a 100644 --- a/lib/core/plugins.js +++ b/lib/core/plugins.js @@ -1,5 +1,5 @@ var Plugin = require('./plugin.js'); -var utils = require('./utils.js'); +var utils = require('../utils/utils.js'); var Plugins = function(options) { this.pluginList = options.plugins || []; diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index 0dd119198..4049e3201 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -1,4 +1,4 @@ -var async = require('./async_extend.js'); +var async = require('../utils/async_extend.js'); var ServicesMonitor = function(options) { this.events = options.events; diff --git a/lib/dashboard/console.js b/lib/dashboard/console.js index c510f204f..51ed85f88 100644 --- a/lib/dashboard/console.js +++ b/lib/dashboard/console.js @@ -1,4 +1,4 @@ -var utils = require('../core/utils.js'); +var utils = require('../utils/utils.js'); var RunCode = require('../core/runCode.js'); var Console = function(options) { diff --git a/lib/index.js b/lib/index.js index 6a71a03fb..fc0c360e1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,6 @@ /*jshint esversion: 6 */ var async = require('async'); -//require("./core/debug_util.js")(__filename, async); +//require("./utils/debug_util.js")(__filename, async); var colors = require('colors'); diff --git a/lib/core/async_extend.js b/lib/utils/async_extend.js similarity index 100% rename from lib/core/async_extend.js rename to lib/utils/async_extend.js diff --git a/lib/core/debug_util.js b/lib/utils/debug_util.js similarity index 100% rename from lib/core/debug_util.js rename to lib/utils/debug_util.js diff --git a/lib/core/utils.js b/lib/utils/utils.js similarity index 100% rename from lib/core/utils.js rename to lib/utils/utils.js From 2dec088a9b66f595620e3ed167264c257c492613 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 11:17:52 -0500 Subject: [PATCH 07/10] add service check error --- lib/core/services_monitor.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index 4049e3201..2177d5f2c 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -35,7 +35,12 @@ ServicesMonitor.prototype.startMonitor = function() { check.fn.call(check.fn, function(obj) { self.events.emit('check:' + checkName, obj); }); + callback(); }, function(err) { + if (err) { + self.logger.error("error running service check"); + self.logger.error(err.message); + } }); }; From 5cf287a747ed5b22ceb7ac09af9b8c9a54de025e Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 11:23:42 -0500 Subject: [PATCH 08/10] implement stopCheck --- lib/core/services_monitor.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index 2177d5f2c..04bdddeab 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -10,10 +10,16 @@ var ServicesMonitor = function(options) { ServicesMonitor.prototype.addCheck = function(name, checkFn, time) { this.logger.info('add check: ' + name); - // TODO: check if a service with the same name already exists this.checkList[name] = {fn: checkFn, interval: time || 5000}; }; +ServicesMonitor.prototype.stopCheck = function(name) { + clearInterval(this.checkTimers[name]); + delete this.checkTimers[name]; + delete this.checkList[name]; + delete this.checkState[name]; +}; + ServicesMonitor.prototype.startMonitor = function() { this.logger.info('startMonitor'); var self = this; From 8d8ff671f7727d69a2bfa8150595f44f56046a50 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 11:48:12 -0500 Subject: [PATCH 09/10] if monitor is already running then init service --- lib/core/services_monitor.js | 56 +++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index 04bdddeab..04febab61 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -6,11 +6,41 @@ var ServicesMonitor = function(options) { this.checkList = {}; this.checkTimers = {}; this.checkState = {}; + this.working = false; }; -ServicesMonitor.prototype.addCheck = function(name, checkFn, time) { - this.logger.info('add check: ' + name); - this.checkList[name] = {fn: checkFn, interval: time || 5000}; +ServicesMonitor.prototype.initCheck = function(checkName) { + var self = this; + var check = this.checkList[checkName]; + + if (!check) { return false; } + + self.events.on('check:' + checkName, function(obj) { + self.checkState[checkName] = obj.name[obj.status]; + self.events.emit("servicesState", self.checkState); + }); + + if (check.interval !== 0) { + self.checkTimers[checkName] = setInterval(function() { + check.fn.call(check.fn, function(obj) { + self.events.emit('check:' + checkName, obj); + }); + }, check.interval); + } + + check.fn.call(check.fn, function(obj) { + self.events.emit('check:' + checkName, obj); + }); +}; + +ServicesMonitor.prototype.addCheck = function(checkName, checkFn, time) { + var self = this; + this.logger.trace('add check: ' + checkName); + this.checkList[checkName] = {fn: checkFn, interval: time || 5000}; + + if (this.working) { + this.initCheck(checkName); + } }; ServicesMonitor.prototype.stopCheck = function(name) { @@ -21,26 +51,12 @@ ServicesMonitor.prototype.stopCheck = function(name) { }; ServicesMonitor.prototype.startMonitor = function() { - this.logger.info('startMonitor'); var self = this; + this.working = true; + this.logger.trace('startMonitor'); async.eachObject(this.checkList, function(checkName, check, callback) { - self.events.on('check:' + checkName, function(obj) { - self.checkState[checkName] = obj.name[obj.status]; - self.events.emit("servicesState", self.checkState); - }); - - if (check.interval !== 0) { - self.checkTimers[checkName] = setInterval(function() { - check.fn.call(check.fn, function(obj) { - self.events.emit('check:' + checkName, obj); - }); - }, check.interval); - } - - check.fn.call(check.fn, function(obj) { - self.events.emit('check:' + checkName, obj); - }); + self.initCheck(checkName); callback(); }, function(err) { if (err) { From 54420b327e2ef5b5c5d3e26445e6446032f822f7 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 12:27:10 -0500 Subject: [PATCH 10/10] detect when ethereum node comes back online and redeploy --- lib/core/services_monitor.js | 11 +++++++++++ lib/index.js | 10 +++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index 04febab61..2685c5c2e 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -1,5 +1,8 @@ var async = require('../utils/async_extend.js'); +// TODO: need to separate colors from states +// i.e use status: /on|off|warn/ not /red|green/ +// it's up to the logger or console to determine the color var ServicesMonitor = function(options) { this.events = options.events; this.logger = options.logger; @@ -16,7 +19,15 @@ ServicesMonitor.prototype.initCheck = function(checkName) { if (!check) { return false; } self.events.on('check:' + checkName, function(obj) { + // TODO: see todo above + if (check && check.status === 'red' && obj.status === 'green') { + self.events.emit('check:backOnline:' + checkName); + } + if (check && check.status === 'green' && obj.status === 'red') { + self.events.emit('check:wentOffline:' + checkName); + } self.checkState[checkName] = obj.name[obj.status]; + check.status = obj.status; self.events.emit("servicesState", self.checkState); }); diff --git a/lib/index.js b/lib/index.js index fc0c360e1..4ed7ad389 100644 --- a/lib/index.js +++ b/lib/index.js @@ -103,12 +103,21 @@ var Embark = { engine.logger.info("loaded plugins: " + pluginList.join(", ")); } + engine.startMonitor(); engine.startService("web3"); engine.startService("pipeline"); engine.startService("abi"); engine.startService("deployment"); engine.startService("ipfs"); + engine.events.on('check:backOnline:Ethereum', function() { + engine.logger.info('Ethereum node detected..'); + engine.config.reloadConfig(); + engine.deployManager.deployContracts(function() { + engine.logger.info('Deployment Done'); + }); + }); + engine.deployManager.deployContracts(function() { engine.startService("fileWatcher"); if (options.runWebserver) { @@ -117,7 +126,6 @@ var Embark = { port: options.serverPort }); } - engine.startMonitor(); callback(); }); }