From 71242465383c71f0fe3d4ca80b4d865fdeded5a9 Mon Sep 17 00:00:00 2001 From: mtvpls Date: Mon, 23 Feb 2026 21:36:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E5=86=B7?= =?UTF-8?q?=E5=8D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/cron/[password]/route.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/app/api/cron/[password]/route.ts b/src/app/api/cron/[password]/route.ts index 3885d50..70a21f0 100644 --- a/src/app/api/cron/[password]/route.ts +++ b/src/app/api/cron/[password]/route.ts @@ -14,6 +14,10 @@ import { SearchResult } from '@/lib/types'; export const runtime = 'nodejs'; +// 内存中记录最后执行时间(毫秒时间戳) +let lastExecutionTime = 0; +const COOLDOWN_MS = 10 * 60 * 1000; // 10分钟冷却时间 + export async function GET( request: NextRequest, { params }: { params: { password: string } } @@ -28,9 +32,32 @@ export async function GET( ); } + // 检查冷却时间 + const now = Date.now(); + const timeSinceLastExecution = now - lastExecutionTime; + + if (lastExecutionTime > 0 && timeSinceLastExecution < COOLDOWN_MS) { + const remainingSeconds = Math.ceil((COOLDOWN_MS - timeSinceLastExecution) / 1000); + const remainingMinutes = Math.floor(remainingSeconds / 60); + const seconds = remainingSeconds % 60; + + console.log(`Cron job skipped: cooldown period active. Remaining: ${remainingMinutes}m ${seconds}s`); + + return NextResponse.json({ + success: false, + message: 'Cron job is in cooldown period', + remainingSeconds, + nextAvailableTime: new Date(lastExecutionTime + COOLDOWN_MS).toISOString(), + timestamp: new Date().toISOString(), + }, { status: 429 }); + } + try { console.log('Cron job triggered:', new Date().toISOString()); + // 更新最后执行时间 + lastExecutionTime = now; + cronJob(); return NextResponse.json({