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,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) {