diff --git a/lib/core/services_monitor.js b/lib/core/services_monitor.js index 04bdddea..04febab6 100644 --- a/lib/core/services_monitor.js +++ b/lib/core/services_monitor.js @@ -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) {