embark/lib/core/services_monitor.js

91 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-03-29 17:50:05 +00:00
let async = require('../utils/async_extend.js');
2017-03-30 11:12:39 +00:00
class ServicesMonitor {
constructor(options) {
const self = this;
2017-03-30 11:12:39 +00:00
this.events = options.events;
this.logger = options.logger;
this.plugins = options.plugins;
2017-03-30 11:12:39 +00:00
this.checkList = {};
this.checkTimers = {};
this.checkState = {};
this.working = false;
2018-06-05 18:39:18 +00:00
self.events.setCommandHandler("services:register", (checkName, checkFn, time, initialStatus) => {
self.addCheck(checkName, checkFn, time, initialStatus);
});
2017-03-30 11:12:39 +00:00
}
}
2017-03-30 11:12:39 +00:00
ServicesMonitor.prototype.initCheck = function (checkName) {
2017-03-29 17:50:05 +00:00
let self = this;
let check = this.checkList[checkName];
2017-03-30 11:12:39 +00:00
if (!check) {
return false;
}
2017-03-30 11:12:39 +00:00
self.events.on('check:' + checkName, function (obj) {
if (check && check.status === 'off' && obj.status === 'on') {
self.events.emit('check:backOnline:' + checkName);
}
if (check && check.status === 'on' && obj.status === 'off') {
self.events.emit('check:wentOffline:' + checkName);
}
2018-10-17 22:36:46 +00:00
self.checkState[checkName] = {name: obj.name, status: obj.status, serviceName: checkName};
check.status = obj.status;
self.events.emit("servicesState", self.checkState);
});
if (check.interval !== 0) {
2017-03-30 11:12:39 +00:00
self.checkTimers[checkName] = setInterval(function () {
check.fn.call(check.fn, function (obj) {
self.events.emit('check:' + checkName, obj);
});
}, check.interval);
}
2017-03-30 11:12:39 +00:00
check.fn.call(check.fn, function (obj) {
self.events.emit('check:' + checkName, obj);
});
};
ServicesMonitor.prototype.addCheck = function (checkName, checkFn, time, initialState) {
this.logger.trace('add check: ' + checkName);
this.checkList[checkName] = {fn: checkFn, interval: time || 5000, status: initialState};
if (this.working) {
this.initCheck(checkName);
}
};
2017-03-30 11:12:39 +00:00
ServicesMonitor.prototype.stopCheck = function (name) {
2017-03-11 16:23:42 +00:00
clearInterval(this.checkTimers[name]);
delete this.checkTimers[name];
delete this.checkList[name];
delete this.checkState[name];
};
2017-03-30 11:12:39 +00:00
ServicesMonitor.prototype.startMonitor = function () {
2017-03-29 17:50:05 +00:00
let self = this;
this.working = true;
this.logger.trace('startMonitor');
let servicePlugins = this.plugins.getPluginsProperty('serviceChecks', 'serviceChecks');
servicePlugins.forEach(function (pluginCheck) {
self.addCheck(pluginCheck.checkName, pluginCheck.checkFn, pluginCheck.time);
});
2017-03-30 11:12:39 +00:00
async.eachObject(this.checkList, function (checkName, check, callback) {
self.initCheck(checkName);
2017-03-11 16:17:52 +00:00
callback();
2017-03-30 11:12:39 +00:00
}, function (err) {
2017-03-11 16:17:52 +00:00
if (err) {
2018-05-08 21:49:46 +00:00
self.logger.error(__("error running service check"));
2017-03-11 16:17:52 +00:00
self.logger.error(err.message);
}
});
};
module.exports = ServicesMonitor;