2015-12-03 22:00:35 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-01 16:52:31 +08:00
|
|
|
const HealthCheckBackend = require('../monitoring/health_check');
|
2015-12-03 22:00:35 +08:00
|
|
|
|
2019-10-01 16:52:31 +08:00
|
|
|
module.exports = class HealthCheckController {
|
|
|
|
constructor () {
|
|
|
|
this.healthCheckBackend = new HealthCheckBackend(global.settings.disabled_file);
|
|
|
|
}
|
2015-12-03 22:00:35 +08:00
|
|
|
|
2019-10-01 16:52:31 +08:00
|
|
|
route (app) {
|
|
|
|
app.get(`${global.settings.base_url}/health`, healthCheck({ healthCheckBackend: this.healthCheckBackend }));
|
|
|
|
}
|
2015-12-03 22:00:35 +08:00
|
|
|
};
|
|
|
|
|
2019-10-01 16:52:31 +08:00
|
|
|
function healthCheck ({ healthCheckBackend }) {
|
|
|
|
return function healthCheckMiddleware (req, res) {
|
|
|
|
const healthConfig = global.settings.health || {};
|
|
|
|
|
|
|
|
if (!healthConfig.enabled) {
|
|
|
|
return res.status(200).send({enabled: false, ok: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
const startTime = Date.now();
|
|
|
|
|
|
|
|
healthCheckBackend.check((err) => {
|
|
|
|
const ok = !err;
|
|
|
|
const response = {
|
2015-12-03 22:00:35 +08:00
|
|
|
enabled: true,
|
2019-10-01 16:52:31 +08:00
|
|
|
ok,
|
2015-12-03 22:00:35 +08:00
|
|
|
elapsed: Date.now() - startTime
|
|
|
|
};
|
2019-10-01 16:52:31 +08:00
|
|
|
|
2015-12-03 22:00:35 +08:00
|
|
|
if (err) {
|
|
|
|
response.err = err.message;
|
|
|
|
}
|
|
|
|
|
2019-10-01 16:52:31 +08:00
|
|
|
res.status(ok ? 200 : 503).send(response);
|
2015-12-03 22:00:35 +08:00
|
|
|
});
|
2019-10-01 16:52:31 +08:00
|
|
|
};
|
|
|
|
}
|