This commit is contained in:
Ramnique Singh 2026-04-14 12:22:09 +05:30
parent 9ebee0cc67
commit c20c12ef64

View file

@ -15,42 +15,41 @@ export function isTrackScheduleDue(schedule: TrackSchedule, lastRunAt: string |
case 'cron': { case 'cron': {
if (!lastRunAt) return true; // Never ran — immediately due if (!lastRunAt) return true; // Never ran — immediately due
try { try {
// Find the MOST RECENT occurrence at-or-before `now`, not the
// occurrence right after lastRunAt. If lastRunAt is old, that
// occurrence would be ancient too and always fall outside the
// grace window, blocking every future fire.
const interval = CronExpressionParser.parse(schedule.expression, { const interval = CronExpressionParser.parse(schedule.expression, {
currentDate: new Date(lastRunAt), currentDate: now,
}); });
const nextRun = interval.next().toDate(); const prevRun = interval.prev().toDate();
return now >= nextRun && now.getTime() <= nextRun.getTime() + GRACE_MS;
// Already ran at-or-after this occurrence → skip.
if (new Date(lastRunAt).getTime() >= prevRun.getTime()) return false;
// Within grace → fire. Outside grace → missed, skip.
return now.getTime() <= prevRun.getTime() + GRACE_MS;
} catch { } catch {
return false; return false;
} }
} }
case 'window': { case 'window': {
if (!lastRunAt) { // Time-of-day filter (applies regardless of lastRunAt state).
// Never ran — due if within the time window now const [startHour, startMin] = schedule.startTime.split(':').map(Number);
const [startHour, startMin] = schedule.startTime.split(':').map(Number); const [endHour, endMin] = schedule.endTime.split(':').map(Number);
const [endHour, endMin] = schedule.endTime.split(':').map(Number); const startMinutes = startHour * 60 + startMin;
const startMinutes = startHour * 60 + startMin; const endMinutes = endHour * 60 + endMin;
const endMinutes = endHour * 60 + endMin; const nowMinutes = now.getHours() * 60 + now.getMinutes();
const nowMinutes = now.getHours() * 60 + now.getMinutes(); if (nowMinutes < startMinutes || nowMinutes > endMinutes) return false;
return nowMinutes >= startMinutes && nowMinutes <= endMinutes;
} if (!lastRunAt) return true;
try { try {
const interval = CronExpressionParser.parse(schedule.cron, { const interval = CronExpressionParser.parse(schedule.cron, {
currentDate: new Date(lastRunAt), currentDate: now,
}); });
const nextRun = interval.next().toDate(); const prevRun = interval.prev().toDate();
if (!(now >= nextRun && now.getTime() <= nextRun.getTime() + GRACE_MS)) { if (new Date(lastRunAt).getTime() >= prevRun.getTime()) return false;
return false; return now.getTime() <= prevRun.getTime() + GRACE_MS;
}
// Check if current time is within the time window
const [startHour, startMin] = schedule.startTime.split(':').map(Number);
const [endHour, endMin] = schedule.endTime.split(':').map(Number);
const startMinutes = startHour * 60 + startMin;
const endMinutes = endHour * 60 + endMin;
const nowMinutes = now.getHours() * 60 + now.getMinutes();
return nowMinutes >= startMinutes && nowMinutes <= endMinutes;
} catch { } catch {
return false; return false;
} }