embark-area-51/lib/core/services_monitor.js

54 lines
1.5 KiB
JavaScript
Raw Normal View History

var async = require('../utils/async_extend.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: ' + name);
this.checkList[name] = {fn: checkFn, interval: time || 5000};
};
2017-03-11 16:23:42 +00:00
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;
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);
});
2017-03-11 16:17:52 +00:00
callback();
}, function(err) {
2017-03-11 16:17:52 +00:00
if (err) {
self.logger.error("error running service check");
self.logger.error(err.message);
}
});
};
module.exports = ServicesMonitor;