diff --git a/backend/src/index.ts b/backend/src/index.ts index 855d62b..8bc0f05 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -14,6 +14,7 @@ import { adminRoutes } from './routes/admin'; // Importing db triggers auto-migration on first load (B-07 fix) import { db } from './db/index'; import { seedAdminUser } from './db/seed-auto'; +import { startScheduler } from './sync/scheduler'; const app = new Hono(); @@ -60,6 +61,9 @@ seedAdminUser().catch((err) => { console.error('[Seed] Failed to seed admin user:', err); }); +// 启动自动同步(每天凌晨 2 点 + 启动时首次同步) +startScheduler(); + // Start server const port = config.PORT; console.info(`DevPerf Dashboard API starting on port ${port}`); diff --git a/backend/src/sync/scheduler.ts b/backend/src/sync/scheduler.ts index 13efcc8..45858d2 100644 --- a/backend/src/sync/scheduler.ts +++ b/backend/src/sync/scheduler.ts @@ -1,40 +1,25 @@ import { Cron } from 'croner'; -import { config } from '../config'; -import { syncPlane } from './sync-plane'; import { syncGitea } from './sync-gitea'; -let planeJob: Cron | null = null; let giteaJob: Cron | null = null; export function startScheduler(): void { - const planeInterval = config.SYNC_PLANE_INTERVAL; - const giteaInterval = config.SYNC_GITEA_INTERVAL; - - // Plane sync every N minutes - planeJob = new Cron(`*/${planeInterval} * * * *`, async () => { - console.info('[SCHEDULER] Starting Plane sync...'); - await syncPlane(); - }); - - // Gitea sync every N minutes - giteaJob = new Cron(`*/${giteaInterval} * * * *`, async () => { - console.info('[SCHEDULER] Starting Gitea sync...'); + // 每天凌晨 2 点 + 晚上 7 点各同步一次 + giteaJob = new Cron('0 2,19 * * *', async () => { + console.info('[SCHEDULER] Gitea 定时同步开始...'); await syncGitea(); }); - console.info(`[SCHEDULER] Plane sync scheduled every ${planeInterval} minutes`); - console.info(`[SCHEDULER] Gitea sync scheduled every ${giteaInterval} minutes`); + console.info('[SCHEDULER] Gitea 自动同步已启动(每天 02:00 + 19:00)'); - // Run initial sync after 5 seconds + // 启动后 10 秒执行一次首次同步 setTimeout(async () => { - console.info('[SCHEDULER] Running initial sync...'); - await syncPlane().catch(e => console.error('[SCHEDULER] Initial Plane sync failed:', e)); - await syncGitea().catch(e => console.error('[SCHEDULER] Initial Gitea sync failed:', e)); - }, 5000); + console.info('[SCHEDULER] 执行首次同步...'); + await syncGitea().catch(e => console.error('[SCHEDULER] 首次同步失败:', e)); + }, 10000); } export function stopScheduler(): void { - planeJob?.stop(); giteaJob?.stop(); - console.info('[SCHEDULER] Stopped all sync jobs'); + console.info('[SCHEDULER] 已停止同步任务'); }