11import type { OnModuleDestroy , OnModuleInit } from '@nestjs/common' ;
22import { Injectable , Logger } from '@nestjs/common' ;
3- import { lastValueFrom } from 'rxjs' ;
3+ import { catchError , lastValueFrom , timeout } from 'rxjs' ;
44
55import { WorkersGrpcClient } from '../../../../src/modules/grpc/immortal-grpc.client' ;
66import type { ServiceRegistryEntity } from '../entities/service-registry.entity' ;
@@ -75,14 +75,23 @@ export default class ServiceRegistryHealthCheckService implements OnModuleInit,
7575 }
7676
7777 private async performHealthCheck ( service : ServiceRegistryEntity ) : Promise < void > {
78+ const TIMEOUT_MS = 5000 ; // e.g. 5 seconds
79+
7880 try {
7981 this . logger . debug ( `Performing health check for service: ${ service . type } (ID: ${ service . _id } )...` ) ;
8082
8183 let isHealthy = false ;
8284
8385 this . immortalGrpcClient . setUrl ( service . url ) ;
8486
85- const res = await lastValueFrom ( this . immortalGrpcClient . serviceClient . status ( { } ) ) ;
87+ const res = await lastValueFrom (
88+ this . immortalGrpcClient . serviceClient . status ( { } ) . pipe (
89+ timeout ( TIMEOUT_MS ) ,
90+ catchError ( ( err ) => {
91+ throw new Error ( `Health check timed out or failed: ${ err . message } ` ) ;
92+ } ) ,
93+ ) ,
94+ ) ;
8695
8796 isHealthy = res . services . every ( ( s ) => s . status === Status . CONNECTED ) ;
8897 res . services . forEach ( ( s ) => console . log ( s . status ) ) ;
@@ -92,7 +101,6 @@ export default class ServiceRegistryHealthCheckService implements OnModuleInit,
92101 status : isHealthy ? ServiceStatus . ACTIVE : ServiceStatus . UN_HEALTHY ,
93102 } ) ;
94103
95- // TODO ::: send notification
96104 this . logger . log (
97105 `Health check result for service: ${ service . type } (ID: ${ service . _id } ): ${ isHealthy ? 'HEALTHY' : 'UNHEALTHY' } ` ,
98106 ) ;
0 commit comments