@lunora/scheduler 0.0.0 → 1.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs ADDED
@@ -0,0 +1,8 @@
1
+ export { default as createScheduler } from './packem_shared/createScheduler-CWMn70nv.mjs';
2
+ export { default as createWorkpool } from './packem_shared/createWorkpool-CEnqCafM.mjs';
3
+ export { createCronTrigger } from './packem_shared/createCronTrigger-Cq9IBcWQ.mjs';
4
+ export { CRON_SCHEDULE_KINDS, compileCronSchedule, cronJobs } from './packem_shared/CRON_SCHEDULE_KINDS-BaLlXJiN.mjs';
5
+ export { createQueueConsumer, createQueueWorkpool, httpDispatcher } from './packem_shared/createQueueConsumer-DWahNPfz.mjs';
6
+ export { SchedulerDO } from './packem_shared/SchedulerDO-BNzXNnS4.mjs';
7
+ export { isWorkflowReference } from './packem_shared/isWorkflowReference-C9mQkMXt.mjs';
8
+ export { assertValidCronExpression, isValidCronExpression } from './packem_shared/assertValidCronExpression-BLfrDgmK.mjs';
@@ -0,0 +1,123 @@
1
+ import { isWorkflowReference } from './isWorkflowReference-C9mQkMXt.mjs';
2
+ import { assertValidCronExpression } from './assertValidCronExpression-BLfrDgmK.mjs';
3
+
4
+ const WEEKDAY_INDEX = {
5
+ friday: 5,
6
+ monday: 1,
7
+ saturday: 6,
8
+ sunday: 0,
9
+ thursday: 4,
10
+ tuesday: 2,
11
+ wednesday: 3
12
+ };
13
+ const field = (value, label, min, max) => {
14
+ if (!Number.isInteger(value) || value < min || value > max) {
15
+ throw new Error(`@lunora/scheduler: cronJobs ${label} must be an integer in [${min.toFixed(0)}, ${max.toFixed(0)}], got ${String(value)}`);
16
+ }
17
+ return value.toFixed(0);
18
+ };
19
+ const compileInterval = (schedule) => {
20
+ const units = ["seconds", "minutes", "hours"].filter((unit2) => schedule[unit2] !== void 0);
21
+ if (units.length !== 1) {
22
+ throw new Error(`@lunora/scheduler: interval schedule must specify exactly one of { seconds, minutes, hours }`);
23
+ }
24
+ const unit = units[0];
25
+ const value = schedule[unit];
26
+ if (unit === "seconds") {
27
+ return `*/${field(value, "interval.seconds", 1, 59)} * * * * *`;
28
+ }
29
+ if (unit === "minutes") {
30
+ return `*/${field(value, "interval.minutes", 1, 59)} * * * *`;
31
+ }
32
+ return `0 */${field(value, "interval.hours", 1, 23)} * * *`;
33
+ };
34
+ const compileDaily = (schedule) => {
35
+ const minute = field(schedule.minuteUTC, "daily.minuteUTC", 0, 59);
36
+ const hour = field(schedule.hourUTC, "daily.hourUTC", 0, 23);
37
+ return `${minute} ${hour} * * *`;
38
+ };
39
+ const compileWeekly = (schedule) => {
40
+ const index = WEEKDAY_INDEX[schedule.dayOfWeek];
41
+ if (index === void 0) {
42
+ throw new Error(`@lunora/scheduler: weekly schedule has invalid dayOfWeek "${schedule.dayOfWeek}"`);
43
+ }
44
+ const minute = field(schedule.minuteUTC, "weekly.minuteUTC", 0, 59);
45
+ const hour = field(schedule.hourUTC, "weekly.hourUTC", 0, 23);
46
+ return `${minute} ${hour} * * ${index.toFixed(0)}`;
47
+ };
48
+ const compileMonthly = (schedule) => {
49
+ const day = field(schedule.day, "monthly.day", 1, 31);
50
+ const minute = field(schedule.minuteUTC, "monthly.minuteUTC", 0, 59);
51
+ const hour = field(schedule.hourUTC, "monthly.hourUTC", 0, 23);
52
+ return `${minute} ${hour} ${day} * *`;
53
+ };
54
+ const CRON_SCHEDULE_KINDS = /* @__PURE__ */ new Set(["daily", "interval", "monthly", "weekly"]);
55
+ const compileCronSchedule = (kind, schedule) => {
56
+ switch (kind) {
57
+ case "daily": {
58
+ return compileDaily(schedule);
59
+ }
60
+ case "interval": {
61
+ return compileInterval(schedule);
62
+ }
63
+ case "monthly": {
64
+ return compileMonthly(schedule);
65
+ }
66
+ case "weekly": {
67
+ return compileWeekly(schedule);
68
+ }
69
+ default: {
70
+ throw new Error(`@lunora/scheduler: unknown cron schedule kind "${String(kind)}"`);
71
+ }
72
+ }
73
+ };
74
+ const cronJobs = () => {
75
+ const jobs = [];
76
+ const seen = /* @__PURE__ */ new Set();
77
+ const register = (name, cron, target, args) => {
78
+ if (typeof name !== "string" || name.trim() === "") {
79
+ throw new Error(`@lunora/scheduler: cron job name must be a non-empty string`);
80
+ }
81
+ if (seen.has(name)) {
82
+ throw new Error(`@lunora/scheduler: duplicate cron job name "${name}" — names must be unique within one cronJobs()`);
83
+ }
84
+ if (isWorkflowReference(target)) {
85
+ assertValidCronExpression(cron, `cron expression for job "${name}"`);
86
+ seen.add(name);
87
+ jobs.push({ args: args ?? {}, cron, name, workflow: typeof target.name === "string" ? target.name : "" });
88
+ return;
89
+ }
90
+ if (!target || typeof target.__lunoraRef !== "string") {
91
+ throw new Error(`@lunora/scheduler: cron job "${name}" requires a function reference (e.g. internal.email.digest) or a workflow reference`);
92
+ }
93
+ assertValidCronExpression(cron, `cron expression for job "${name}"`);
94
+ seen.add(name);
95
+ jobs.push({ args: args ?? {}, cron, functionPath: target.__lunoraRef, name });
96
+ };
97
+ const builder = {
98
+ cron(name, cronExpr, function_, args) {
99
+ register(name, cronExpr, function_, args);
100
+ return builder;
101
+ },
102
+ daily(name, schedule, function_, args) {
103
+ register(name, compileCronSchedule("daily", schedule), function_, args);
104
+ return builder;
105
+ },
106
+ interval(name, schedule, function_, args) {
107
+ register(name, compileCronSchedule("interval", schedule), function_, args);
108
+ return builder;
109
+ },
110
+ jobs: () => [...jobs],
111
+ monthly(name, schedule, function_, args) {
112
+ register(name, compileCronSchedule("monthly", schedule), function_, args);
113
+ return builder;
114
+ },
115
+ weekly(name, schedule, function_, args) {
116
+ register(name, compileCronSchedule("weekly", schedule), function_, args);
117
+ return builder;
118
+ }
119
+ };
120
+ return builder;
121
+ };
122
+
123
+ export { CRON_SCHEDULE_KINDS, compileCronSchedule, cronJobs };