1- import { Controller , Get , Logger } from '@nestjs/common' ;
1+ import { Controller , Get , Inject , Logger } from '@nestjs/common' ;
22import {
33 HealthCheckService ,
44 HttpHealthIndicator ,
55 HealthCheck ,
66 TypeOrmHealthIndicator ,
77 HealthIndicator ,
88 HealthIndicatorResult ,
9+ HealthCheckResult ,
910} from '@nestjs/terminus' ;
1011import { ApiExcludeEndpoint } from '@nestjs/swagger' ;
1112import { PostgresService } from 'src/db/postgres.service' ;
@@ -16,15 +17,17 @@ import { ConfigService } from '@nestjs/config';
1617import { AllocatorReportGeneratorJobService } from 'src/jobs/allocator-report-generator-job/allocator-report-generator-job.service' ;
1718import { IpniAdvertisementFetcherJobService } from 'src/jobs/ipni-advertisement-fetcher-job/ipni-advertisement-fetcher-job.service' ;
1819import { LocationService } from 'src/service/location/location.service' ;
19- import { CacheTTL } from '@nestjs/cache-manager' ;
20+ import { Cache , CACHE_MANAGER , CacheTTL } from '@nestjs/cache-manager' ;
2021import { GitHubTriggersHandlerService } from 'src/service/github-triggers-handler-service/github-triggers-handler.service' ;
22+ import { Cacheable } from 'src/utils/cacheable' ;
2123
2224@Controller ( )
2325export class AppController extends HealthIndicator {
2426 private readonly logger = new Logger ( AppController . name ) ;
2527 private readonly appStartTime = new Date ( ) ;
2628
2729 constructor (
30+ @Inject ( CACHE_MANAGER ) private readonly cacheManager : Cache ,
2831 private readonly healthCheckService : HealthCheckService ,
2932 private readonly httpHealthIndicator : HttpHealthIndicator ,
3033 private readonly typeOrmHealthIndicator : TypeOrmHealthIndicator ,
@@ -50,57 +53,57 @@ export class AppController extends HealthIndicator {
5053 @Get ( '/debug' )
5154 @CacheTTL ( 1 ) // disable cache
5255 @ApiExcludeEndpoint ( )
53- async getDebug ( ) {
56+ public async getDebug ( ) {
5457 return 'debug' ;
5558 }
5659
57- private async _getHealth ( ) : Promise < HealthIndicatorResult > {
60+ @Get ( 'health' )
61+ @HealthCheck ( )
62+ @CacheTTL ( 1 ) // disable cache
63+ public async getHealth ( ) : Promise < HealthCheckResult > {
64+ return this . _getHealth ( ) ;
65+ }
66+
67+ private async _getHealthMetadata ( ) : Promise < HealthIndicatorResult > {
5868 return this . getStatus ( 'app' , true , {
5969 appStartTime : this . appStartTime ,
6070 } ) ;
6171 }
6272
63- @Get ( 'health' )
64- @HealthCheck ( )
65- @CacheTTL ( 1000 * 10 ) // 10 seconds
66- public async getHealth ( ) {
67- this . logger . debug ( 'Running healthcheck' ) ;
73+ // cache http ping checks for better performance
74+ @Cacheable ( { ttl : 1000 * 60 * 10 } ) // 10 minutes
75+ private async _httpPingCheck (
76+ name : string ,
77+ url : string ,
78+ ) : Promise < HealthIndicatorResult > {
79+ return this . httpHealthIndicator . pingCheck ( name , url ) ;
80+ }
81+
82+ // dedicated cache for ipinfo.io because of token limits
83+ @Cacheable ( { ttl : 1000 * 60 * 60 } ) // 1 hour
84+ private async _httpPingCheckIpInfo ( ) : Promise < HealthIndicatorResult > {
85+ return await this . httpHealthIndicator . pingCheck (
86+ 'ipinfo.io' ,
87+ `https://ipinfo.io/8.8.8.8?token=${ this . configService . get < string > ( 'IP_INFO_TOKEN' ) } ` ,
88+ ) ;
89+ }
6890
91+ @Cacheable ( { ttl : 1000 * 10 } ) // 10 seconds
92+ private async _getHealth ( ) : Promise < HealthCheckResult > {
93+ // prettier-ignore
6994 return this . healthCheckService . check ( [
70- ( ) => this . _getHealth ( ) ,
71- ( ) => this . locationService . getHealth ( ) ,
72- ( ) =>
73- this . httpHealthIndicator . pingCheck (
74- 'cid.contact' ,
75- 'https://cid.contact/health' ,
76- ) ,
77- ( ) =>
78- this . httpHealthIndicator . pingCheck (
79- 'glif-api' ,
80- `${ this . configService . get < string > ( 'GLIF_API_BASE_URL' ) } /v1` ,
81- ) ,
82- ( ) =>
83- this . httpHealthIndicator . pingCheck (
84- 'allocator-tech-api' ,
85- `${ this . configService . get < string > ( 'ALLOCATOR_TECH_BASE_URL' ) } /health` ,
86- ) ,
87- ( ) =>
88- this . httpHealthIndicator . pingCheck (
89- 'api.datacapstats.io' ,
90- 'https://api.datacapstats.io/api/health' ,
91- ) ,
92- ( ) =>
93- this . httpHealthIndicator . pingCheck (
94- 'stats.filspark.com' ,
95- 'https://stats.filspark.com' ,
96- ) ,
97- ( ) =>
98- this . typeOrmHealthIndicator . pingCheck ( 'database' , {
95+ ( ) => this . _getHealthMetadata ( ) ,
96+ ( ) => this . _httpPingCheckIpInfo ( ) ,
97+ ( ) => this . _httpPingCheck ( 'cid.contact' , 'https://cid.contact/health' ) ,
98+ ( ) => this . _httpPingCheck ( 'glif-api' , `${ this . configService . get < string > ( 'GLIF_API_BASE_URL' ) } /v1` ) ,
99+ ( ) => this . _httpPingCheck ( 'allocator-tech-api' , `${ this . configService . get < string > ( 'ALLOCATOR_TECH_BASE_URL' ) } /health` ) ,
100+ ( ) => this . _httpPingCheck ( 'api.datacapstats.io' , 'https://api.datacapstats.io/api/health' ) ,
101+ ( ) => this . _httpPingCheck ( 'stats.filspark.com' , 'https://stats.filspark.com' ) ,
102+ ( ) => this . typeOrmHealthIndicator . pingCheck ( 'database' , {
99103 connection : this . postgresService . pool ,
100104 timeout : 2000 ,
101105 } ) ,
102- ( ) =>
103- this . typeOrmHealthIndicator . pingCheck ( 'database-dmob' , {
106+ ( ) => this . typeOrmHealthIndicator . pingCheck ( 'database-dmob' , {
104107 connection : this . postgresDmobService . pool ,
105108 timeout : 2000 ,
106109 } ) ,
0 commit comments