summaryrefslogtreecommitdiff
path: root/app/api/cron/forms/route.ts
diff options
context:
space:
mode:
authordujinkim <dujin.kim@dtsolution.co.kr>2025-04-28 02:13:30 +0000
committerdujinkim <dujin.kim@dtsolution.co.kr>2025-04-28 02:13:30 +0000
commitef4c533ebacc2cdc97e518f30e9a9350004fcdfb (patch)
tree345251a3ed0f4429716fa5edaa31024d8f4cb560 /app/api/cron/forms/route.ts
parent9ceed79cf32c896f8a998399bf1b296506b2cd4a (diff)
~20250428 작업사항
Diffstat (limited to 'app/api/cron/forms/route.ts')
-rw-r--r--app/api/cron/forms/route.ts57
1 files changed, 51 insertions, 6 deletions
diff --git a/app/api/cron/forms/route.ts b/app/api/cron/forms/route.ts
index f58c146b..abe6753a 100644
--- a/app/api/cron/forms/route.ts
+++ b/app/api/cron/forms/route.ts
@@ -1,20 +1,65 @@
-// src/app/api/cron/tag-form-mappings/route.ts
+// app/api/cron/forms/route.ts
import { syncTagFormMappings } from '@/lib/sedp/sync-form';
import { NextRequest } from 'next/server';
+import { revalidateTag } from 'next/cache';
+
+// TypeScript에서 global 객체를 확장하기 위한 type 선언
+declare global {
+ var pendingTasks: Set<Promise<any>>;
+}
+
+// 글로벌 태스크 집합 초기화 (서버가 시작될 때만 한 번 실행됨)
+if (!global.pendingTasks) {
+ global.pendingTasks = new Set<Promise<any>>();
+}
+
+// 이 함수는 비동기 작업을 더 안전하게 처리하기 위한 도우미 함수입니다
+function runBackgroundTask<T>(task: Promise<T>, taskName: string): Promise<T> {
+ // 작업을 추적 세트에 추가
+ global.pendingTasks.add(task);
+
+ // finally 블록을 사용하여 작업이 완료될 때 세트에서 제거
+ task
+ .then(result => {
+ console.log(`Background task '${taskName}' completed successfully`);
+ return result;
+ })
+ .catch(error => {
+ console.error(`Background task '${taskName}' failed:`, error);
+ })
+ .finally(() => {
+ global.pendingTasks.delete(task);
+ });
+
+ return task;
+}
export async function GET(request: NextRequest) {
try {
console.log('태그 폼 매핑 동기화 API 호출됨:', new Date().toISOString());
- // syncTagFormMappings 함수 호출
- const result = await syncTagFormMappings();
+ // 비동기 작업을 생성하고 전역 객체에 저장
+ const syncTask = syncTagFormMappings()
+ .then(result => {
+ // 작업이 완료되면 캐시 무효화
+ revalidateTag('form-lists');
+ return result;
+ });
+
+ // 백그라운드에서 작업이 계속 실행되도록 보장
+ runBackgroundTask(syncTask, 'form-sync');
- // 성공 시 결과와 함께 200 OK 반환
- return Response.json({ success: true, result }, { status: 200 });
+ // 먼저 상태를 반환하고, 그 동안 백그라운드에서 작업 계속
+ return new Response(
+ JSON.stringify({
+ success: true,
+ message: 'Form sync started in background. This may take a while.'
+ }),
+ { status: 202, headers: { 'Content-Type': 'application/json' } }
+ );
} catch (error: any) {
console.error('태그 폼 매핑 동기화 API 에러:', error);
- // 에러 시에는 message를 담아 500 반환
const message = error.message || 'Something went wrong';
return Response.json({ success: false, error: message }, { status: 500 });
}