-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add graceful shutdown timeout (#196)
# What ❔ Add graceful shutdown timeout. ## Why ❔ To ensure that we can gracefully process all incoming HTTP requests before shutting down the service. ## Checklist <!-- Check your PR fulfills the following items. --> <!-- For draft PRs check the boxes as you complete them. --> - [X] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [X] Tests for the changes have been added / updated.
- Loading branch information
1 parent
18925c4
commit 19b0099
Showing
13 changed files
with
128 additions
and
31 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,35 @@ | ||
import { Controller, Get } from "@nestjs/common"; | ||
import { Logger, Controller, Get, BeforeApplicationShutdown } from "@nestjs/common"; | ||
import { HealthCheckService, TypeOrmHealthIndicator, HealthCheck, HealthCheckResult } from "@nestjs/terminus"; | ||
import { ApiExcludeController } from "@nestjs/swagger"; | ||
import { ConfigService } from "@nestjs/config"; | ||
import { setTimeout } from "node:timers/promises"; | ||
|
||
@ApiExcludeController() | ||
@Controller(["health", "ready"]) | ||
export class HealthController { | ||
export class HealthController implements BeforeApplicationShutdown { | ||
private readonly logger: Logger; | ||
private readonly gracefulShutdownTimeoutMs: number; | ||
|
||
constructor( | ||
private readonly healthCheckService: HealthCheckService, | ||
private readonly dbHealthChecker: TypeOrmHealthIndicator | ||
) {} | ||
private readonly dbHealthChecker: TypeOrmHealthIndicator, | ||
configService: ConfigService | ||
) { | ||
this.logger = new Logger(HealthController.name); | ||
this.gracefulShutdownTimeoutMs = configService.get<number>("gracefulShutdownTimeoutMs"); | ||
} | ||
|
||
@Get() | ||
@HealthCheck() | ||
public async check(): Promise<HealthCheckResult> { | ||
return await this.healthCheckService.check([() => this.dbHealthChecker.pingCheck("database")]); | ||
} | ||
|
||
public async beforeApplicationShutdown(signal?: string): Promise<void> { | ||
if (this.gracefulShutdownTimeoutMs && signal === "SIGTERM") { | ||
this.logger.debug(`Awaiting ${this.gracefulShutdownTimeoutMs}ms before shutdown`); | ||
await setTimeout(this.gracefulShutdownTimeoutMs); | ||
this.logger.debug(`Timeout reached, shutting down now`); | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,39 @@ | ||
import { Logger, Controller, Get, OnApplicationShutdown } from "@nestjs/common"; | ||
import { Logger, Controller, Get, BeforeApplicationShutdown } from "@nestjs/common"; | ||
import { HealthCheckService, HealthCheck, HealthCheckResult } from "@nestjs/terminus"; | ||
import { ConfigService } from "@nestjs/config"; | ||
import { setTimeout } from "node:timers/promises"; | ||
import { JsonRpcHealthIndicator } from "./jsonRpcProvider.health"; | ||
|
||
@Controller(["health", "ready"]) | ||
export class HealthController implements OnApplicationShutdown { | ||
export class HealthController implements BeforeApplicationShutdown { | ||
private readonly logger: Logger; | ||
private readonly gracefulShutdownTimeoutMs: number; | ||
|
||
constructor( | ||
private readonly healthCheckService: HealthCheckService, | ||
private readonly jsonRpcHealthIndicator: JsonRpcHealthIndicator | ||
private readonly jsonRpcHealthIndicator: JsonRpcHealthIndicator, | ||
configService: ConfigService | ||
) { | ||
this.logger = new Logger(HealthController.name); | ||
this.gracefulShutdownTimeoutMs = configService.get<number>("gracefulShutdownTimeoutMs"); | ||
} | ||
|
||
@Get() | ||
@HealthCheck() | ||
public async check(): Promise<HealthCheckResult> { | ||
try { | ||
const healthCheckResult = await this.healthCheckService.check([ | ||
() => this.jsonRpcHealthIndicator.isHealthy("jsonRpcProvider"), | ||
]); | ||
this.logger.debug({ message: "Health check result", ...healthCheckResult }); | ||
return healthCheckResult; | ||
return await this.healthCheckService.check([() => this.jsonRpcHealthIndicator.isHealthy("jsonRpcProvider")]); | ||
} catch (error) { | ||
this.logger.error({ message: error.message, response: error.getResponse() }, error.stack); | ||
throw error; | ||
} | ||
} | ||
|
||
onApplicationShutdown(signal?: string): void { | ||
this.logger.debug({ message: "Received a signal", signal }); | ||
public async beforeApplicationShutdown(signal?: string): Promise<void> { | ||
if (this.gracefulShutdownTimeoutMs && signal === "SIGTERM") { | ||
this.logger.debug(`Awaiting ${this.gracefulShutdownTimeoutMs}ms before shutdown`); | ||
await setTimeout(this.gracefulShutdownTimeoutMs); | ||
this.logger.debug(`Timeout reached, shutting down now`); | ||
} | ||
} | ||
} |
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