forked from steroids/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Graceful rest application shutting down.
- Loading branch information
Showing
5 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/infrastructure/applications/rest/graceful/GracefulController.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
src/infrastructure/applications/rest/graceful/GracefulService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} | ||
} |