From 4bf31328e2dfdb183b9adcef28349b66c57794c0 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 11 Mar 2017 07:32:16 -0500 Subject: [PATCH] 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