use eachObject to avoid async issues; support non-recurrent checks

This commit is contained in:
Iuri Matias 2017-03-11 07:32:16 -05:00
parent 173c571147
commit 4bf31328e2
2 changed files with 29 additions and 14 deletions

View File

@ -32,7 +32,7 @@ Engine.prototype.init = function(_options) {
this.servicesMonitor = new ServicesMonitor({events: this.events, logger: this.logger}); this.servicesMonitor = new ServicesMonitor({events: this.events, logger: this.logger});
this.servicesMonitor.addCheck('embarkVersion', function(cb) { this.servicesMonitor.addCheck('embarkVersion', function(cb) {
return cb({name: 'Embark ' + self.version, status: 'green'}); return cb({name: 'Embark ' + self.version, status: 'green'});
}); }, 0);
}; };
Engine.prototype.startMonitor = function() { Engine.prototype.startMonitor = function() {
@ -160,7 +160,6 @@ Engine.prototype.webServerService = function(options) {
Engine.prototype.ipfsService = function(options) { Engine.prototype.ipfsService = function(options) {
var self = this; var self = this;
self.servicesMonitor.addCheck('IPFS', function(cb) { self.servicesMonitor.addCheck('IPFS', function(cb) {
utils.checkIsAvailable('http://localhost:5001', function(available) { utils.checkIsAvailable('http://localhost:5001', function(available) {
if (available) { if (available) {
//Ideally this method should be in an IPFS API JSONRPC wrapper //Ideally this method should be in an IPFS API JSONRPC wrapper

View File

@ -3,6 +3,18 @@ var async = require('async');
var http = require('http'); var http = require('http');
var utils = require('./utils.js'); 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) { var ServicesMonitor = function(options) {
this.events = options.events; this.events = options.events;
this.logger = options.logger; this.logger = options.logger;
@ -12,34 +24,38 @@ var ServicesMonitor = function(options) {
}; };
ServicesMonitor.prototype.addCheck = function(name, checkFn, time) { 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 // TODO: check if a service with the same name already exists
this.checkList[name] = {fn: checkFn, interval: time || 5000}; this.checkList[name] = {fn: checkFn, interval: time || 5000};
}; };
ServicesMonitor.prototype.startMonitor = function() { ServicesMonitor.prototype.startMonitor = function() {
this.logger.info('startMonitor');
var self = this; 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.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.checkState[checkName] = obj.name[obj.status];
self.events.emit("servicesState", self.checkState); self.events.emit("servicesState", self.checkState);
}); });
this.checkTimers[checkName] = setInterval(function() { if (check.interval !== 0) {
check.fn.call(check.fn, function(obj) { self.checkTimers[checkName] = setInterval(function() {
self.events.emit('check:' + checkName, obj); check.fn.call(check.fn, function(obj) {
}); self.events.emit('check:' + checkName, obj);
}, check.interval); });
}, check.interval);
}
check.fn.call(check.fn, function(obj) { check.fn.call(check.fn, function(obj) {
self.events.emit('check:' + checkName, obj); self.events.emit('check:' + checkName, obj);
}); });
} }, function(err) {
});
this.logger.info(JSON.stringify(this.checkState));
}; };
// TODO: old checks to be moved // TODO: old checks to be moved