2020-06-27 18:06:39 +02:00
|
|
|
import config from "config";
|
2021-05-03 19:29:22 +02:00
|
|
|
import {NextFunction, Request, Response, Router} from "express";
|
|
|
|
|
|
|
|
import ApplicationComponent from "../ApplicationComponent.js";
|
|
|
|
import {ServiceUnavailableHttpError} from "../HttpError.js";
|
2020-04-22 15:52:17 +02:00
|
|
|
|
2020-09-25 22:03:22 +02:00
|
|
|
export default class MaintenanceComponent extends ApplicationComponent {
|
2021-05-13 16:26:27 +02:00
|
|
|
public async initRoutes(router: Router): Promise<void> {
|
2020-04-22 15:52:17 +02:00
|
|
|
router.use((req: Request, res: Response, next: NextFunction) => {
|
|
|
|
if (res.headersSent) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2021-05-13 16:26:27 +02:00
|
|
|
if (!this.getApp().isReady()) {
|
2020-04-22 15:52:17 +02:00
|
|
|
res.header({'Retry-After': 60});
|
|
|
|
res.locals.refresh_after = 5;
|
2020-06-27 18:06:39 +02:00
|
|
|
throw new ServiceUnavailableHttpError(`${config.get('app.name')} is readying up. Please wait a few seconds...`);
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
|
2021-05-13 16:26:27 +02:00
|
|
|
for (const component of this.getApp().getComponents()) {
|
|
|
|
if (!component.isReady()) {
|
|
|
|
res.header({'Retry-After': 60});
|
|
|
|
res.locals.refresh_after = 30;
|
|
|
|
throw new ServiceUnavailableHttpError(`${config.get('app.name')} is unavailable due to failure of dependent services.`);
|
|
|
|
}
|
2020-04-22 15:52:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
2020-09-25 23:42:15 +02:00
|
|
|
}
|