if monitor is already running then init service

This commit is contained in:
Iuri Matias 2017-03-11 11:48:12 -05:00
parent 5cf287a747
commit 8d8ff671f7

View File

@ -6,25 +6,15 @@ 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.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');
ServicesMonitor.prototype.initCheck = function(checkName) {
var self = this;
var check = this.checkList[checkName];
if (!check) { return false; }
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);
@ -41,6 +31,32 @@ ServicesMonitor.prototype.startMonitor = function() {
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) {
clearInterval(this.checkTimers[name]);
delete this.checkTimers[name];
delete this.checkList[name];
delete this.checkState[name];
};
ServicesMonitor.prototype.startMonitor = function() {
var self = this;
this.working = true;
this.logger.trace('startMonitor');
async.eachObject(this.checkList, function(checkName, check, callback) {
self.initCheck(checkName);
callback();
}, function(err) {
if (err) {