fix: new jobs may be scheduled before the current time

This commit is contained in:
alikia2x (寒寒) 2025-03-24 23:43:54 +08:00
parent 314beb54b5
commit 6b7142a6d5
Signed by: alikia2x
GPG Key ID: 56209E0CCD8420C6

View File

@ -179,29 +179,31 @@ export async function adjustSnapshotTime(
): Promise<Date> { ): Promise<Date> {
const currentWindow = getCurrentWindowIndex(); const currentWindow = getCurrentWindowIndex();
// 计算目标窗口偏移量
const targetOffset = Math.floor((expectedStartTime.getTime() - Date.now()) / (5 * MINUTE)); const targetOffset = Math.floor((expectedStartTime.getTime() - Date.now()) / (5 * MINUTE));
// 在 Redis 中查找可用窗口
for (let i = 0; i < WINDOW_SIZE; i++) { for (let i = 0; i < WINDOW_SIZE; i++) {
const offset = (currentWindow + targetOffset + i) % WINDOW_SIZE; const offset = (currentWindow + targetOffset + i) % WINDOW_SIZE;
const count = await getWindowCount(redisClient, offset); const count = await getWindowCount(redisClient, offset);
if (count < allowedCounts) { if (count < allowedCounts) {
// 找到可用窗口,更新计数
await updateWindowCount(redisClient, offset, 1); await updateWindowCount(redisClient, offset, 1);
// 计算具体时间
const startPoint = new Date(); const startPoint = new Date();
startPoint.setHours(0, 0, 0, 0); startPoint.setHours(0, 0, 0, 0);
const startTime = startPoint.getTime(); const startTime = startPoint.getTime();
const windowStart = startTime + offset * 5 * MINUTE; const windowStart = startTime + offset * 5 * MINUTE;
const randomDelay = Math.floor(Math.random() * 5 * MINUTE); const randomDelay = Math.floor(Math.random() * 5 * MINUTE);
return new Date(windowStart + randomDelay); const delayedDate = new Date(windowStart + randomDelay);
const now = new Date();
if (delayedDate.getTime() < now.getTime()) {
return now;
}
return delayedDate;
} }
} }
// 如果没有找到可用窗口,返回原始时间
return expectedStartTime; return expectedStartTime;
} }