Skip to content

Commit 5fd7ed7

Browse files
authored
Merge pull request #3 from GNU-connect/refactor-project
fix: WarmupService 로직 수정
2 parents d9af382 + 3d9d61f commit 5fd7ed7

File tree

2 files changed

+46
-30
lines changed

2 files changed

+46
-30
lines changed

src/modules/app/app.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ import { ReadingRoomsModule } from '../reading-rooms/reading-rooms.module';
1313
import { ScheduleModule } from '@nestjs/schedule';
1414
import { HttpModule } from '@nestjs/axios';
1515
import { AuthModule } from 'src/modules/auth/auth.module';
16-
16+
import { DiscoveryModule } from '@nestjs/core';
1717
@Module({
1818
imports: [
1919
SentryModule.forRoot(),
2020
ScheduleModule.forRoot(),
2121
HttpModule,
2222
SupabaseModule,
2323
UtilsModule,
24+
DiscoveryModule,
2425
CommonModule,
2526
UsersModule,
2627
ReadingRoomsModule,

src/modules/app/app.service.ts

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,61 @@
11
import { Injectable, OnModuleInit } from '@nestjs/common';
22
import { HttpService } from '@nestjs/axios';
33
import { firstValueFrom } from 'rxjs';
4+
import { DiscoveryService } from '@nestjs/core';
45
import { Cron } from '@nestjs/schedule';
6+
import { Reflector } from '@nestjs/core';
57

68
@Injectable()
79
export 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

Comments
 (0)