Skip to content

Commit

Permalink
Implement Graceful rest application shutting down.
Browse files Browse the repository at this point in the history
  • Loading branch information
miltdev committed Jun 21, 2023
1 parent b472565 commit e626419
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export interface IRestAppModuleConfig extends IAppModuleConfig {
allowMethods?: string[],
allowHeaders?: string[],
},
gracefulEnabled?: boolean,
}
7 changes: 7 additions & 0 deletions src/infrastructure/applications/rest/RestApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ export class RestApplication extends BaseApplication {
this._app.use(urlencoded({ extended: true, limit: this._config.requestSizeLimit }));
}

protected initGraceful() {
if (this._config.gracefulEnabled) {
this._app.enableShutdownHooks();
}
}

public async init() {
await super.init();

Expand All @@ -137,6 +143,7 @@ export class RestApplication extends BaseApplication {
this.initSentry();
this.initInterceptors();
this.initSettings();
this.initGraceful();
}

public async start() {
Expand Down
8 changes: 7 additions & 1 deletion src/infrastructure/applications/rest/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import baseConfig from '../base/config';
import {IRestAppModuleConfig} from './IRestAppModuleConfig';
import {ValidationExceptionFilterCustom} from './filters/ValidationExceptionFilterCustom';
import {RequestExecutionExceptionFilter} from './filters/RequestExecutionExceptionFilter';
import {GracefulController} from "./graceful/GracefulController";
import {GracefulService} from "./graceful/GracefulService";

export default {
...baseConfig,
Expand All @@ -17,6 +19,10 @@ export default {
provide: APP_FILTER,
useClass: RequestExecutionExceptionFilter,
},
],
config.gracefulEnabled && GracefulService,
].filter(Boolean),
controllers: [
config.gracefulEnabled && GracefulController,
].filter(Boolean),
}),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {Controller, Get, InternalServerErrorException} from "@nestjs/common";
import {GracefulService} from "./GracefulService";

@Controller()
export class GracefulController {
constructor(
public service: GracefulService
) { }

@Get('health')
checkHealth() {
if (this.service.isShutdown) {
throw new InternalServerErrorException('Application is currently shutting down.');
}

return 'ok';
}
}
18 changes: 18 additions & 0 deletions src/infrastructure/applications/rest/graceful/GracefulService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {BeforeApplicationShutdown} from "@nestjs/common";

export class GracefulService implements BeforeApplicationShutdown {
public isShutdown = false;

async beforeApplicationShutdown(signal?: string) {
this.isShutdown = true;

if (process.env.K8S_READINESS_FAILURE_THRESHOLD && process.env.K8S_READINESS_PERIOD_SECONDS) {
const failureThreshold = parseInt(process.env.K8S_READINESS_FAILURE_THRESHOLD, 10);
const periodSeconds = parseInt(process.env.K8S_READINESS_PERIOD_SECONDS, 10);

await new Promise((resolve) => {
setTimeout(resolve, failureThreshold * periodSeconds * 1000);
});
}
}
}

0 comments on commit e626419

Please sign in to comment.