Open History to see the last commit
1import { MIN_SCHEDULED_TASK_INTERVAL_MS, type ScheduledTaskSchedule } from "@t3tools/contracts";
2import * as DateTime from "effect/DateTime";
3
4const MINUTE_MS = 60_000;
5
6export function parseTimeOfDay(value: string): { hour: number; minute: number } | null {
7 const match = /^([01]?\d|2[0-3]):([0-5]\d)$/.exec(value.trim());
8 if (!match) return null;
9 return { hour: Number(match[1]), minute: Number(match[2]) };
10}
11
12export function nextScheduledRunAt(
13 schedule: ScheduledTaskSchedule,
14 from: DateTime.DateTime,
15): DateTime.DateTime | null {
16 if (schedule.type === "webhook") return null;
17 if (schedule.type === "interval") {
18 // Persisted rows created before the one-minute floor remain readable, but
19 // they must not retain their old high-frequency execution rate.
20 // Intervals advance elapsed time, including across daylight-saving changes.
21 return DateTime.addDuration(from, Math.max(schedule.everyMs, MIN_SCHEDULED_TASK_INTERVAL_MS));
22 }
23
24 const time = parseTimeOfDay(schedule.timeOfDay);
25 if (time === null) return null;
26 const weekdays =
27 schedule.weekdays && schedule.weekdays.length > 0 ? new Set(schedule.weekdays) : null;
28 for (let offset = 0; offset <= 7; offset += 1) {
29 const candidate = DateTime.setParts(DateTime.add(from, { days: offset }), {
30 hour: time.hour,
31 minute: time.minute,
32 second: 0,
33 millisecond: 0,
34 });
35 if (DateTime.toEpochMillis(candidate) <= DateTime.toEpochMillis(from)) continue;
36 if (weekdays !== null && !weekdays.has(DateTime.toParts(candidate).weekDay)) continue;
37 return candidate;
38 }
39 return null;
40}
41
42function weekdayKey(weekdays: ReadonlyArray<number> | undefined): string {
43 const unique = [...new Set(weekdays ?? [])].toSorted((x, y) => x - y);
44 if (unique.length === 0 || unique.length === 7) return "daily";
45 return unique.join(",");
46}
47
48export function isSameSchedule(a: ScheduledTaskSchedule, b: ScheduledTaskSchedule): boolean {
49 if (a.type === "interval") {
50 return b.type === "interval" && a.everyMs === b.everyMs;
51 }
52 if (a.type === "webhook") return b.type === "webhook";
53 if (b.type !== "fixed_time") return false;
54 // The contract accepts padded and unpadded hours ("9:00" and "09:00"), so
55 // compare the parsed time — string equality would treat a format-only edit
56 // as a schedule change and recompute the pending run.
57 const aTime = parseTimeOfDay(a.timeOfDay);
58 const bTime = parseTimeOfDay(b.timeOfDay);
59 return (
60 aTime !== null &&
61 bTime !== null &&
62 aTime.hour === bTime.hour &&
63 aTime.minute === bTime.minute &&
64 weekdayKey(a.weekdays) === weekdayKey(b.weekdays)
65 );
66}
67
68const MISSED_FIXED_TIME_GRACE_MS = 10 * MINUTE_MS;
69
70export function isMissedFixedTimeRun(
71 schedule: ScheduledTaskSchedule,
72 dueAt: DateTime.DateTime,
73 now: DateTime.DateTime,
74): boolean {
75 if (schedule.type !== "fixed_time") return false;
76 return DateTime.toEpochMillis(now) - DateTime.toEpochMillis(dueAt) > MISSED_FIXED_TIME_GRACE_MS;
77}
78
79function describeSchedule(schedule: ScheduledTaskSchedule): string {
80 if (schedule.type === "webhook") return "On webhook";
81 if (schedule.type === "interval") {
82 const minutes = schedule.everyMs / MINUTE_MS;
83 if (Number.isInteger(minutes)) {
84 return `Every ${minutes === 1 ? "minute" : `${minutes} minutes`}`;
85 }
86 return `Every ${Math.round(schedule.everyMs / 1000)} seconds`;
87 }
88
89 const weekdayCount = schedule.weekdays?.length ?? 0;
90 const days =
91 weekdayCount === 0
92 ? "day"
93 : weekdayCount === 5 && schedule.weekdays?.every((day) => day >= 1 && day <= 5)
94 ? "weekday"
95 : "selected day";
96 return `At ${schedule.timeOfDay} every ${days}`;
97}