@vite-hub/schedule 0.0.0 → 0.0.1
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/README.md +59 -1
- package/dist/client-BJ33CRLg.js +401 -0
- package/dist/discovery-Di9MD-SN.js +574 -0
- package/dist/index.d.ts +59 -0
- package/dist/index.js +152 -0
- package/dist/module-C_j2ahfg.js +1767 -0
- package/dist/nitro.d.ts +6 -0
- package/dist/nitro.js +2 -0
- package/dist/registry-module.d.ts +9 -0
- package/dist/runtime/state.d.ts +13 -0
- package/dist/runtime/state.js +2 -0
- package/dist/runtime/static.d.ts +17 -0
- package/dist/runtime/static.js +15 -0
- package/dist/runtime.d.ts +4 -0
- package/dist/runtime.js +3 -0
- package/dist/state-DoONzfij.js +441 -0
- package/dist/store-CeeSsInC.d.ts +63 -0
- package/dist/types-DOjXDq0s.d.ts +111 -0
- package/dist/vite.d.ts +9 -0
- package/dist/vite.js +110 -0
- package/package.json +70 -5
package/dist/index.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { t as discoverScheduleDefinitions } from "./discovery-Di9MD-SN.js";
|
|
2
|
+
import { a as resetScheduleRuntime, c as setScheduleRuntimeRegistry, d as createMemoryRuntimeScheduleStore, f as createMemoryScheduleRunStore, i as loadScheduleDefinition, l as createKVRuntimeScheduleStore, n as getScheduleRunStore, o as setRuntimeScheduleStore, p as ScheduleError, r as getScheduleRuntimeRegistry, s as setScheduleRunStore, t as getRuntimeScheduleStore, u as createKVScheduleRunStore } from "./state-DoONzfij.js";
|
|
3
|
+
import { a as executeSchedule, i as executeRuntimeSchedule, n as validateRuntimeScheduleCron, o as executeStaticSchedule, r as createScheduleRun, t as schedules } from "./client-BJ33CRLg.js";
|
|
4
|
+
import { parseCronExpression } from "cron-schedule";
|
|
5
|
+
//#region src/definition.ts
|
|
6
|
+
const cronFieldPattern = /^[^\s]+$/;
|
|
7
|
+
const scheduleDefinitionKeys = new Set([
|
|
8
|
+
"allowRuntimeSchedules",
|
|
9
|
+
"cron",
|
|
10
|
+
"handler"
|
|
11
|
+
]);
|
|
12
|
+
function isPlainObject(value) {
|
|
13
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
14
|
+
}
|
|
15
|
+
function validateCron(cron) {
|
|
16
|
+
if (typeof cron !== "string" || cron.trim() !== cron || cron.length === 0) throw new TypeError("`defineSchedule()` requires a cron string.");
|
|
17
|
+
const fields = cron.split(/\s+/);
|
|
18
|
+
if (fields.length !== 5 || !fields.every((field) => cronFieldPattern.test(field))) throw new TypeError("`defineSchedule()` cron must be a five-field UTC cron expression.");
|
|
19
|
+
}
|
|
20
|
+
function defineSchedule(input) {
|
|
21
|
+
if (!isPlainObject(input)) throw new TypeError("`defineSchedule()` expects an object with `cron` and `handler`.");
|
|
22
|
+
const unknownKey = Object.keys(input).find((key) => !scheduleDefinitionKeys.has(key));
|
|
23
|
+
if (unknownKey) throw new TypeError(`\`defineSchedule()\` does not support the "${unknownKey}" option.`);
|
|
24
|
+
validateCron(input.cron);
|
|
25
|
+
if (typeof input.handler !== "function") throw new TypeError("`defineSchedule()` requires a schedule handler.");
|
|
26
|
+
if (typeof input.allowRuntimeSchedules !== "undefined" && typeof input.allowRuntimeSchedules !== "boolean") throw new TypeError("`defineSchedule()` allowRuntimeSchedules must be a boolean.");
|
|
27
|
+
const definition = {
|
|
28
|
+
cron: input.cron,
|
|
29
|
+
handler: input.handler
|
|
30
|
+
};
|
|
31
|
+
if (typeof input.allowRuntimeSchedules !== "undefined") definition.options = { allowRuntimeSchedules: input.allowRuntimeSchedules };
|
|
32
|
+
return definition;
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/runtime/runner.ts
|
|
36
|
+
const DEFAULT_INTERVAL_MS = 6e4;
|
|
37
|
+
const DEFAULT_CONCURRENCY = 1;
|
|
38
|
+
function floorUTCMinute(date) {
|
|
39
|
+
return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes()));
|
|
40
|
+
}
|
|
41
|
+
function isDue(schedule, scheduledAt) {
|
|
42
|
+
if (schedule.cron.trim().split(/\s+/).length !== 5) throw new TypeError(`Runtime Schedule "${schedule.id}" must use a five-field cron expression.`);
|
|
43
|
+
const cron = parseCronExpression(schedule.cron);
|
|
44
|
+
const minute = scheduledAt.getUTCMinutes();
|
|
45
|
+
const hour = scheduledAt.getUTCHours();
|
|
46
|
+
const day = scheduledAt.getUTCDate();
|
|
47
|
+
const month = scheduledAt.getUTCMonth();
|
|
48
|
+
const weekday = scheduledAt.getUTCDay();
|
|
49
|
+
if (!cron.minutes.includes(minute) || !cron.hours.includes(hour) || !cron.months.includes(month)) return false;
|
|
50
|
+
if (cron.days.length !== 31 && cron.weekdays.length !== 7) return cron.days.includes(day) || cron.weekdays.includes(weekday);
|
|
51
|
+
return cron.days.includes(day) && cron.weekdays.includes(weekday);
|
|
52
|
+
}
|
|
53
|
+
function toRunId(scheduleId, scheduledAt) {
|
|
54
|
+
return `srun_runtime_${encodeURIComponent(scheduleId)}_${scheduledAt.toISOString()}`;
|
|
55
|
+
}
|
|
56
|
+
function reportError(error, onError) {
|
|
57
|
+
if (!onError) return;
|
|
58
|
+
try {
|
|
59
|
+
Promise.resolve(onError(error)).catch(() => {});
|
|
60
|
+
} catch {}
|
|
61
|
+
}
|
|
62
|
+
function startScheduleRunner(options = {}) {
|
|
63
|
+
const intervalMs = options.intervalMs ?? DEFAULT_INTERVAL_MS;
|
|
64
|
+
const concurrency = options.concurrency ?? DEFAULT_CONCURRENCY;
|
|
65
|
+
if (!Number.isFinite(intervalMs) || intervalMs <= 0) throw new TypeError("Schedule Runner intervalMs must be a positive number.");
|
|
66
|
+
if (!Number.isInteger(concurrency) || concurrency < 1) throw new TypeError("Schedule Runner concurrency must be a positive integer.");
|
|
67
|
+
const runtimeScheduleStore = options.runtimeScheduleStore ?? getRuntimeScheduleStore();
|
|
68
|
+
const scheduleRunStore = options.scheduleRunStore ?? getScheduleRunStore();
|
|
69
|
+
const now = options.now ?? (() => /* @__PURE__ */ new Date());
|
|
70
|
+
let stopped = false;
|
|
71
|
+
let active = 0;
|
|
72
|
+
const queuedRuns = [];
|
|
73
|
+
const queuedRunIds = /* @__PURE__ */ new Set();
|
|
74
|
+
let scanning = false;
|
|
75
|
+
let timer;
|
|
76
|
+
const pump = () => {
|
|
77
|
+
while (!stopped && active < concurrency) {
|
|
78
|
+
const next = queuedRuns.shift();
|
|
79
|
+
if (!next) return;
|
|
80
|
+
dispatch(next.schedule, next.scheduledAt);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
const dispatch = (schedule, scheduledAt) => {
|
|
84
|
+
active++;
|
|
85
|
+
executeRuntimeSchedule({
|
|
86
|
+
id: schedule.id,
|
|
87
|
+
runtimeScheduleStore,
|
|
88
|
+
scheduledAt,
|
|
89
|
+
scheduleRunStore
|
|
90
|
+
}).catch((error) => {
|
|
91
|
+
reportError(error, options.onError);
|
|
92
|
+
}).finally(() => {
|
|
93
|
+
active--;
|
|
94
|
+
queuedRunIds.delete(toRunId(schedule.id, scheduledAt));
|
|
95
|
+
pump();
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
const scan = () => {
|
|
99
|
+
if (stopped || scanning) return;
|
|
100
|
+
scanning = true;
|
|
101
|
+
Promise.resolve().then(() => runtimeScheduleStore.list()).then(async (schedules) => {
|
|
102
|
+
if (stopped) return;
|
|
103
|
+
const scheduledAt = floorUTCMinute(now());
|
|
104
|
+
for (const schedule of schedules) {
|
|
105
|
+
if (stopped) return;
|
|
106
|
+
if (!schedule.enabled) continue;
|
|
107
|
+
let due = false;
|
|
108
|
+
try {
|
|
109
|
+
due = isDue(schedule, scheduledAt);
|
|
110
|
+
} catch (error) {
|
|
111
|
+
reportError(error, options.onError);
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (!due) continue;
|
|
115
|
+
const runId = toRunId(schedule.id, scheduledAt);
|
|
116
|
+
if (queuedRunIds.has(runId)) continue;
|
|
117
|
+
try {
|
|
118
|
+
if (await scheduleRunStore.getRun(runId)) continue;
|
|
119
|
+
} catch (error) {
|
|
120
|
+
reportError(error, options.onError);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
queuedRunIds.add(runId);
|
|
124
|
+
queuedRuns.push({
|
|
125
|
+
scheduledAt,
|
|
126
|
+
schedule
|
|
127
|
+
});
|
|
128
|
+
pump();
|
|
129
|
+
}
|
|
130
|
+
}).catch((error) => {
|
|
131
|
+
reportError(error, options.onError);
|
|
132
|
+
}).finally(() => {
|
|
133
|
+
scanning = false;
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
timer = setInterval(scan, intervalMs);
|
|
137
|
+
scan();
|
|
138
|
+
return {
|
|
139
|
+
get running() {
|
|
140
|
+
return !stopped;
|
|
141
|
+
},
|
|
142
|
+
stop() {
|
|
143
|
+
stopped = true;
|
|
144
|
+
if (timer) {
|
|
145
|
+
clearInterval(timer);
|
|
146
|
+
timer = void 0;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
//#endregion
|
|
152
|
+
export { ScheduleError, createKVRuntimeScheduleStore, createKVScheduleRunStore, createMemoryRuntimeScheduleStore, createMemoryScheduleRunStore, createScheduleRun, defineSchedule, discoverScheduleDefinitions, executeRuntimeSchedule, executeSchedule, executeStaticSchedule, getRuntimeScheduleStore, getScheduleRunStore, getScheduleRuntimeRegistry, loadScheduleDefinition, resetScheduleRuntime, schedules, setRuntimeScheduleStore, setScheduleRunStore, setScheduleRuntimeRegistry, startScheduleRunner, validateRuntimeScheduleCron };
|