Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Schedule.ts

98 lines3.5 KB

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]) };
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 return DateTime.add(from, {
21 milliseconds: Math.max(schedule.everyMs, MIN_SCHEDULED_TASK_INTERVAL_MS),
22 });
23 }
24​
25 const time = parseTimeOfDay(schedule.timeOfDay);
26 if (time === null) return null;
27 const weekdays =
28 schedule.weekdays && schedule.weekdays.length > 0 ? new Set(schedule.weekdays) : null;
29 for (let offset = 0; offset <= 7; offset += 1) {
30 const candidate = DateTime.setParts(DateTime.add(from, { days: offset }), {
31 hour: time.hour,
32 minute: time.minute,
33 second: 0,
34 millisecond: 0,
35 });
36 if (DateTime.toEpochMillis(candidate) <= DateTime.toEpochMillis(from)) continue;
37 if (weekdays !== null && !weekdays.has(DateTime.toParts(candidate).weekDay)) continue;
38 return candidate;
39 }
40 return null;
42​
43function weekdayKey(weekdays: ReadonlyArray<number> | undefined): string {
44 const unique = [...new Set(weekdays ?? [])].toSorted((x, y) => x - y);
45 if (unique.length === 0 || unique.length === 7) return "daily";
46 return unique.join(",");
48​
49export function isSameSchedule(a: ScheduledTaskSchedule, b: ScheduledTaskSchedule): boolean {
50 if (a.type === "interval") {
51 return b.type === "interval" && a.everyMs === b.everyMs;
52 }
53 if (a.type === "webhook") return b.type === "webhook";
54 if (b.type !== "fixed_time") return false;
55 // The contract accepts padded and unpadded hours ("9:00" and "09:00"), so
56 // compare the parsed time — string equality would treat a format-only edit
57 // as a schedule change and recompute the pending run.
58 const aTime = parseTimeOfDay(a.timeOfDay);
59 const bTime = parseTimeOfDay(b.timeOfDay);
60 return (
61 aTime !== null &&
62 bTime !== null &&
63 aTime.hour === bTime.hour &&
64 aTime.minute === bTime.minute &&
65 weekdayKey(a.weekdays) === weekdayKey(b.weekdays)
66 );
68​
69const MISSED_FIXED_TIME_GRACE_MS = 10 * MINUTE_MS;
70​
71export function isMissedFixedTimeRun(
72 schedule: ScheduledTaskSchedule,
73 dueAt: DateTime.DateTime,
74 now: DateTime.DateTime,
75): boolean {
76 if (schedule.type !== "fixed_time") return false;
77 return DateTime.toEpochMillis(now) - DateTime.toEpochMillis(dueAt) > MISSED_FIXED_TIME_GRACE_MS;
79​
80function describeSchedule(schedule: ScheduledTaskSchedule): string {
81 if (schedule.type === "webhook") return "On webhook";
82 if (schedule.type === "interval") {
83 const minutes = schedule.everyMs / MINUTE_MS;
84 if (Number.isInteger(minutes)) {
85 return `Every ${minutes === 1 ? "minute" : `${minutes} minutes`}`;
86 }
87 return `Every ${Math.round(schedule.everyMs / 1000)} seconds`;
88 }
89​
90 const weekdayCount = schedule.weekdays?.length ?? 0;
91 const days =
92 weekdayCount === 0
93 ? "day"
94 : weekdayCount === 5 && schedule.weekdays?.every((day) => day >= 1 && day <= 5)
95 ? "weekday"
96 : "selected day";
97 return `At ${schedule.timeOfDay} every ${days}`;