11import { Controller , Get , Inject , Logger } from '@nestjs/common' ;
22import {
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' ;
1111import { ApiExcludeEndpoint } from '@nestjs/swagger' ;
1212import { PostgresService } from 'src/db/postgres.service' ;
@@ -24,6 +24,7 @@ import { Cacheable } from 'src/utils/cacheable';
2424export 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