@vite-hub/schedule 0.3.1 → 0.4.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.
Files changed (34) hide show
  1. package/README.md +14 -14
  2. package/dist/definition.js +1 -1
  3. package/dist/{due-BVMRQ93v.js → due-sOJvF_6Z.js} +1 -1
  4. package/dist/{error-diagnostics-Mpeks7J0.js → error-diagnostics-BBtYYl8j.js} +4 -0
  5. package/dist/{error-diagnostics-DSkCJC_L.js → error-diagnostics-CQ4BUqRe.js} +5 -1
  6. package/dist/index.d.ts +19 -5
  7. package/dist/index.js +34 -5
  8. package/dist/{kv-storage-iJChCS5A.js → kv-storage-2kZtP2CC.js} +2 -2
  9. package/dist/{kv-storage-C0KZZB6v.d.ts → kv-storage-Bm9aYLfZ.d.ts} +5 -1
  10. package/dist/{operation-definition-8gOe-ubD.d.ts → operation-definition-nmRu_O3V.d.ts} +5 -0
  11. package/dist/{operations-DwZWFIUX.js → operations-DqwmJ8MN.js} +29 -6
  12. package/dist/operations.d.ts +10 -1
  13. package/dist/operations.js +10 -7
  14. package/dist/os.js +1 -1
  15. package/dist/runtime/console.js +1 -1
  16. package/dist/runtime/kv.d.ts +1 -1
  17. package/dist/runtime/kv.js +1 -1
  18. package/dist/runtime/process.js +5 -5
  19. package/dist/runtime/state.d.ts +2 -2
  20. package/dist/runtime/state.js +2 -2
  21. package/dist/runtime/static.js +1 -1
  22. package/dist/runtime.d.ts +3 -3
  23. package/dist/runtime.js +5 -5
  24. package/dist/{started-Bx7MDbrl.js → started-B0pc3JY0.js} +2 -2
  25. package/dist/{started-BuGmnus4.js → started-Bk2Icknk.js} +3 -3
  26. package/dist/{started-errors-p7cu_rI7.js → started-errors-DEmYH-vp.js} +30 -23
  27. package/dist/{inspect-DTfAo5gS.js → started-name-ruCM1EtQ.js} +5 -1
  28. package/dist/{state-Do2wV4Tx.d.ts → state-CPd10SXq.d.ts} +10 -1
  29. package/dist/{state-B-Fuyfb4.js → state-D4SZH6zk.js} +10 -2
  30. package/dist/{static-DQwzzvPg.js → static-7JCga3Iu.js} +21 -6
  31. package/dist/{store-Df0-fzCl.d.ts → store-DeMojnXM.d.ts} +1 -1
  32. package/dist/vite.d.ts +1 -1
  33. package/dist/vite.js +714 -462
  34. package/package.json +6 -6
@@ -1,7 +1,8 @@
1
1
  import { n as isRecordOrFunction } from "./object-Da1zyPCR.js";
2
2
  import { a as validateScheduledAt, i as scheduleIdempotencyKey, o as createScheduleError, r as parseScheduledAt, t as runWithScheduleWaitUntil } from "./wait-until-Dy4Bn3ZN.js";
3
- import { r as getScheduleRunStore } from "./state-B-Fuyfb4.js";
3
+ import { i as getScheduleRunStore } from "./state-D4SZH6zk.js";
4
4
  import { defineErrors, serializeResponse, toResponse } from "@vite-hub/runtime";
5
+ import { emitRunFailed, toRunFailedError } from "@vite-hub/runtime/internal/run-failed";
5
6
  import { runInRunScopes } from "@vite-hub/runtime/internal/run-scope";
6
7
  //#region ../internal/src/runtime/random.ts
