2015-12-03 22:00:35 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-04 00:24:39 +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 {
|
2019-10-02 22:02:13 +08:00
|
|
|
constructor () {
|
2019-10-01 16:52:31 +08:00
|
|
|
this.healthCheckBackend = new HealthCheckBackend(global.settings.disabled_file);
|
|
|
|
}
|
2015-12-03 22:00:35 +08:00
|
|
|
|
2019-10-02 22:02:13 +08:00
|
|
|
route (apiRouter) {
|
|
|
|
apiRouter.get('/health', healthCheck({ healthCheckBackend: this.healthCheckBackend }));
|
2019-10-01 16:52:31 +08:00
|
|
|
}
|
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) {
|
2019-12-24 01:19:08 +08:00
|
|
|
return res.status(200).send({ enabled: false, ok: true });
|
2019-10-01 16:52:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|