@vite-hub/schedule 0.0.1 → 0.0.3
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 +73 -8
- package/dist/client-BMFCy0dX.js +233 -0
- package/dist/definition.d.ts +7 -0
- package/dist/definition.js +42 -0
- package/dist/discovery-BZlat3Kc.js +776 -0
- package/dist/due-Bbh4yIxq.js +59 -0
- package/dist/execute-ClFYvmp9.js +211 -0
- package/dist/index.d.ts +28 -23
- package/dist/index.js +7 -152
- package/dist/kv-BJ1I9IlJ.js +6 -0
- package/dist/kv-storage-DWinmLLy.js +30 -0
- package/dist/nuxt.d.ts +19 -0
- package/dist/nuxt.js +25 -0
- package/dist/runtime/driver.d.ts +26 -0
- package/dist/runtime/driver.js +334 -0
- package/dist/runtime/process.d.ts +11 -0
- package/dist/runtime/process.js +285 -0
- package/dist/runtime/state.d.ts +2 -2
- package/dist/runtime/state.js +1 -1
- package/dist/runtime/static.d.ts +27 -3
- package/dist/runtime/static.js +126 -3
- package/dist/runtime.d.ts +4 -3
- package/dist/runtime.js +3 -3
- package/dist/{state-DoONzfij.js → state-By7WU_ks.js} +55 -24
- package/dist/store-t-74rsM_.d.ts +48 -0
- package/dist/{types-DOjXDq0s.d.ts → types-Bcje4dLV.d.ts} +34 -9
- package/dist/vite-BX6_KiRw.js +1218 -0
- package/dist/vite.d.ts +35 -6
- package/dist/vite.js +2 -110
- package/dist/wait-until-CR-JjIOb.js +23 -0
- package/package.json +40 -39
- package/dist/client-BJ33CRLg.js +0 -401
- package/dist/discovery-Di9MD-SN.js +0 -574
- package/dist/module-C_j2ahfg.js +0 -1767
- package/dist/nitro.d.ts +0 -6
- package/dist/nitro.js +0 -2
- package/dist/store-CeeSsInC.d.ts +0 -63
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { parseCronExpression } from "cron-schedule";
|
|
2
|
+
//#region src/runtime/due.ts
|
|
3
|
+
const weekdayIndexes = new Map([
|
|
4
|
+
["Sun", 0],
|
|
5
|
+
["Mon", 1],
|
|
6
|
+
["Tue", 2],
|
|
7
|
+
["Wed", 3],
|
|
8
|
+
["Thu", 4],
|
|
9
|
+
["Fri", 5],
|
|
10
|
+
["Sat", 6]
|
|
11
|
+
]);
|
|
12
|
+
function scheduleDateFields(scheduledAt, timeZone) {
|
|
13
|
+
if (!timeZone) return {
|
|
14
|
+
day: scheduledAt.getUTCDate(),
|
|
15
|
+
hour: scheduledAt.getUTCHours(),
|
|
16
|
+
minute: scheduledAt.getUTCMinutes(),
|
|
17
|
+
month: scheduledAt.getUTCMonth(),
|
|
18
|
+
weekday: scheduledAt.getUTCDay()
|
|
19
|
+
};
|
|
20
|
+
const parts = new Intl.DateTimeFormat("en-US", {
|
|
21
|
+
day: "numeric",
|
|
22
|
+
hour: "numeric",
|
|
23
|
+
hourCycle: "h23",
|
|
24
|
+
minute: "numeric",
|
|
25
|
+
month: "numeric",
|
|
26
|
+
timeZone,
|
|
27
|
+
weekday: "short"
|
|
28
|
+
}).formatToParts(scheduledAt);
|
|
29
|
+
const values = new Map(parts.map((part) => [part.type, part.value]));
|
|
30
|
+
const weekday = weekdayIndexes.get(values.get("weekday") || "");
|
|
31
|
+
const day = Number(values.get("day"));
|
|
32
|
+
const hour = Number(values.get("hour"));
|
|
33
|
+
const minute = Number(values.get("minute"));
|
|
34
|
+
const month = Number(values.get("month")) - 1;
|
|
35
|
+
if (weekday === void 0 || [
|
|
36
|
+
day,
|
|
37
|
+
hour,
|
|
38
|
+
minute,
|
|
39
|
+
month
|
|
40
|
+
].some((value) => !Number.isInteger(value))) throw new TypeError(`Runtime Schedule time zone could not resolve calendar fields: ${timeZone}`);
|
|
41
|
+
return {
|
|
42
|
+
day,
|
|
43
|
+
hour,
|
|
44
|
+
minute,
|
|
45
|
+
month,
|
|
46
|
+
weekday
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function isRuntimeScheduleDue(schedule, scheduledAt) {
|
|
50
|
+
if (scheduledAt.getUTCSeconds() !== 0 || scheduledAt.getUTCMilliseconds() !== 0) return false;
|
|
51
|
+
if (schedule.cron.trim().split(/\s+/).length !== 5) throw new TypeError(`Runtime Schedule "${schedule.id}" must use a five-field cron expression.`);
|
|
52
|
+
const cron = parseCronExpression(schedule.cron);
|
|
53
|
+
const { day, hour, minute, month, weekday } = scheduleDateFields(scheduledAt, schedule.timeZone);
|
|
54
|
+
if (!cron.minutes.includes(minute) || !cron.hours.includes(hour) || !cron.months.includes(month)) return false;
|
|
55
|
+
if (cron.days.length !== 31 && cron.weekdays.length !== 7) return cron.days.includes(day) || cron.weekdays.includes(weekday);
|
|
56
|
+
return cron.days.includes(day) && cron.weekdays.includes(weekday);
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
export { isRuntimeScheduleDue as t };
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { h as invalidScheduleValueDetails, i as loadScheduleDefinition, m as createScheduleError, n as getScheduleRunStore, p as assertRuntimeScheduleId, t as getRuntimeScheduleStore } from "./state-By7WU_ks.js";
|
|
2
|
+
import { t as isRuntimeScheduleDue } from "./due-Bbh4yIxq.js";
|
|
3
|
+
import { t as createLocalWaitUntil } from "./wait-until-CR-JjIOb.js";
|
|
4
|
+
//#region ../internal/src/runtime/random.ts
|
|
5
|
+
function randomId(prefix) {
|
|
6
|
+
const body = `${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
|
|
7
|
+
return prefix ? `${prefix}_${body}` : body;
|
|
8
|
+
}
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region src/runtime/execute.ts
|
|
11
|
+
function assertRuntimeExecuteOptionsObject(options) {
|
|
12
|
+
if (typeof options !== "object" || options === null || Array.isArray(options)) throw createScheduleError("SCHEDULE_INVALID_INPUT", { details: invalidScheduleValueDetails("options", options) });
|
|
13
|
+
}
|
|
14
|
+
function normalizeRunSource(source) {
|
|
15
|
+
return source ?? "direct";
|
|
16
|
+
}
|
|
17
|
+
function toRunId(source, scheduleId, scheduledAt) {
|
|
18
|
+
return `srun_${normalizeRunSource(source)}_${encodeURIComponent(scheduleId)}_${scheduledAt.toISOString()}`;
|
|
19
|
+
}
|
|
20
|
+
function validateScheduledAt(scheduledAt) {
|
|
21
|
+
if (!(scheduledAt instanceof Date) || Number.isNaN(scheduledAt.getTime())) throw createScheduleError("SCHEDULE_INVALID_SCHEDULED_AT", { details: invalidScheduleValueDetails("scheduledAt", scheduledAt) });
|
|
22
|
+
return scheduledAt;
|
|
23
|
+
}
|
|
24
|
+
function toRunError(error) {
|
|
25
|
+
if (error instanceof Error) return {
|
|
26
|
+
message: error.message,
|
|
27
|
+
name: error.name,
|
|
28
|
+
stack: error.stack
|
|
29
|
+
};
|
|
30
|
+
return { message: String(error) };
|
|
31
|
+
}
|
|
32
|
+
function requireUpdatedRun(run) {
|
|
33
|
+
if (!run) throw createScheduleError("SCHEDULE_RUN_NOT_FOUND");
|
|
34
|
+
return run;
|
|
35
|
+
}
|
|
36
|
+
async function createOrGetRun(options) {
|
|
37
|
+
const store = options.runStore ?? getScheduleRunStore();
|
|
38
|
+
const id = toRunId(options.source, options.scheduleId, options.scheduledAt);
|
|
39
|
+
const existing = await store.getRun(id);
|
|
40
|
+
if (existing) return {
|
|
41
|
+
created: false,
|
|
42
|
+
run: existing
|
|
43
|
+
};
|
|
44
|
+
const now = /* @__PURE__ */ new Date();
|
|
45
|
+
const runInput = {
|
|
46
|
+
attemptCount: 0,
|
|
47
|
+
createdAt: now,
|
|
48
|
+
id,
|
|
49
|
+
scheduleId: options.scheduleId,
|
|
50
|
+
scheduledAt: options.scheduledAt,
|
|
51
|
+
status: "pending",
|
|
52
|
+
target: options.target,
|
|
53
|
+
updatedAt: now
|
|
54
|
+
};
|
|
55
|
+
try {
|
|
56
|
+
return {
|
|
57
|
+
created: true,
|
|
58
|
+
run: await store.createRun(runInput)
|
|
59
|
+
};
|
|
60
|
+
} catch (error) {
|
|
61
|
+
const run = await store.getRun(id);
|
|
62
|
+
if (run) return {
|
|
63
|
+
created: false,
|
|
64
|
+
run
|
|
65
|
+
};
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
async function startAttempt(run, store = getScheduleRunStore()) {
|
|
70
|
+
const now = /* @__PURE__ */ new Date();
|
|
71
|
+
const attempt = await store.createAttempt({
|
|
72
|
+
createdAt: now,
|
|
73
|
+
id: randomId("satt"),
|
|
74
|
+
runId: run.id,
|
|
75
|
+
startedAt: now,
|
|
76
|
+
status: "running",
|
|
77
|
+
updatedAt: now
|
|
78
|
+
});
|
|
79
|
+
await store.updateRun(run.id, {
|
|
80
|
+
attemptCount: run.attemptCount + 1,
|
|
81
|
+
startedAt: run.startedAt ?? now,
|
|
82
|
+
status: "running",
|
|
83
|
+
updatedAt: now
|
|
84
|
+
});
|
|
85
|
+
return attempt;
|
|
86
|
+
}
|
|
87
|
+
function toHandlerContext(run, attempt, input, waitUntil) {
|
|
88
|
+
return {
|
|
89
|
+
attemptId: attempt.id,
|
|
90
|
+
id: run.id,
|
|
91
|
+
...input !== void 0 ? { input } : {},
|
|
92
|
+
runId: run.id,
|
|
93
|
+
scheduleId: run.scheduleId,
|
|
94
|
+
scheduledAt: run.scheduledAt,
|
|
95
|
+
target: run.target,
|
|
96
|
+
waitUntil
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
async function completeRun(run, attempt, store = getScheduleRunStore()) {
|
|
100
|
+
const now = /* @__PURE__ */ new Date();
|
|
101
|
+
await store.updateAttempt(attempt.id, {
|
|
102
|
+
completedAt: now,
|
|
103
|
+
status: "succeeded",
|
|
104
|
+
updatedAt: now
|
|
105
|
+
});
|
|
106
|
+
return requireUpdatedRun(await store.updateRun(run.id, {
|
|
107
|
+
completedAt: now,
|
|
108
|
+
status: "succeeded",
|
|
109
|
+
updatedAt: now
|
|
110
|
+
}));
|
|
111
|
+
}
|
|
112
|
+
async function failRun(run, attempt, error, store = getScheduleRunStore()) {
|
|
113
|
+
const now = /* @__PURE__ */ new Date();
|
|
114
|
+
const serializedError = toRunError(error);
|
|
115
|
+
await store.updateAttempt(attempt.id, {
|
|
116
|
+
completedAt: now,
|
|
117
|
+
error: serializedError,
|
|
118
|
+
status: "failed",
|
|
119
|
+
updatedAt: now
|
|
120
|
+
});
|
|
121
|
+
return requireUpdatedRun(await store.updateRun(run.id, {
|
|
122
|
+
completedAt: now,
|
|
123
|
+
error: serializedError,
|
|
124
|
+
status: "failed",
|
|
125
|
+
updatedAt: now
|
|
126
|
+
}));
|
|
127
|
+
}
|
|
128
|
+
async function createScheduleRun(options) {
|
|
129
|
+
const scheduledAt = validateScheduledAt(options.scheduledAt ?? /* @__PURE__ */ new Date());
|
|
130
|
+
return (await createOrGetRun({
|
|
131
|
+
...options,
|
|
132
|
+
scheduledAt
|
|
133
|
+
})).run;
|
|
134
|
+
}
|
|
135
|
+
async function executeSchedule(options) {
|
|
136
|
+
const scheduledAt = validateScheduledAt(options.scheduledAt ?? /* @__PURE__ */ new Date());
|
|
137
|
+
const { created, run } = await createOrGetRun({
|
|
138
|
+
...options,
|
|
139
|
+
scheduledAt
|
|
140
|
+
});
|
|
141
|
+
if (!created) return run;
|
|
142
|
+
const runStore = options.runStore ?? getScheduleRunStore();
|
|
143
|
+
const attempt = await startAttempt(run, runStore);
|
|
144
|
+
const localWaitUntil = createLocalWaitUntil();
|
|
145
|
+
const waitUntil = options.waitUntil ?? localWaitUntil.waitUntil;
|
|
146
|
+
try {
|
|
147
|
+
await options.definition.handler(toHandlerContext(run, attempt, options.input, waitUntil));
|
|
148
|
+
if (!options.waitUntil) await localWaitUntil.flush();
|
|
149
|
+
return await completeRun(run, attempt, runStore);
|
|
150
|
+
} catch (error) {
|
|
151
|
+
if (!options.waitUntil) try {
|
|
152
|
+
await localWaitUntil.flush();
|
|
153
|
+
} catch {}
|
|
154
|
+
await failRun(run, attempt, error, runStore);
|
|
155
|
+
throw error;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
async function executeStaticSchedule(options) {
|
|
159
|
+
return await executeSchedule({
|
|
160
|
+
definition: options.definition,
|
|
161
|
+
scheduleId: options.name,
|
|
162
|
+
source: "static",
|
|
163
|
+
scheduledAt: options.scheduledAt,
|
|
164
|
+
target: options.name,
|
|
165
|
+
waitUntil: options.waitUntil
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
async function loadRequiredRuntimeSchedule(id, store = getRuntimeScheduleStore()) {
|
|
169
|
+
const schedule = await store.get(id);
|
|
170
|
+
if (!schedule) throw createScheduleError("SCHEDULE_NOT_FOUND");
|
|
171
|
+
return schedule;
|
|
172
|
+
}
|
|
173
|
+
async function executeRuntimeSchedule(options) {
|
|
174
|
+
const runtimeOptions = typeof options === "string" ? { id: options } : options;
|
|
175
|
+
assertRuntimeExecuteOptionsObject(runtimeOptions);
|
|
176
|
+
assertRuntimeScheduleId(runtimeOptions.id);
|
|
177
|
+
const id = runtimeOptions.id;
|
|
178
|
+
const scheduledAt = validateScheduledAt(runtimeOptions.scheduledAt ?? /* @__PURE__ */ new Date());
|
|
179
|
+
const runtimeScheduleStore = runtimeOptions.runtimeScheduleStore;
|
|
180
|
+
const scheduleRunStore = runtimeOptions.scheduleRunStore;
|
|
181
|
+
const existingRun = await (scheduleRunStore ?? getScheduleRunStore()).getRun(toRunId("runtime", id, scheduledAt));
|
|
182
|
+
if (existingRun) return existingRun;
|
|
183
|
+
const schedule = await loadRequiredRuntimeSchedule(id, runtimeScheduleStore);
|
|
184
|
+
if (!schedule.enabled) throw createScheduleError("SCHEDULE_DISABLED");
|
|
185
|
+
if (runtimeOptions.requireDue && !isRuntimeScheduleDue(schedule, scheduledAt)) throw createScheduleError("SCHEDULE_NOT_DUE");
|
|
186
|
+
const definition = await loadScheduleDefinition(schedule.target);
|
|
187
|
+
if (!definition) throw createScheduleError("SCHEDULE_TARGET_NOT_FOUND");
|
|
188
|
+
if (definition.options?.allowRuntimeSchedules !== true) throw createScheduleError("SCHEDULE_TARGET_NOT_ELIGIBLE");
|
|
189
|
+
return await executeSchedule({
|
|
190
|
+
definition,
|
|
191
|
+
input: schedule.input,
|
|
192
|
+
runStore: scheduleRunStore,
|
|
193
|
+
scheduleId: schedule.id,
|
|
194
|
+
source: "runtime",
|
|
195
|
+
scheduledAt,
|
|
196
|
+
target: schedule.target,
|
|
197
|
+
waitUntil: runtimeOptions.waitUntil
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
async function executeRuntimeScheduleWake(input, options) {
|
|
201
|
+
await executeRuntimeSchedule({
|
|
202
|
+
id: input.scheduleId,
|
|
203
|
+
requireDue: true,
|
|
204
|
+
runtimeScheduleStore: options.runtimeScheduleStore,
|
|
205
|
+
scheduledAt: input.scheduledAt,
|
|
206
|
+
scheduleRunStore: options.scheduleRunStore,
|
|
207
|
+
waitUntil: options.waitUntil
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
//#endregion
|
|
211
|
+
export { executeStaticSchedule as a, executeSchedule as i, executeRuntimeSchedule as n, randomId as o, executeRuntimeScheduleWake as r, createScheduleRun as t };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,59 +1,64 @@
|
|
|
1
|
-
|
|
2
|
-
import { a as
|
|
1
|
+
/// <reference path="./registry-module.d.ts" />
|
|
2
|
+
import { C as ScheduleTargetName, S as ScheduleTargetDefinitionInput, _ as ScheduleRunError, a as RuntimeScheduleStore, b as ScheduleRunStore, c as ScheduleDefinition, d as ScheduleDefinitionRegistry, f as ScheduleHandler, g as ScheduleRunContext, h as ScheduleRunAttemptStatus, i as RuntimeScheduleRecord, l as ScheduleDefinitionInput, m as ScheduleRunAttemptRecord, n as RuntimeScheduleCreateInput, o as RuntimeScheduleUpdateInput, p as ScheduleRegistryDefinition, r as RuntimeScheduleMetadata, t as DiscoveredScheduleDefinition, v as ScheduleRunRecord, x as ScheduleTargetDefinition, y as ScheduleRunStatus } from "./types-Bcje4dLV.js";
|
|
3
|
+
import { defineSchedule, defineScheduleTarget } from "./definition.js";
|
|
4
|
+
import { a as createMemoryScheduleRunStore, c as schedules, i as createMemoryRuntimeScheduleStore, l as validateRuntimeScheduleCron, n as createKVRuntimeScheduleStore, o as ScheduleKVStorage, r as createKVScheduleRunStore, s as createScheduleKVStorage, t as KVScheduleStoreOptions } from "./store-t-74rsM_.js";
|
|
3
5
|
import { getRuntimeScheduleStore, getScheduleRunStore, getScheduleRuntimeRegistry, loadScheduleDefinition, resetScheduleRuntime, setRuntimeScheduleStore, setScheduleRunStore, setScheduleRuntimeRegistry } from "./runtime/state.js";
|
|
6
|
+
import { ViteHubError } from "@vite-hub/runtime";
|
|
4
7
|
|
|
5
|
-
//#region src/definition.d.ts
|
|
6
|
-
declare function defineSchedule<TResult = unknown>(input: ScheduleDefinitionInput<TResult>): ScheduleDefinition<TResult>;
|
|
7
|
-
//#endregion
|
|
8
8
|
//#region src/discovery.d.ts
|
|
9
9
|
declare function discoverScheduleDefinitions(options: {
|
|
10
10
|
mode?: "vite-suffix";
|
|
11
11
|
rootDir: string;
|
|
12
12
|
scanDirs?: string[];
|
|
13
|
+
serverDirs?: string[];
|
|
14
|
+
serverRootDir?: string;
|
|
13
15
|
} | {
|
|
14
|
-
mode: "
|
|
16
|
+
mode: "server-schedules";
|
|
15
17
|
scanDirs: string[];
|
|
16
18
|
}): DiscoveredScheduleDefinition[];
|
|
17
19
|
//#endregion
|
|
18
20
|
//#region src/runtime/execute.d.ts
|
|
19
21
|
interface ExecuteScheduleOptions {
|
|
20
|
-
definition:
|
|
22
|
+
definition: ScheduleRegistryDefinition;
|
|
23
|
+
input?: unknown;
|
|
21
24
|
runStore?: ScheduleRunStore;
|
|
22
25
|
scheduleId: string;
|
|
23
26
|
source?: "direct" | "runtime" | "static";
|
|
24
27
|
scheduledAt?: Date;
|
|
25
28
|
target: ScheduleTargetName;
|
|
29
|
+
waitUntil?: (promise: PromiseLike<unknown>) => void;
|
|
26
30
|
}
|
|
27
31
|
interface ExecuteStaticScheduleOptions {
|
|
28
32
|
cron: string;
|
|
29
33
|
definition: ScheduleDefinition;
|
|
30
34
|
name: string;
|
|
31
35
|
scheduledAt?: Date;
|
|
36
|
+
waitUntil?: (promise: PromiseLike<unknown>) => void;
|
|
32
37
|
}
|
|
33
38
|
interface ExecuteRuntimeScheduleOptions {
|
|
34
39
|
id: string;
|
|
40
|
+
requireDue?: boolean;
|
|
35
41
|
runtimeScheduleStore?: RuntimeScheduleStore;
|
|
36
42
|
scheduledAt?: Date;
|
|
37
43
|
scheduleRunStore?: ScheduleRunStore;
|
|
44
|
+
waitUntil?: (promise: PromiseLike<unknown>) => void;
|
|
38
45
|
}
|
|
39
|
-
declare function createScheduleRun(options: Omit<ExecuteScheduleOptions, "definition">): Promise<ScheduleRunRecord>;
|
|
46
|
+
declare function createScheduleRun(options: Omit<ExecuteScheduleOptions, "definition" | "waitUntil">): Promise<ScheduleRunRecord>;
|
|
40
47
|
declare function executeSchedule(options: ExecuteScheduleOptions): Promise<ScheduleRunRecord>;
|
|
41
48
|
declare function executeStaticSchedule(options: ExecuteStaticScheduleOptions): Promise<ScheduleRunRecord>;
|
|
42
49
|
declare function executeRuntimeSchedule(options: ExecuteRuntimeScheduleOptions | string): Promise<ScheduleRunRecord>;
|
|
43
50
|
//#endregion
|
|
44
|
-
//#region src/
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
declare function startScheduleRunner(options?: ScheduleRunnerOptions): ScheduleRunnerController;
|
|
51
|
+
//#region src/errors.d.ts
|
|
52
|
+
declare const scheduleErrorCodes: readonly ["SCHEDULE_ALREADY_EXISTS", "SCHEDULE_DISABLED", "SCHEDULE_INVALID_CRON", "SCHEDULE_INVALID_ENABLED", "SCHEDULE_INVALID_ID", "SCHEDULE_INVALID_INPUT", "SCHEDULE_INVALID_SCHEDULED_AT", "SCHEDULE_INVALID_TARGET", "SCHEDULE_INVALID_TIME_ZONE", "SCHEDULE_NOT_DUE", "SCHEDULE_NOT_FOUND", "SCHEDULE_RUN_NOT_FOUND", "SCHEDULE_TARGET_NOT_ELIGIBLE", "SCHEDULE_TARGET_NOT_FOUND"];
|
|
53
|
+
declare const scheduleErrorFields: readonly ["cron", "enabled", "id", "input", "options", "scheduledAt", "target", "timeZone"];
|
|
54
|
+
declare const scheduleErrorValueTypes: readonly ["array", "bigint", "boolean", "date", "function", "null", "number", "object", "string", "symbol", "undefined", "unknown"];
|
|
55
|
+
type ScheduleErrorCode = typeof scheduleErrorCodes[number];
|
|
56
|
+
type ScheduleErrorField = typeof scheduleErrorFields[number];
|
|
57
|
+
type ScheduleErrorValueType = typeof scheduleErrorValueTypes[number];
|
|
58
|
+
type ScheduleValidationErrorCode = Extract<ScheduleErrorCode, `SCHEDULE_INVALID_${string}`>;
|
|
59
|
+
type ScheduleErrorDetails = {
|
|
60
|
+
field: ScheduleErrorField;
|
|
61
|
+
valueType: ScheduleErrorValueType;
|
|
62
|
+
};
|
|
58
63
|
//#endregion
|
|
59
|
-
export { type DiscoveredScheduleDefinition, type KVScheduleStoreOptions, type RuntimeScheduleCreateInput, type RuntimeScheduleMetadata, type RuntimeScheduleRecord, type RuntimeScheduleStore, type RuntimeScheduleUpdateInput, type ScheduleDefinition, type ScheduleDefinitionInput, type ScheduleDefinitionRegistry,
|
|
64
|
+
export { type DiscoveredScheduleDefinition, type KVScheduleStoreOptions, type RuntimeScheduleCreateInput, type RuntimeScheduleMetadata, type RuntimeScheduleRecord, type RuntimeScheduleStore, type RuntimeScheduleUpdateInput, type ScheduleDefinition, type ScheduleDefinitionInput, type ScheduleDefinitionRegistry, type ScheduleErrorCode, type ScheduleErrorDetails, type ScheduleErrorField, type ScheduleErrorValueType, type ScheduleHandler, type ScheduleKVStorage, type ScheduleRegistryDefinition, type ScheduleRunAttemptRecord, type ScheduleRunAttemptStatus, type ScheduleRunContext, type ScheduleRunError, type ScheduleRunRecord, type ScheduleRunStatus, type ScheduleRunStore, type ScheduleTargetDefinition, type ScheduleTargetDefinitionInput, type ScheduleTargetName, type ScheduleValidationErrorCode, createKVRuntimeScheduleStore, createKVScheduleRunStore, createMemoryRuntimeScheduleStore, createMemoryScheduleRunStore, createScheduleKVStorage, createScheduleRun, defineSchedule, defineScheduleTarget, discoverScheduleDefinitions, executeRuntimeSchedule, executeSchedule, executeStaticSchedule, getRuntimeScheduleStore, getScheduleRunStore, getScheduleRuntimeRegistry, loadScheduleDefinition, resetScheduleRuntime, schedules, setRuntimeScheduleStore, setScheduleRunStore, setScheduleRuntimeRegistry, validateRuntimeScheduleCron };
|
package/dist/index.js
CHANGED
|
@@ -1,152 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { a as
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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 };
|
|
1
|
+
import { defineSchedule, defineScheduleTarget } from "./definition.js";
|
|
2
|
+
import { t as discoverScheduleDefinitions } from "./discovery-BZlat3Kc.js";
|
|
3
|
+
import { a as executeStaticSchedule, i as executeSchedule, n as executeRuntimeSchedule, t as createScheduleRun } from "./execute-ClFYvmp9.js";
|
|
4
|
+
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, r as getScheduleRuntimeRegistry, s as setScheduleRunStore, t as getRuntimeScheduleStore, u as createKVScheduleRunStore } from "./state-By7WU_ks.js";
|
|
5
|
+
import { n as validateRuntimeScheduleCron, t as schedules } from "./client-BMFCy0dX.js";
|
|
6
|
+
import { t as createScheduleKVStorage } from "./kv-storage-DWinmLLy.js";
|
|
7
|
+
export { createKVRuntimeScheduleStore, createKVScheduleRunStore, createMemoryRuntimeScheduleStore, createMemoryScheduleRunStore, createScheduleKVStorage, createScheduleRun, defineSchedule, defineScheduleTarget, discoverScheduleDefinitions, executeRuntimeSchedule, executeSchedule, executeStaticSchedule, getRuntimeScheduleStore, getScheduleRunStore, getScheduleRuntimeRegistry, loadScheduleDefinition, resetScheduleRuntime, schedules, setRuntimeScheduleStore, setScheduleRunStore, setScheduleRuntimeRegistry, validateRuntimeScheduleCron };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//#region src/runtime/kv-storage.ts
|
|
2
|
+
function createScheduleKVStorage(kvStore) {
|
|
3
|
+
return {
|
|
4
|
+
async del(key) {
|
|
5
|
+
const [error] = await kvStore.del(key);
|
|
6
|
+
if (error) throw error;
|
|
7
|
+
},
|
|
8
|
+
async get(key) {
|
|
9
|
+
const [error, value] = await kvStore.get(key);
|
|
10
|
+
if (error) throw error;
|
|
11
|
+
return value;
|
|
12
|
+
},
|
|
13
|
+
async has(key) {
|
|
14
|
+
const [error, value] = await kvStore.has(key);
|
|
15
|
+
if (error) throw error;
|
|
16
|
+
return value ?? false;
|
|
17
|
+
},
|
|
18
|
+
async keys(base) {
|
|
19
|
+
const [error, value] = await kvStore.keys(base);
|
|
20
|
+
if (error) throw error;
|
|
21
|
+
return value ?? [];
|
|
22
|
+
},
|
|
23
|
+
async set(key, value) {
|
|
24
|
+
const [error] = await kvStore.set(key, value);
|
|
25
|
+
if (error) throw error;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
export { createScheduleKVStorage as t };
|
package/dist/nuxt.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ScheduleVitePluginOptions } from "./vite.js";
|
|
2
|
+
|
|
3
|
+
//#region src/nuxt.d.ts
|
|
4
|
+
interface ScheduleNuxtModuleOptions extends ScheduleVitePluginOptions {}
|
|
5
|
+
type NuxtLike = {
|
|
6
|
+
hook?: (name: "nitro:config", handler: (nitroConfig: Record<string, unknown>) => void | Promise<void>) => void;
|
|
7
|
+
options: {
|
|
8
|
+
dev?: boolean;
|
|
9
|
+
nitro?: Record<string, unknown>;
|
|
10
|
+
rootDir?: string;
|
|
11
|
+
srcDir?: string;
|
|
12
|
+
vite?: {
|
|
13
|
+
plugins?: unknown[];
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
declare function viteHubScheduleNuxtModule(options?: ScheduleNuxtModuleOptions, nuxt?: NuxtLike): void;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { ScheduleNuxtModuleOptions, viteHubScheduleNuxtModule as default };
|
package/dist/nuxt.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { n as hubSchedule, t as createScheduleNitroConfig } from "./vite-BX6_KiRw.js";
|
|
2
|
+
//#region src/nuxt.ts
|
|
3
|
+
function isScheduleVitePlugin(value) {
|
|
4
|
+
return Boolean(value && typeof value === "object" && value.name === "@vite-hub/schedule/vite");
|
|
5
|
+
}
|
|
6
|
+
function viteHubScheduleNuxtModule(options = {}, nuxt) {
|
|
7
|
+
if (!nuxt) return;
|
|
8
|
+
nuxt.options.vite ??= {};
|
|
9
|
+
const plugins = Array.isArray(nuxt.options.vite.plugins) ? nuxt.options.vite.plugins : [];
|
|
10
|
+
if (!plugins.some(isScheduleVitePlugin)) plugins.push(hubSchedule(options));
|
|
11
|
+
nuxt.options.vite.plugins = plugins;
|
|
12
|
+
nuxt.hook?.("nitro:config", async (nitroConfig) => {
|
|
13
|
+
const rootDir = nuxt.options.rootDir || process.cwd();
|
|
14
|
+
const nitro = await createScheduleNitroConfig({
|
|
15
|
+
...options,
|
|
16
|
+
command: nuxt.options.dev ? "serve" : "build",
|
|
17
|
+
nitro: nitroConfig,
|
|
18
|
+
projectRoot: options.projectRoot || rootDir,
|
|
19
|
+
root: nuxt.options.srcDir || rootDir
|
|
20
|
+
});
|
|
21
|
+
if (nitro) Object.assign(nitroConfig, nitro);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
export { viteHubScheduleNuxtModule as default };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { a as RuntimeScheduleStore, b as ScheduleRunStore, d as ScheduleDefinitionRegistry, i as RuntimeScheduleRecord, s as RuntimeScheduleWake } from "../types-Bcje4dLV.js";
|
|
2
|
+
|
|
3
|
+
//#region src/runtime/driver.d.ts
|
|
4
|
+
interface RuntimeScheduleWakeDriverContext {
|
|
5
|
+
reportError(error: unknown): void;
|
|
6
|
+
wake(input: RuntimeScheduleWake): Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
interface RuntimeScheduleWakeDriver {
|
|
9
|
+
close?(): Promise<void> | void;
|
|
10
|
+
reconcile(schedules: readonly RuntimeScheduleRecord[]): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
type RuntimeScheduleWakeDriverFactory = (context: RuntimeScheduleWakeDriverContext) => Promise<RuntimeScheduleWakeDriver> | RuntimeScheduleWakeDriver;
|
|
13
|
+
interface InstallScheduleRuntimeOptions {
|
|
14
|
+
createDriver: RuntimeScheduleWakeDriverFactory;
|
|
15
|
+
onError?: (error: unknown) => void;
|
|
16
|
+
registry: ScheduleDefinitionRegistry;
|
|
17
|
+
runtimeScheduleStore: RuntimeScheduleStore;
|
|
18
|
+
scheduleRunStore: ScheduleRunStore;
|
|
19
|
+
staticRegistry?: ScheduleDefinitionRegistry;
|
|
20
|
+
}
|
|
21
|
+
interface ScheduleRuntimeController {
|
|
22
|
+
close(): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
declare function installScheduleRuntime(options: InstallScheduleRuntimeOptions): Promise<ScheduleRuntimeController>;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { InstallScheduleRuntimeOptions, type RuntimeScheduleWake, RuntimeScheduleWakeDriver, RuntimeScheduleWakeDriverContext, RuntimeScheduleWakeDriverFactory, ScheduleRuntimeController, installScheduleRuntime };
|