7
8
  function randomId(prefix) {
@@ -165,14 +166,23 @@ async function executeSchedule(options) {
165
166
  }
166
167
  const attempt = await startAttempt(run, runStore);
167
168
  options.onExecution?.();
169
+ let response;
168
170
  try {
169
171
  const value = await runInRunScopes(() => runWithScheduleWaitUntil((waitUntil) => options.definition.handler(toHandlerContext(run, attempt, waitUntil)), options.waitUntil));
170
- return await completeRun(run, attempt, await serializeResponse(toResponse(value)), runStore);
172
+ response = await serializeResponse(toResponse(value));
171
173
  } catch (error) {
172
174
  const failed = await failRun(run, attempt, error, runStore);
175
+ await emitRunFailed({
176
+ event: "schedule.failed",
177
+ schedule: failed.scheduleId,
178
+ runId: failed.id,
179
+ error: toRunFailedError(error),
180
+ failedAt: (failed.completedAt ?? /* @__PURE__ */ new Date()).toISOString()
181
+ });
173
182
  if (options.captureHandlerFailure) return failed;
174
183
  throw error;
175
184
  }
185
+ return await completeRun(run, attempt, response, runStore);
176
186
  }
177
187
  async function executeStaticSchedule(options) {
178
188
  return await executeSchedule({
@@ -195,29 +205,26 @@ async function loadStaticScheduleDefinition(name, registry) {
195
205
  const record = isRecordOrFunction(loaded) ? loaded : void 0;
196
206
  return record && isStaticScheduleDefinition(record) ? record : record && Object.hasOwn(record, "default") && isStaticScheduleDefinition(record.default) ? record.default : void 0;
197
207
  }
198
- /** Runs a manually dispatchable static Schedule Definition of `registry` immediately. */
199
- async function runScheduleFrom(registry, name, options = {}) {
208
+ async function resolveManualSchedule(registry, name) {
200
209
  const definition = typeof name === "string" && name ? await loadStaticScheduleDefinition(name, registry) : void 0;
201
210
  if (!definition) throw createScheduleError("SCHEDULE_DEFINITION_NOT_FOUND");
202
211
  if (definition.options?.manual !== true) throw createScheduleError("SCHEDULE_MANUAL_RUN_DISABLED");
203
- const scheduledAt = parseScheduledAt(options.scheduledAt);
204
- const runStore = getScheduleRunStore();
205
- const runIdSuffix = randomId("manual");
206
- try {
207
- return await executeSchedule({
208
- definition,
209
- runIdSuffix,
210
- runStore,
211
- scheduleId: name,
212
- source: "manual",
213
- scheduledAt,
214
- target: name
215
- });
216
- } catch (error) {
217
- const run = await runStore.getRun(toRunId("manual", name, scheduledAt, runIdSuffix));
218
- if (run?.status === "failed") return run;
219
- throw error;
220
- }
212
+ return definition;
213
+ }
214
+ function executeManualSchedule(definition, name, scheduledAt) {
215
+ return executeSchedule({
216
+ captureHandlerFailure: true,
217
+ definition,
218
+ runIdSuffix: randomId("manual"),
219
+ scheduleId: name,
220
+ scheduledAt,
221
+ source: "manual",
222
+ target: name
223
+ });
224
+ }
225
+ /** Runs a manually dispatchable static Schedule Definition of `registry` immediately. */
226
+ async function runScheduleFrom(registry, name, options = {}) {
227
+ return await executeManualSchedule(await resolveManualSchedule(registry, name), name, parseScheduledAt(options.scheduledAt));
221
228
  }
222
229
  //#endregion
223
230
  //#region src/started-errors.ts
@@ -247,4 +254,4 @@ const startedScheduleErrors = /*#__PURE__*/ defineErrors({
247
254
  }
248
255
  });
249
256
  //#endregion
250
- export { runScheduleFrom as a, executeStaticSchedule as i, createScheduleRun as n, toRunId as o, executeSchedule as r, randomId as s, startedScheduleErrors as t };
257
+ export { executeStaticSchedule as a, runScheduleFrom as c, executeSchedule as i, toRunId as l, createScheduleRun as n, loadStaticScheduleDefinition as o, executeManualSchedule as r, resolveManualSchedule as s, startedScheduleErrors as t, randomId as u };
@@ -115,4 +115,8 @@ function redactInspectionText(value) {
115
115
  return value.replace(embeddedUrlCredentialPattern, `$1${redactedInspectionValue}@`).replace(embeddedAuthorizationPattern, `$1${redactedInspectionValue}`).replace(embeddedCookiePattern, `$1${redactedInspectionValue}`).replace(embeddedBearerPattern, `Bearer ${redactedInspectionValue}`).replace(embeddedSecretAssignmentPattern, `$1${redactedInspectionValue}`);
116
116
  }
117
117
  //#endregion
118
- export { relative as a, isAbsolute as i, relativeDefinitionFile as n, resolve as o, dirname as r, redactInspectionText as t };
118
+ //#region src/runtime/started-name.ts
119
+ /** Name of the Schedule that runs due started Schedules each minute. Discovery adds it when a Definition has no `cron`. */
120
+ const startedSchedulesTickName = "vitehub/started-schedules";
121
+ //#endregion
122
+ export { isAbsolute as a, dirname as i, redactInspectionText as n, relative as o, relativeDefinitionFile as r, resolve as s, startedSchedulesTickName as t };
@@ -16,6 +16,13 @@ interface OccurrenceClaims {
16
16
  //#region src/runtime/state.d.ts
17
17
  /** The cron and time zone of a Schedule that the Process Runtime runs. */
18
18
  type ProcessScheduleTimingRecord = Pick<ScheduleDefinition, "cron" | "timeZone">;
19
+ interface HostScheduleInventory {
20
+ timings: Readonly<Record<string, {
21
+ cron: string;
22
+ timeZone?: string;
23
+ }>>;
24
+ registry?: ScheduleDefinitionRegistry;
25
+ }
19
26
  /** What `startSchedule()` and the started-Schedule tick use. The generated Nitro plugin installs it. */
20
27
  interface StartedScheduleRuntime {
21
28
  /** How far back the first tick after a start runs cron minutes that no tick checked. */
@@ -36,6 +43,8 @@ declare function getScheduleRuntimeRegistry(): ScheduleDefinitionRegistry | unde
36
43
  /** Publish only ready runtimes, retaining earlier snapshots until their controllers close. */
37
44
  declare function registerProcessScheduleDefinitions(definitions: ReadonlyMap<string, ProcessScheduleTimingRecord>): () => void;
38
45
  declare function getProcessScheduleDefinitions(): ReadonlyMap<string, ProcessScheduleTimingRecord> | undefined;
46
+ declare function setHostScheduleInventory(inventory: HostScheduleInventory): void;
47
+ declare function getHostScheduleInventory(): HostScheduleInventory | undefined;
39
48
  declare function setScheduleRunStore(store: ScheduleRunStore | undefined): void;
40
49
  declare function getScheduleRunStore(): ScheduleRunStore;
41
50
  /** Installs the claims that make each occurrence run in one process only. The Process Runtime installs them. */
@@ -46,4 +55,4 @@ declare function getStartedScheduleRuntime(): StartedScheduleRuntime | undefined
46
55
  declare function loadScheduleDefinition(name: string): Promise<ScheduleRegistryDefinition | undefined>;
47
56
  declare function resetScheduleRuntime(): void;
48
57
  //#endregion
49
- export { getScheduleRuntimeRegistry as a, registerProcessScheduleDefinitions as c, setScheduleRunStore as d, setScheduleRuntimeRegistry as f, getScheduleRunStore as i, resetScheduleRuntime as l, getProcessScheduleDefinitions as n, getStartedScheduleRuntime as o, setStartedScheduleRuntime as p, getScheduleOccurrenceClaims as r, loadScheduleDefinition as s, StartedScheduleRuntime as t, setScheduleOccurrenceClaims as u };
58
+ export { getScheduleRunStore as a, loadScheduleDefinition as c, setHostScheduleInventory as d, setScheduleOccurrenceClaims as f, setStartedScheduleRuntime as h, getScheduleOccurrenceClaims as i, registerProcessScheduleDefinitions as l, setScheduleRuntimeRegistry as m, getHostScheduleInventory as n, getScheduleRuntimeRegistry as o, setScheduleRunStore as p, getProcessScheduleDefinitions as r, getStartedScheduleRuntime as s, StartedScheduleRuntime as t, resetScheduleRuntime as u };
@@ -1,5 +1,5 @@
1
1
  import { n as isRecordOrFunction } from "./object-Da1zyPCR.js";
2
- import { t as scheduleErrorDiagnostics } from "./error-diagnostics-Mpeks7J0.js";
2
+ import { t as scheduleErrorDiagnostics } from "./error-diagnostics-BBtYYl8j.js";
3
3
  import { AsyncLocalStorage } from "node:async_hooks";
4
4
  //#region src/runtime/store.ts
5
5
  const maxLegacyHistoryRecords = 1e3;
@@ -368,6 +368,7 @@ function createKVScheduleRunStore(options) {
368
368
  //#endregion
369
369
  //#region src/runtime/state.ts
370
370
  let runtimeRegistry;
371
+ let hostInventory;
371
372
  const processDefinitions = /* @__PURE__ */ new Set();
372
373
  let runtimeRegistryVersion = 0;
373
374
  let runStore;
@@ -398,6 +399,12 @@ function registerProcessScheduleDefinitions(definitions) {
398
399
  function getProcessScheduleDefinitions() {
399
400
  return [...processDefinitions].at(-1);
400
401
  }
402
+ function setHostScheduleInventory(inventory) {
403
+ hostInventory = inventory;
404
+ }
405
+ function getHostScheduleInventory() {
406
+ return hostInventory;
407
+ }
401
408
  function setScheduleRunStore(store) {
402
409
  runStore = store;
403
410
  }
@@ -456,6 +463,7 @@ async function loadScheduleDefinition(name) {
456
463
  }
457
464
  function resetScheduleRuntime() {
458
465
  runtimeRegistry = void 0;
466
+ hostInventory = void 0;
459
467
  processDefinitions.clear();
460
468
  runtimeRegistryVersion++;
461
469
  runStore = void 0;
@@ -465,4 +473,4 @@ function resetScheduleRuntime() {
465
473
  loadingRegistryEntries.clear();
466
474
  }
467
475
  //#endregion
468
- export { getStartedScheduleRuntime as a, resetScheduleRuntime as c, setScheduleRuntimeRegistry as d, setStartedScheduleRuntime as f, keySegments as g, joinKey as h, getScheduleRuntimeRegistry as i, setScheduleOccurrenceClaims as l, createMemoryScheduleRunStore as m, getScheduleOccurrenceClaims as n, loadScheduleDefinition as o, createKVScheduleRunStore as p, getScheduleRunStore as r, registerProcessScheduleDefinitions as s, getProcessScheduleDefinitions as t, setScheduleRunStore as u };
476
+ export { joinKey as _, getScheduleRuntimeRegistry as a, registerProcessScheduleDefinitions as c, setScheduleOccurrenceClaims as d, setScheduleRunStore as f, createMemoryScheduleRunStore as g, createKVScheduleRunStore as h, getScheduleRunStore as i, resetScheduleRuntime as l, setStartedScheduleRuntime as m, getProcessScheduleDefinitions as n, getStartedScheduleRuntime as o, setScheduleRuntimeRegistry as p, getScheduleOccurrenceClaims as r, loadScheduleDefinition as s, getHostScheduleInventory as t, setHostScheduleInventory as u, keySegments as v };
@@ -1,8 +1,9 @@
1
1
  import { n as isRecordOrFunction } from "./object-Da1zyPCR.js";
2
- import { t as scheduleErrorDiagnostics } from "./error-diagnostics-Mpeks7J0.js";
2
+ import { t as scheduleErrorDiagnostics } from "./error-diagnostics-BBtYYl8j.js";
3
3
  import { a as validateScheduledAt, i as scheduleIdempotencyKey, n as isScheduleDate, o as createScheduleError, s as invalidScheduleValueDetails, t as runWithScheduleWaitUntil } from "./wait-until-Dy4Bn3ZN.js";
4
- import { n as latestCronMinute, t as isScheduleDue } from "./due-BVMRQ93v.js";
4
+ import { n as latestCronMinute, t as isScheduleDue } from "./due-sOJvF_6Z.js";
5
5
  import { normalizeRuntimeDiagnosticError } from "@vite-hub/runtime";
6
+ import { emitRunFailed, toRunFailedError } from "@vite-hub/runtime/internal/run-failed";
6
7
  import { runInRunScopes } from "@vite-hub/runtime/internal/run-scope";
7
8
  //#region ../internal/src/secret.ts
8
9
  const secretEncoder = new TextEncoder();
@@ -123,10 +124,24 @@ function createStaticScheduleRun(options) {
123
124
  };
124
125
  }
125
126
  async function executeStaticSchedule(options) {
126
- return await runInRunScopes(() => runWithScheduleWaitUntil((waitUntil) => options.definition.handler(createStaticScheduleRun({
127
- ...options,
128
- waitUntil
129
- })), options.waitUntil));
127
+ return await runInRunScopes(() => runWithScheduleWaitUntil(async (waitUntil) => {
128
+ const run = createStaticScheduleRun({
129
+ ...options,
130
+ waitUntil
131
+ });
132
+ try {
133
+ return await options.definition.handler(run);
134
+ } catch (error) {
135
+ await emitRunFailed({
136
+ event: "schedule.failed",
137
+ schedule: run.scheduleId,
138
+ runId: run.id,
139
+ error: toRunFailedError(error),
140
+ failedAt: (/* @__PURE__ */ new Date()).toISOString()
141
+ });
142
+ throw error;
143
+ }
144
+ }, options.waitUntil));
130
145
  }
131
146
  async function executeMatchingStaticSchedules(options) {
132
147
  if (typeof options.cron !== "string" || !options.cron.trim()) throw createScheduleError("SCHEDULE_INVALID_CRON", { details: invalidScheduleValueDetails("cron", options.cron) });
@@ -1,5 +1,5 @@
1
1
  import { a as ScheduleDefinitionRegistry, g as ScheduleRunStore, n as ScheduleDefinition, p as ScheduleRunRecord } from "./types-Cw5LjPuc.js";
2
- import { t as ScheduleKVStorage } from "./kv-storage-C0KZZB6v.js";
2
+ import { t as ScheduleKVStorage } from "./kv-storage-Bm9aYLfZ.js";
3
3
  //#region src/runtime/execute.d.ts
4
4
  interface ExecuteScheduleOptions {
5
5
  /** Return the persisted failed run when the handler throws. Guards still throw. */
package/dist/vite.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { t as DiscoveredScheduleDefinition } from "./types-Cw5LjPuc.js";
2
- import { n as OperationContributionEntry } from "./operation-definition-8gOe-ubD.js";
2
+ import { n as OperationContributionEntry } from "./operation-definition-nmRu_O3V.js";
3
3
  import { i as ViteHubInspectionPluginMetadata, n as ViteHubConsoleRuntimeRecordTableSection, r as ViteHubDefinitionSummary } from "./console-wEhr0LWn.js";
4
4
  //#region ../internal/src/deployment.d.ts
5
5
  /** The host that ViteHub builds for: the `host` option of `vitehub()` and the value of `VITEHUB_HOST`. */