Open History to see the last commit
1import { MIN_SCHEDULED_TASK_INTERVAL_MS, type ScheduledTaskSchedule } from "@t3tools/contracts";
2import * as DateTime from "effect/DateTime";
3import * as Option from "effect/Option";
4
5const MINUTE_MS = 60_000;
6
7export function parseTimeOfDay(value: string): { hour: number; minute: number } | null {
8 const match = /^([01]?\d|2[0-3]):([0-5]\d)$/.exec(value.trim());
9 if (!match) return null;
10 return { hour: Number(match[1]), minute: Number(match[2]) };
11}
12
13function effectiveIntervalMs(everyMs: number): number {
14 return Math.max(everyMs, MIN_SCHEDULED_TASK_INTERVAL_MS);
15}
16
17export function nextScheduledRunAt(
18 schedule: ScheduledTaskSchedule,
19 from: DateTime.DateTime,
20): DateTime.DateTime | null {
21 if (schedule.type === "webhook") return null;
22 if (schedule.type === "once") {
23 const runAt = DateTime.make(schedule.runAt);
24 return Option.isSome(runAt) &&
25 DateTime.toEpochMillis(runAt.value) > DateTime.toEpochMillis(from)
26 ? runAt.value
27 : null;
28 }
29 if (schedule.type === "interval") {
30 // Intervals advance elapsed time, including across daylight-saving changes.
31 return DateTime.addDuration(from, effectiveIntervalMs(schedule.everyMs));
32 }
33
34 const time = parseTimeOfDay(schedule.timeOfDay);
35 if (time === null) return null;
36 const weekdays =
37 schedule.weekdays && schedule.weekdays.length > 0 ? new Set(schedule.weekdays) : null;
38 for (let offset = 0; offset <= 7; offset += 1) {
39 // Resolve the wall time afresh: keeping the offset of `from` would pick the
40 // repeated fall-back hour once `from` is in it, scheduling the slot twice.
41 const candidate = DateTime.mutate(DateTime.add(from, { days: offset }), (date) =>
42 date.setUTCHours(time.hour, time.minute, 0, 0),
43 );
44 if (DateTime.toEpochMillis(candidate) <= DateTime.toEpochMillis(from)) continue;
45 if (weekdays !== null && !weekdays.has(DateTime.toParts(candidate).weekDay)) continue;
46 return candidate;
47 }
48 return null;
49}
50
51function weekdayKey(weekdays: ReadonlyArray<number> | undefined): string {
52 const unique = [...new Set(weekdays ?? [])].toSorted((x, y) => x - y);
53 if (unique.length === 0 || unique.length === 7) return "daily";
54 return unique.join(",");
55}
56
57export function isSameSchedule(a: ScheduledTaskSchedule, b: ScheduledTaskSchedule): boolean {
58 if (a.type === "interval") {
59 return (
60 b.type === "interval" && effectiveIntervalMs(a.everyMs) === effectiveIntervalMs(b.everyMs)
61 );
62 }
63 if (a.type === "webhook") return b.type === "webhook";
64 if (a.type === "once") return b.type === "once" && Date.parse(a.runAt) === Date.parse(b.runAt);
65 if (b.type !== "fixed_time") return false;
66 // Compare parsed times so padded-hour edits do not reschedule pending runs.
67 const aTime = parseTimeOfDay(a.timeOfDay);
68 const bTime = parseTimeOfDay(b.timeOfDay);
69 return (
70 aTime !== null &&
71 bTime !== null &&
72 aTime.hour === bTime.hour &&
73 aTime.minute === bTime.minute &&
74 weekdayKey(a.weekdays) === weekdayKey(b.weekdays)
75 );
76}
77
78const MISSED_FIXED_TIME_GRACE_MS = 10 * MINUTE_MS;
79
80export function isMissedFixedTimeRun(
81 schedule: ScheduledTaskSchedule,
82 dueAt: DateTime.DateTime,
83 now: DateTime.DateTime,
84): boolean {
85 if (schedule.type !== "fixed_time") return false;
86 return DateTime.toEpochMillis(now) - DateTime.toEpochMillis(dueAt) > MISSED_FIXED_TIME_GRACE_MS;
87}