diff options
Diffstat (limited to 'lib/nonsap-sync/purchase-group-code/user-code-sync-scheduler.ts')
| -rw-r--r-- | lib/nonsap-sync/purchase-group-code/user-code-sync-scheduler.ts | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/nonsap-sync/purchase-group-code/user-code-sync-scheduler.ts b/lib/nonsap-sync/purchase-group-code/user-code-sync-scheduler.ts new file mode 100644 index 00000000..23d0b568 --- /dev/null +++ b/lib/nonsap-sync/purchase-group-code/user-code-sync-scheduler.ts @@ -0,0 +1,46 @@ +'use server'; + +import * as cron from 'node-cron'; +import { syncUserCodes } from './purchase-group-code-sync'; + +// CRON 스케줄 (기본: 매일 새벽 3시) +const CRON_STRING = process.env.USER_CODE_SYNC_CRON || '0 3 * * *'; + +// 애플리케이션 기동 시 최초 한 번 실행 여부 +const DO_FIRST_RUN = process.env.USER_CODE_SYNC_FIRST_RUN === 'true'; + +/** + * 사용자 코드 동기화 스케줄러 시작 + */ +export async function startUserCodeSyncScheduler(): Promise<void> { + console.log('[USER-CODE-SYNC] 사용자 코드 동기화 스케줄러 초기화 중...'); + + try { + // 환경 변수에 따라 실행시 즉시 실행 여부 결정 + if (DO_FIRST_RUN) { + console.log('[USER-CODE-SYNC] 첫 실행: 사용자 코드 동기화 즉시 시작'); + syncUserCodes().catch((error) => { + console.error('[USER-CODE-SYNC] 첫 실행 중 오류:', error); + }); + } + + // CRON JOB 등록 + cron.schedule(CRON_STRING, async () => { + try { + console.log('[USER-CODE-SYNC] CRON 실행: 사용자 코드 동기화 시작'); + await syncUserCodes(); + } catch (error) { + console.error('[USER-CODE-SYNC] 예약된 동기화 실패:', error); + // 동기화 실패해도 다음 스케줄은 계속 실행 + } + }, { + timezone: 'Asia/Seoul' + }); + + console.log(`[USER-CODE-SYNC] ✅ 사용자 코드 동기화 CRON 작업 등록 완료 (${CRON_STRING})`); + + } catch (error) { + console.error('[USER-CODE-SYNC] 스케줄러 초기화 실패:', error); + throw error; + } +} |
