Skip to content

Commit 01553ae

Browse files
Return last failed healthcheck in healthcheck endpoint
1 parent b132ab2 commit 01553ae

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/controller/app/app.controller.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Controller, Get, Inject, Logger } from '@nestjs/common';
22
import {
3-
HealthCheckService,
4-
HttpHealthIndicator,
53
HealthCheck,
6-
TypeOrmHealthIndicator,
4+
HealthCheckResult,
5+
HealthCheckService,
76
HealthIndicator,
87
HealthIndicatorResult,
9-
HealthCheckResult,
8+
HttpHealthIndicator,
9+
TypeOrmHealthIndicator,
1010
} from '@nestjs/terminus';
1111
import { ApiExcludeEndpoint } from '@nestjs/swagger';
1212
import { PostgresService } from 'src/db/postgres.service';
@@ -24,6 +24,7 @@ import { Cacheable } from 'src/utils/cacheable';
2424
export class AppController extends HealthIndicator {
2525
private readonly logger = new Logger(AppController.name);
2626
private readonly appStartTime = new Date();
27+
private lastHealthcheckFailedTime: Date | null = null;
2728

2829
constructor(
2930
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
@@ -59,12 +60,18 @@ export class AppController extends HealthIndicator {
5960
@HealthCheck()
6061
@CacheTTL(1) // disable cache
6162
public async getHealth(): Promise<HealthCheckResult> {
62-
return this._getHealth();
63+
try {
64+
return await this._getHealth();
65+
} catch (err) {
66+
this.lastHealthcheckFailedTime = new Date();
67+
throw err;
68+
}
6369
}
6470

6571
private async _getHealthMetadata(): Promise<HealthIndicatorResult> {
6672
return this.getStatus('app', true, {
6773
appStartTime: this.appStartTime,
74+
lastHealthcheckFailedTime: this.lastHealthcheckFailedTime,
6875
});
6976
}
7077

0 commit comments

Comments
 (0)