2015-12-03 22:00:35 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var HealthCheck = require('../monitoring/health_check');
|
|
|
|
|
|
|
|
function HealthCheckController() {
|
|
|
|
this.healthCheck = new HealthCheck(global.settings.disabled_file);
|
|
|
|
}
|
|
|
|
|
2015-12-04 01:43:13 +08:00
|
|
|
HealthCheckController.prototype.route = function (app) {
|
2015-12-03 22:00:35 +08:00
|
|
|
app.get(global.settings.base_url + '/health', this.handleHealthCheck.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
HealthCheckController.prototype.handleHealthCheck = function (req, res) {
|
|
|
|
var healthConfig = global.settings.health || {};
|
|
|
|
if (!!healthConfig.enabled) {
|
|
|
|
var startTime = Date.now();
|
|
|
|
this.healthCheck.check(function(err) {
|
|
|
|
var ok = !err;
|
|
|
|
var response = {
|
|
|
|
enabled: true,
|
|
|
|
ok: ok,
|
|
|
|
elapsed: Date.now() - startTime
|
|
|
|
};
|
|
|
|
if (err) {
|
|
|
|
response.err = err.message;
|
|
|
|
}
|
2016-09-27 00:10:20 +08:00
|
|
|
res.status(ok ? 200 : 503).send(response);
|
2015-12-03 22:00:35 +08:00
|
|
|
|
|
|
|
});
|
|
|
|
} else {
|
2016-09-27 00:10:20 +08:00
|
|
|
res.status(200).send({enabled: false, ok: true});
|
2015-12-03 22:00:35 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = HealthCheckController;
|