@vite-hub/schedule 0.0.3 → 0.1.0
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/LICENSE +201 -0
- package/README.md +199 -65
- package/dist/definition-CMiOg1lg.js +111 -0
- package/dist/definition.d.ts +4 -6
- package/dist/definition.js +2 -42
- package/dist/error-diagnostics-A_8Kp-48.js +116 -0
- package/dist/error-diagnostics-in7mif5s.js +45 -0
- package/dist/execute-BJrgNgQ0.js +201 -0
- package/dist/index.d.ts +7 -51
- package/dist/index.js +2 -7
- package/dist/inspect-DTfAo5gS.js +118 -0
- package/dist/kv-storage-Bv9ocZT-.d.ts +18 -0
- package/dist/nuxt.d.ts +4 -6
- package/dist/nuxt.js +3 -1
- package/dist/operations-gk2qzhYV.js +131 -0
- package/dist/operations-tNUdj3G4.d.ts +212 -0
- package/dist/operations.d.ts +61 -0
- package/dist/operations.js +921 -0
- package/dist/registry-module.d.ts +0 -5
- package/dist/runtime/kv.d.ts +4 -0
- package/dist/{kv-BJ1I9IlJ.js → runtime/kv.js} +1 -1
- package/dist/runtime/process.d.ts +26 -5
- package/dist/runtime/process.js +337 -75
- package/dist/runtime/state.d.ts +8 -12
- package/dist/runtime/state.js +2 -2
- package/dist/runtime/static.d.ts +18 -12
- package/dist/runtime/static.js +2 -138
- package/dist/runtime.d.ts +36 -4
- package/dist/runtime.js +5 -3
- package/dist/state-DSxmHPn3.js +448 -0
- package/dist/static-6g82xguw.js +245 -0
- package/dist/store-QAOd8bxm.d.ts +11 -0
- package/dist/types-CHdV9FQU.d.ts +113 -0
- package/dist/vite-BAVyZ8hZ.d.ts +474 -0
- package/dist/vite-BJwZkMrn.js +5193 -0
- package/dist/vite.d.ts +2 -38
- package/dist/vite.js +2 -2
- package/dist/wait-until-BmIqRo4x.js +87 -0
- package/package.json +68 -22
- package/dist/client-BMFCy0dX.js +0 -233
- package/dist/discovery-BZlat3Kc.js +0 -776
- package/dist/due-Bbh4yIxq.js +0 -59
- package/dist/execute-ClFYvmp9.js +0 -211
- package/dist/runtime/driver.d.ts +0 -26
- package/dist/runtime/driver.js +0 -334
- package/dist/state-By7WU_ks.js +0 -472
- package/dist/store-t-74rsM_.d.ts +0 -48
- package/dist/types-Bcje4dLV.d.ts +0 -136
- package/dist/vite-BX6_KiRw.js +0 -1218
- package/dist/wait-until-CR-JjIOb.js +0 -23
package/dist/due-Bbh4yIxq.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
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 };
|
package/dist/execute-ClFYvmp9.js
DELETED
|
@@ -1,211 +0,0 @@
|
|
|
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/runtime/driver.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
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 };
|
package/dist/runtime/driver.js
DELETED
|
@@ -1,334 +0,0 @@
|
|
|
1
|
-
import { a as executeStaticSchedule, r as executeRuntimeScheduleWake } from "../execute-ClFYvmp9.js";
|
|
2
|
-
import { c as setScheduleRuntimeRegistry, m as createScheduleError, n as getScheduleRunStore, o as setRuntimeScheduleStore, r as getScheduleRuntimeRegistry, s as setScheduleRunStore, t as getRuntimeScheduleStore } from "../state-By7WU_ks.js";
|
|
3
|
-
import { t as isRuntimeScheduleDue } from "../due-Bbh4yIxq.js";
|
|
4
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
5
|
-
//#region src/runtime/driver.ts
|
|
6
|
-
const staticScheduleIdPrefix = "\0vitehub:static:";
|
|
7
|
-
function isScheduleRegistryDefinition(value) {
|
|
8
|
-
return !!value && typeof value === "object" && "handler" in value;
|
|
9
|
-
}
|
|
10
|
-
function unwrapDefinition(loaded) {
|
|
11
|
-
if ("default" in loaded && isScheduleRegistryDefinition(loaded.default)) return loaded.default;
|
|
12
|
-
if (isScheduleRegistryDefinition(loaded)) return loaded;
|
|
13
|
-
}
|
|
14
|
-
async function loadStaticDefinitions(registry) {
|
|
15
|
-
if (!registry) return [];
|
|
16
|
-
return (await Promise.all(Object.entries(registry).map(async ([name, load]) => {
|
|
17
|
-
const definition = unwrapDefinition(await load());
|
|
18
|
-
return definition && "cron" in definition ? {
|
|
19
|
-
definition,
|
|
20
|
-
name
|
|
21
|
-
} : void 0;
|
|
22
|
-
}))).filter((definition) => definition !== void 0);
|
|
23
|
-
}
|
|
24
|
-
function createStaticSchedules(definitions, runtimeSchedules) {
|
|
25
|
-
const occupiedIds = new Set(runtimeSchedules.map((schedule) => schedule.id));
|
|
26
|
-
const byId = /* @__PURE__ */ new Map();
|
|
27
|
-
const timestamp = /* @__PURE__ */ new Date(0);
|
|
28
|
-
return {
|
|
29
|
-
byId,
|
|
30
|
-
records: definitions.map((entry) => {
|
|
31
|
-
const baseId = `${staticScheduleIdPrefix}${encodeURIComponent(entry.name)}`;
|
|
32
|
-
let id = baseId;
|
|
33
|
-
let suffix = 2;
|
|
34
|
-
while (occupiedIds.has(id)) id = `${baseId}:${suffix++}`;
|
|
35
|
-
occupiedIds.add(id);
|
|
36
|
-
const record = {
|
|
37
|
-
createdAt: timestamp,
|
|
38
|
-
cron: entry.definition.cron,
|
|
39
|
-
enabled: true,
|
|
40
|
-
id,
|
|
41
|
-
target: entry.name,
|
|
42
|
-
updatedAt: timestamp
|
|
43
|
-
};
|
|
44
|
-
byId.set(id, {
|
|
45
|
-
...entry,
|
|
46
|
-
record
|
|
47
|
-
});
|
|
48
|
-
return record;
|
|
49
|
-
})
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
function createSerializer() {
|
|
53
|
-
let tail = Promise.resolve();
|
|
54
|
-
const operationStorage = new AsyncLocalStorage();
|
|
55
|
-
const serialize = function serialize(operation) {
|
|
56
|
-
const activeOperation = operationStorage.getStore();
|
|
57
|
-
if (activeOperation?.active) return (async () => {
|
|
58
|
-
activeOperation.depth++;
|
|
59
|
-
try {
|
|
60
|
-
return await operation();
|
|
61
|
-
} finally {
|
|
62
|
-
activeOperation.depth--;
|
|
63
|
-
}
|
|
64
|
-
})();
|
|
65
|
-
const operationContext = {
|
|
66
|
-
active: true,
|
|
67
|
-
deferred: [],
|
|
68
|
-
depth: 0,
|
|
69
|
-
wakeReleases: /* @__PURE__ */ new Set()
|
|
70
|
-
};
|
|
71
|
-
const result = tail.then(() => operationStorage.run(operationContext, async () => {
|
|
72
|
-
try {
|
|
73
|
-
const value = await operation();
|
|
74
|
-
while (operationContext.deferred.length > 0) await operationContext.deferred.shift()();
|
|
75
|
-
return value;
|
|
76
|
-
} finally {
|
|
77
|
-
operationContext.active = false;
|
|
78
|
-
}
|
|
79
|
-
}));
|
|
80
|
-
tail = result.then(() => {}, () => {});
|
|
81
|
-
return result;
|
|
82
|
-
};
|
|
83
|
-
serialize.defer = (operation) => {
|
|
84
|
-
const activeOperation = operationStorage.getStore();
|
|
85
|
-
if (!activeOperation?.active) throw new Error("Cannot defer work outside an active serialized operation.");
|
|
86
|
-
activeOperation.deferred.push(operation);
|
|
87
|
-
for (const release of activeOperation.wakeReleases) release();
|
|
88
|
-
};
|
|
89
|
-
serialize.isReentrant = () => {
|
|
90
|
-
const activeOperation = operationStorage.getStore();
|
|
91
|
-
return Boolean(activeOperation?.active && activeOperation.depth > 0);
|
|
92
|
-
};
|
|
93
|
-
serialize.runWake = async (operation) => {
|
|
94
|
-
const activeOperation = operationStorage.getStore();
|
|
95
|
-
if (!activeOperation?.active) {
|
|
96
|
-
await operation();
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
let release;
|
|
100
|
-
let released = false;
|
|
101
|
-
const releasePromise = new Promise((resolve) => {
|
|
102
|
-
release = () => {
|
|
103
|
-
released = true;
|
|
104
|
-
resolve();
|
|
105
|
-
};
|
|
106
|
-
});
|
|
107
|
-
activeOperation.wakeReleases.add(release);
|
|
108
|
-
let execution;
|
|
109
|
-
try {
|
|
110
|
-
execution = Promise.resolve(operation());
|
|
111
|
-
} catch (error) {
|
|
112
|
-
execution = Promise.reject(error);
|
|
113
|
-
}
|
|
114
|
-
try {
|
|
115
|
-
await Promise.race([execution, releasePromise]);
|
|
116
|
-
if (released) activeOperation.deferred.push(async () => {
|
|
117
|
-
await execution;
|
|
118
|
-
});
|
|
119
|
-
} finally {
|
|
120
|
-
activeOperation.wakeReleases.delete(release);
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
return serialize;
|
|
124
|
-
}
|
|
125
|
-
function createErrorReporter(onError) {
|
|
126
|
-
return (error) => {
|
|
127
|
-
if (!onError) return;
|
|
128
|
-
try {
|
|
129
|
-
Promise.resolve(onError(error)).catch(() => {});
|
|
130
|
-
} catch {}
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
async function reconcileAfterMutation(driver, store, rollback, reportError) {
|
|
134
|
-
try {
|
|
135
|
-
await driver.reconcile(await store.list());
|
|
136
|
-
} catch (error) {
|
|
137
|
-
try {
|
|
138
|
-
await rollback();
|
|
139
|
-
} catch (rollbackError) {
|
|
140
|
-
throw new AggregateError([error, rollbackError], "Runtime Schedule reconciliation failed and the stored record could not be rolled back.");
|
|
141
|
-
}
|
|
142
|
-
try {
|
|
143
|
-
await driver.reconcile(await store.list());
|
|
144
|
-
} catch (rollbackReconcileError) {
|
|
145
|
-
reportError(rollbackReconcileError);
|
|
146
|
-
}
|
|
147
|
-
throw error;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
function deferReentrantReconciliation(driver, store, rollback, reportError, serialize) {
|
|
151
|
-
if (!serialize.isReentrant()) return;
|
|
152
|
-
let rejectReconciliation;
|
|
153
|
-
let resolveReconciliation;
|
|
154
|
-
const reconciliation = new Promise((resolve, reject) => {
|
|
155
|
-
rejectReconciliation = reject;
|
|
156
|
-
resolveReconciliation = resolve;
|
|
157
|
-
});
|
|
158
|
-
serialize.defer(async () => {
|
|
159
|
-
try {
|
|
160
|
-
await reconcileAfterMutation(driver, store, rollback, reportError);
|
|
161
|
-
resolveReconciliation();
|
|
162
|
-
} catch (error) {
|
|
163
|
-
rejectReconciliation(error);
|
|
164
|
-
}
|
|
165
|
-
});
|
|
166
|
-
return reconciliation;
|
|
167
|
-
}
|
|
168
|
-
function createReconciledStore(store, getDriver, reportError, serialize) {
|
|
169
|
-
return {
|
|
170
|
-
create(record) {
|
|
171
|
-
return serialize(async () => {
|
|
172
|
-
const driver = getDriver();
|
|
173
|
-
const created = await store.create(record);
|
|
174
|
-
const rollback = async () => {
|
|
175
|
-
if (!await store.delete(created.id)) throw new Error(`Runtime Schedule create rollback failed: ${created.id}`);
|
|
176
|
-
};
|
|
177
|
-
const deferred = deferReentrantReconciliation(driver, store, rollback, reportError, serialize);
|
|
178
|
-
if (deferred) await deferred;
|
|
179
|
-
else await reconcileAfterMutation(driver, store, rollback, reportError);
|
|
180
|
-
return created;
|
|
181
|
-
});
|
|
182
|
-
},
|
|
183
|
-
delete(id) {
|
|
184
|
-
return serialize(async () => {
|
|
185
|
-
const driver = getDriver();
|
|
186
|
-
const previous = await store.get(id);
|
|
187
|
-
if (!await store.delete(id)) return false;
|
|
188
|
-
const rollback = async () => {
|
|
189
|
-
if (!previous) throw new Error(`Runtime Schedule delete rollback failed: ${id}`);
|
|
190
|
-
await store.create(previous);
|
|
191
|
-
};
|
|
192
|
-
const deferred = deferReentrantReconciliation(driver, store, rollback, reportError, serialize);
|
|
193
|
-
if (deferred) await deferred;
|
|
194
|
-
else await reconcileAfterMutation(driver, store, rollback, reportError);
|
|
195
|
-
return true;
|
|
196
|
-
});
|
|
197
|
-
},
|
|
198
|
-
get(id) {
|
|
199
|
-
return store.get(id);
|
|
200
|
-
},
|
|
201
|
-
list() {
|
|
202
|
-
return store.list();
|
|
203
|
-
},
|
|
204
|
-
update(id, patch) {
|
|
205
|
-
return serialize(async () => {
|
|
206
|
-
const driver = getDriver();
|
|
207
|
-
const previous = await store.get(id);
|
|
208
|
-
const updated = await store.update(id, patch);
|
|
209
|
-
if (!updated) return void 0;
|
|
210
|
-
const rollback = async () => {
|
|
211
|
-
if (!previous) {
|
|
212
|
-
if (!await store.delete(id)) throw new Error(`Runtime Schedule update rollback failed: ${id}`);
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
if (!await store.delete(id)) throw new Error(`Runtime Schedule update rollback failed: ${id}`);
|
|
216
|
-
await store.create(previous);
|
|
217
|
-
};
|
|
218
|
-
const deferred = deferReentrantReconciliation(driver, store, rollback, reportError, serialize);
|
|
219
|
-
if (deferred) await deferred;
|
|
220
|
-
else await reconcileAfterMutation(driver, store, rollback, reportError);
|
|
221
|
-
return updated;
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
async function installScheduleRuntime(options) {
|
|
227
|
-
const previousRegistry = getScheduleRuntimeRegistry();
|
|
228
|
-
const previousRuntimeScheduleStore = getRuntimeScheduleStore();
|
|
229
|
-
const previousScheduleRunStore = getScheduleRunStore();
|
|
230
|
-
const reportError = createErrorReporter(options.onError);
|
|
231
|
-
const pendingWork = /* @__PURE__ */ new Set();
|
|
232
|
-
function waitUntil(value) {
|
|
233
|
-
const promise = Promise.resolve(value);
|
|
234
|
-
pendingWork.add(promise);
|
|
235
|
-
promise.then(() => pendingWork.delete(promise), (error) => {
|
|
236
|
-
pendingWork.delete(promise);
|
|
237
|
-
reportError(error);
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
async function flushWaitUntil() {
|
|
241
|
-
while (pendingWork.size > 0) await Promise.allSettled([...pendingWork]);
|
|
242
|
-
}
|
|
243
|
-
const serialize = createSerializer();
|
|
244
|
-
const activeWakes = /* @__PURE__ */ new Set();
|
|
245
|
-
let driver;
|
|
246
|
-
let reconciledDriver;
|
|
247
|
-
let staticSchedules = {
|
|
248
|
-
byId: /* @__PURE__ */ new Map(),
|
|
249
|
-
records: []
|
|
250
|
-
};
|
|
251
|
-
let aborted = false;
|
|
252
|
-
let closing = false;
|
|
253
|
-
setScheduleRuntimeRegistry(options.registry);
|
|
254
|
-
setScheduleRunStore(options.scheduleRunStore);
|
|
255
|
-
const initializing = serialize(async () => {
|
|
256
|
-
try {
|
|
257
|
-
const runtimeSchedules = await options.runtimeScheduleStore.list();
|
|
258
|
-
staticSchedules = createStaticSchedules(await loadStaticDefinitions(options.staticRegistry), runtimeSchedules);
|
|
259
|
-
driver = await options.createDriver({
|
|
260
|
-
reportError,
|
|
261
|
-
wake(input) {
|
|
262
|
-
if (closing) return Promise.resolve();
|
|
263
|
-
const wake = serialize.runWake(async () => {
|
|
264
|
-
const staticSchedule = staticSchedules.byId.get(input.scheduleId);
|
|
265
|
-
if (staticSchedule) {
|
|
266
|
-
if (!isRuntimeScheduleDue(staticSchedule.record, input.scheduledAt)) throw createScheduleError("SCHEDULE_NOT_DUE");
|
|
267
|
-
await executeStaticSchedule({
|
|
268
|
-
cron: staticSchedule.definition.cron,
|
|
269
|
-
definition: staticSchedule.definition,
|
|
270
|
-
name: staticSchedule.name,
|
|
271
|
-
scheduledAt: input.scheduledAt,
|
|
272
|
-
waitUntil
|
|
273
|
-
});
|
|
274
|
-
return;
|
|
275
|
-
}
|
|
276
|
-
await executeRuntimeScheduleWake(input, {
|
|
277
|
-
runtimeScheduleStore: options.runtimeScheduleStore,
|
|
278
|
-
scheduleRunStore: options.scheduleRunStore,
|
|
279
|
-
waitUntil
|
|
280
|
-
});
|
|
281
|
-
});
|
|
282
|
-
activeWakes.add(wake);
|
|
283
|
-
wake.finally(() => activeWakes.delete(wake)).catch(() => {});
|
|
284
|
-
return wake;
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
const installedDriver = driver;
|
|
288
|
-
reconciledDriver = {
|
|
289
|
-
close: () => installedDriver.close?.(),
|
|
290
|
-
async reconcile(records) {
|
|
291
|
-
const conflictingId = records.find((record) => staticSchedules.byId.has(record.id))?.id;
|
|
292
|
-
if (conflictingId) throw new Error(`Runtime Schedule id conflicts with a Static Schedule driver identity: ${JSON.stringify(conflictingId)}`);
|
|
293
|
-
await installedDriver.reconcile([...staticSchedules.records, ...records]);
|
|
294
|
-
}
|
|
295
|
-
};
|
|
296
|
-
await reconciledDriver.reconcile(runtimeSchedules);
|
|
297
|
-
} catch (error) {
|
|
298
|
-
aborted = true;
|
|
299
|
-
throw error;
|
|
300
|
-
}
|
|
301
|
-
});
|
|
302
|
-
setRuntimeScheduleStore(createReconciledStore(options.runtimeScheduleStore, () => {
|
|
303
|
-
if (!reconciledDriver || aborted) throw new Error("Runtime Schedule wake driver installation did not complete.");
|
|
304
|
-
return reconciledDriver;
|
|
305
|
-
}, reportError, serialize));
|
|
306
|
-
try {
|
|
307
|
-
await initializing;
|
|
308
|
-
} catch (error) {
|
|
309
|
-
try {
|
|
310
|
-
await driver?.close?.();
|
|
311
|
-
} catch (closeError) {
|
|
312
|
-
reportError(closeError);
|
|
313
|
-
}
|
|
314
|
-
setScheduleRuntimeRegistry(previousRegistry);
|
|
315
|
-
setRuntimeScheduleStore(previousRuntimeScheduleStore);
|
|
316
|
-
setScheduleRunStore(previousScheduleRunStore);
|
|
317
|
-
throw error;
|
|
318
|
-
}
|
|
319
|
-
if (!reconciledDriver) throw new Error("Runtime Schedule wake driver installation did not produce a driver.");
|
|
320
|
-
const installedDriver = reconciledDriver;
|
|
321
|
-
let closePromise;
|
|
322
|
-
return { close() {
|
|
323
|
-
if (closePromise) return closePromise;
|
|
324
|
-
closing = true;
|
|
325
|
-
return closePromise = (async () => {
|
|
326
|
-
while (activeWakes.size > 0) await Promise.allSettled([...activeWakes]);
|
|
327
|
-
await serialize(async () => {});
|
|
328
|
-
await flushWaitUntil();
|
|
329
|
-
await installedDriver.close?.();
|
|
330
|
-
})();
|
|
331
|
-
} };
|
|
332
|
-
}
|
|
333
|
-
//#endregion
|
|
334
|
-
export { installScheduleRuntime };
|