11import { Injectable , OnModuleInit } from '@nestjs/common' ;
22import { HttpService } from '@nestjs/axios' ;
33import { firstValueFrom } from 'rxjs' ;
4+ import { DiscoveryService } from '@nestjs/core' ;
45import { Cron } from '@nestjs/schedule' ;
6+ import { Reflector } from '@nestjs/core' ;
57
68@Injectable ( )
79export class AppService implements OnModuleInit {
8- constructor ( private readonly httpService : HttpService ) { }
10+ constructor (
11+ private readonly httpService : HttpService ,
12+ private readonly discoverService : DiscoveryService ,
13+ private readonly reflector : Reflector ,
14+ ) { }
915
10- onModuleInit ( ) {
16+ public onModuleInit ( ) {
1117 if ( process . env . NODE_ENV === 'production' ) {
1218 this . warmUpServer ( ) ;
1319 }
1420 }
1521
22+ // Cron을 이용한 주기적인 warm-up 호출
23+ @Cron ( '*/10 * * * *' )
24+ public handleCron ( ) {
25+ if ( process . env . NODE_ENV === 'production' ) {
26+ console . log ( 'Triggering server warm-up...' ) ;
27+ this . warmUpServer ( ) ; // warm-up 메서드를 호출
28+ }
29+ }
30+
1631 // 서버를 Warm-up하기 위한 메서드
17- async warmUpServer ( ) {
32+ private async warmUpServer ( ) {
1833 console . log ( 'Running warm-up tasks...' ) ;
1934
20- const requestBody = {
35+ const controllers = this . discoverService . getControllers ( ) ;
36+
37+ for ( const controller of controllers ) {
38+ const instance = controller . instance ;
39+ const prototype = Object . getPrototypeOf ( instance ) ;
40+
41+ // 컨트롤러의 모든 메소드를 순회
42+ const methods = Object . getOwnPropertyNames ( prototype ) . filter (
43+ ( method ) => method !== 'constructor' ,
44+ ) ;
45+
46+ for ( const method of methods ) {
47+ const path = this . reflector . get ( 'path' , instance [ method ] ) ;
48+ const httpMethod = this . reflector . get ( 'method' , instance [ method ] ) ;
49+
50+ if ( path && httpMethod ) {
51+ await this . callApi ( path , this . getDefaultRequestBody ( ) ) ;
52+ }
53+ }
54+ }
55+ }
56+
57+ private getDefaultRequestBody ( ) {
58+ return {
2159 userRequest : {
2260 user : {
2361 id : '74f7e7ab2bf19e63bb1ec845b760631259d7615440a2d3db7b344ac48ed1bbcde5' ,
@@ -27,42 +65,19 @@ export class AppService implements OnModuleInit {
2765 clientExtra : {
2866 sys_campus_id : '1' ,
2967 } ,
30- params : { } ,
31- detailParams : { } ,
3268 } ,
3369 } ;
34-
35- try {
36- await this . callApi ( '/api/node/user/get/campus' , requestBody ) ;
37- await this . callApi ( '/api/node/user/get/profile' , requestBody ) ;
38- await this . callApi ( '/api/node/clicker/get/campus' , requestBody ) ;
39-
40- console . log ( 'All API calls are completed for warm-up.' ) ;
41- } catch ( error ) {
42- console . error ( 'Error occurred during warm-up API calls:' , error ) ;
43- }
4470 }
4571
4672 // HTTP 요청을 보낼 함수
4773 private async callApi ( apiUrl : string , body : any ) {
48- const baseUrl = 'https://connectgnu.kro.kr' ;
74+ const baseUrl = 'https://connectgnu.kro.kr/ ' ;
4975
5076 try {
51- const response = await firstValueFrom (
52- this . httpService . post ( `${ baseUrl } ${ apiUrl } ` , body ) ,
53- ) ;
77+ await firstValueFrom ( this . httpService . post ( `${ baseUrl } ${ apiUrl } ` , body ) ) ;
5478 console . log ( `${ apiUrl } data fetched successfully` ) ;
5579 } catch ( error ) {
5680 console . error ( `${ apiUrl } failed:` , error ) ;
5781 }
5882 }
59-
60- // Cron을 이용한 주기적인 warm-up 호출
61- @Cron ( '*/10 * * * *' )
62- handleCron ( ) {
63- if ( process . env . NODE_ENV === 'production' ) {
64- console . log ( 'Triggering server warm-up...' ) ;
65- this . warmUpServer ( ) ; // warm-up 메서드를 호출
66- }
67- }
6883}
0 commit comments