@vite-hub/schedule 0.4.1 → 0.5.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 (53) hide show
  1. package/README.md +12 -7
  2. package/dist/{console-wEhr0LWn.d.ts → console-DFI1QBkP.d.ts} +2 -4
  3. package/dist/cron-BT3ef48f.js +68 -0
  4. package/dist/definition.d.ts +2 -4
  5. package/dist/definition.js +4 -6
  6. package/dist/{due-sOJvF_6Z.js → due-D-0trD1g.js} +23 -9
  7. package/dist/durability.d.ts +3 -5
  8. package/dist/durability.js +6 -7
  9. package/dist/{error-diagnostics-BBtYYl8j.js → error-diagnostics-B1AaIGWP.js} +2 -7
  10. package/dist/{error-diagnostics-CQ4BUqRe.js → error-diagnostics-kKXseq8d.js} +10 -3
  11. package/dist/index.d.ts +3 -13
  12. package/dist/index.js +5 -8
  13. package/dist/{started-name-ruCM1EtQ.js → inspect-CW0qwt9p.js} +3 -9
  14. package/dist/{kv-storage-2kZtP2CC.js → kv-storage-Dsr3TL9c.js} +1 -2
  15. package/dist/{kv-storage-Bm9aYLfZ.d.ts → kv-storage-hI2IOjb5.d.ts} +0 -2
  16. package/dist/{object-Da1zyPCR.js → object-CAK6UYR5.js} +1 -2
  17. package/dist/{operation-definition-nmRu_O3V.d.ts → operation-definition-CqZkx1Rm.d.ts} +8 -6
  18. package/dist/{operations-DqwmJ8MN.js → operations-BTUQeba1.js} +7 -11
  19. package/dist/operations.d.ts +2 -6
  20. package/dist/operations.js +11 -16
  21. package/dist/os.d.ts +0 -9
  22. package/dist/os.js +23 -104
  23. package/dist/out-By90R-Bo.js +22706 -0
  24. package/dist/rolldown-runtime-DegdyBIC.js +25 -0
  25. package/dist/runtime/console.d.ts +2 -4
  26. package/dist/runtime/console.js +3 -4
  27. package/dist/runtime/kv.d.ts +2 -4
  28. package/dist/runtime/kv.js +2 -3
  29. package/dist/runtime/process.d.ts +1 -3
  30. package/dist/runtime/process.js +28 -52
  31. package/dist/runtime/state.d.ts +1 -1
  32. package/dist/runtime/state.js +1 -2
  33. package/dist/runtime/static.d.ts +2 -73
  34. package/dist/runtime/static.js +3 -2
  35. package/dist/runtime.d.ts +5 -7
  36. package/dist/runtime.js +5 -5
  37. package/dist/{started-B0pc3JY0.js → started-B1Se_Iwo.js} +6 -8
  38. package/dist/{started-Bk2Icknk.js → started-Bzvr4t5f.js} +26 -9
  39. package/dist/{started-errors-DEmYH-vp.js → started-errors-IG9DlT2A.js} +49 -12
  40. package/dist/started-name-w6n9uDdt.js +4 -0
  41. package/dist/{state-CPd10SXq.d.ts → state-BDEDVlyr.d.ts} +3 -6
  42. package/dist/{state-D4SZH6zk.js → state-BZLy46UJ.js} +73 -122
  43. package/dist/static-Bg7Jj92L.js +603 -0
  44. package/dist/static-DeRvJqcQ.d.ts +92 -0
  45. package/dist/{store-DeMojnXM.d.ts → store-CEv3cScg.d.ts} +4 -12
  46. package/dist/{time-zone-SBldrUgt.js → time-zone-pMp_g8Uu.js} +1 -2
  47. package/dist/{types-Cw5LjPuc.d.ts → types-DTVPVY5O.d.ts} +0 -2
  48. package/dist/vite.d.ts +6 -20
  49. package/dist/vite.js +2943 -297
  50. package/package.json +9 -9
  51. package/dist/invariant-DolT9EEx.js +0 -22
  52. package/dist/static-7JCga3Iu.js +0 -281
  53. package/dist/wait-until-Dy4Bn3ZN.js +0 -112
@@ -1,16 +1,16 @@
1
- import { n as isRecordOrFunction } from "./object-Da1zyPCR.js";
2
- import { a as validateScheduledAt, i as scheduleIdempotencyKey, o as createScheduleError, r as parseScheduledAt, t as runWithScheduleWaitUntil } from "./wait-until-Dy4Bn3ZN.js";
3
- import { i as getScheduleRunStore } from "./state-D4SZH6zk.js";
4
- import { defineErrors, serializeResponse, toResponse } from "@vite-hub/runtime";
1
+ import { n as isRecordOrFunction } from "./object-CAK6UYR5.js";
2
+ import { S as createScheduleError, _ as errorCode, b as scheduleIdempotencyKey, c as markLoggedScheduleError, d as normalizeScheduleRuntimeError, g as emitLogEvent, h as logScheduleRun, m as runWithScheduleWaitUntil, x as validateScheduledAt, y as parseScheduledAt } from "./static-Bg7Jj92L.js";
3
+ import { i as getScheduleRunStore } from "./state-BZLy46UJ.js";
4
+ import { defineErrors } from "@vite-hub/runtime";
5
+ import { serializeResponse, toResponse } from "@vite-hub/runtime/internal/response";
5
6
  import { emitRunFailed, toRunFailedError } from "@vite-hub/runtime/internal/run-failed";
6
7
  import { runInRunScopes } from "@vite-hub/runtime/internal/run-scope";
7
- //#region ../internal/src/runtime/random.ts
8
+ // ../internal/src/runtime/random.ts
8
9
  function randomId(prefix) {
9
10
  const body = `${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
10
11
  return prefix ? `${prefix}_${body}` : body;
11
12
  }
12
- //#endregion
13
- //#region src/runtime/execute.ts
13
+ // src/runtime/execute.ts
14
14
  function normalizeRunSource(source) {
15
15
  return source ?? "direct";
16
16
  }
@@ -30,6 +30,10 @@ function toRunError(error) {
30
30
  return { message: "[unprintable thrown value]" };
31
31
  }
32
32
  }
33
+ function errorCodeField(error) {
34
+ const code = errorCode(error);
35
+ return code ? { code } : {};
36
+ }
33
37
  function requireUpdatedRun(run) {
34
38
  if (!run) throw createScheduleError("SCHEDULE_RUN_NOT_FOUND");
35
39
  return run;
@@ -166,12 +170,44 @@ async function executeSchedule(options) {
166
170
  }
167
171
  const attempt = await startAttempt(run, runStore);
168
172
  options.onExecution?.();
173
+ const runLog = {
174
+ name: run.target,
175
+ ...run.scheduleId === run.target ? {} : { id: run.scheduleId },
176
+ runId: run.id,
177
+ source: normalizeRunSource(options.source),
178
+ scheduledAt: run.scheduledAt,
179
+ attempt: run.attemptCount + 1
180
+ };
181
+ const waitUntil = options.waitUntil ? { waitUntil: (promise) => options.waitUntil?.(promise) } : {};
182
+ const recordRun = async (write) => {
183
+ try {
184
+ return await write();
185
+ } catch (error) {
186
+ throw emitLogEvent({
187
+ field: "schedule",
188
+ fields: {
189
+ op: "record-run",
190
+ name: run.target,
191
+ runId: run.id,
192
+ ...errorCodeField(error)
193
+ },
194
+ level: "error",
195
+ message: "Schedule could not record a finished run",
196
+ ...waitUntil
197
+ }) ? markLoggedScheduleError(error) : error;
198
+ }
199
+ };
200
+ const startedAt = performance.now();
169
201
  let response;
170
202
  try {
171
203
  const value = await runInRunScopes(() => runWithScheduleWaitUntil((waitUntil) => options.definition.handler(toHandlerContext(run, attempt, waitUntil)), options.waitUntil));
172
204
  response = await serializeResponse(toResponse(value));
173
205
  } catch (error) {
174
- const failed = await failRun(run, attempt, error, runStore);
206
+ const logged = logScheduleRun(runLog, startedAt, "failure", {
207
+ error: normalizeScheduleRuntimeError(error),
208
+ ...waitUntil
209
+ });
210
+ const failed = await recordRun(() => failRun(run, attempt, error, runStore));
175
211
  await emitRunFailed({
176
212
  event: "schedule.failed",
177
213
  schedule: failed.scheduleId,
@@ -180,15 +216,18 @@ async function executeSchedule(options) {
180
216
  failedAt: (failed.completedAt ?? /* @__PURE__ */ new Date()).toISOString()
181
217
  });
182
218
  if (options.captureHandlerFailure) return failed;
219
+ if (logged) options.onRunLogged?.();
183
220
  throw error;
184
221
  }
185
- return await completeRun(run, attempt, response, runStore);
222
+ logScheduleRun(runLog, startedAt, "success", waitUntil);
223
+ return await recordRun(() => completeRun(run, attempt, response, runStore));
186
224
  }
187
225
  async function executeStaticSchedule(options) {
188
226
  return await executeSchedule({
189
227
  ...options.claimed ? { claimed: true } : {},
190
228
  definition: options.definition,
191
229
  onExecution: options.onExecution,
230
+ ...options.onRunLogged ? { onRunLogged: options.onRunLogged } : {},
192
231
  scheduleId: options.name,
193
232
  source: "static",
194
233
  scheduledAt: options.scheduledAt,
@@ -226,8 +265,7 @@ function executeManualSchedule(definition, name, scheduledAt) {
226
265
  async function runScheduleFrom(registry, name, options = {}) {
227
266
  return await executeManualSchedule(await resolveManualSchedule(registry, name), name, parseScheduledAt(options.scheduledAt));
228
267
  }
229
- //#endregion
230
- //#region src/started-errors.ts
268
+ // src/started-errors.ts
231
269
  const startedScheduleErrors = /*#__PURE__*/ defineErrors({
232
270
  docs: "https://vitehub.dev/docs/schedule/diagnostics",
233
271
  codes: {
@@ -253,5 +291,4 @@ const startedScheduleErrors = /*#__PURE__*/ defineErrors({
253
291
  }
254
292
  }
255
293
  });
256
- //#endregion
257
294
  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 };
@@ -0,0 +1,4 @@
1
+ // src/runtime/started-name.ts
2
+ /** Name of the Schedule that runs due started Schedules each minute. Discovery adds it when a Definition has no `cron`. */
3
+ const startedSchedulesTickName = "vitehub/started-schedules";
4
+ export { startedSchedulesTickName as t };
@@ -1,6 +1,5 @@
1
- import { a as ScheduleDefinitionRegistry, b as StartedScheduleStore, g as ScheduleRunStore, n as ScheduleDefinition, s as ScheduleRegistryDefinition } from "./types-Cw5LjPuc.js";
1
+ import { a as ScheduleDefinitionRegistry, b as StartedScheduleStore, g as ScheduleRunStore, n as ScheduleDefinition, s as ScheduleRegistryDefinition } from "./types-DTVPVY5O.js";
2
2
  import { LeaseStore } from "@vite-hub/runtime";
3
- //#region src/runtime/claims.d.ts
4
3
  /** `held` when another process holds the occurrence now, `done` when a process finished it, `ran` when this call ran it. */
5
4
  type OccurrenceClaimResult = "done" | "held" | "ran";
6
5
  /** Runs each Schedule occurrence in one process of all the processes that share `leases`. */
@@ -12,9 +11,6 @@ interface OccurrenceClaims {
12
11
  */
13
12
  run(runId: string, run: (markFinal: () => void) => Promise<unknown>): Promise<OccurrenceClaimResult>;
14
13
  }
15
- //#endregion
16
- //#region src/runtime/state.d.ts
17
- /** The cron and time zone of a Schedule that the Process Runtime runs. */
18
14
  type ProcessScheduleTimingRecord = Pick<ScheduleDefinition, "cron" | "timeZone">;
19
15
  interface HostScheduleInventory {
20
16
  timings: Readonly<Record<string, {
@@ -34,6 +30,8 @@ interface StartedScheduleRuntime {
34
30
  scheduledAt: Date;
35
31
  scheduleId: string;
36
32
  }>;
33
+ /** The cause that each failing started Schedule last logged, by id. A held occurrence fails again each tick. */
34
+ failing?: Map<string, string>;
37
35
  /** Schedule Definitions without `cron`, by name. */
38
36
  registry: ScheduleDefinitionRegistry;
39
37
  store: StartedScheduleStore;
@@ -54,5 +52,4 @@ declare function setStartedScheduleRuntime(runtime: StartedScheduleRuntime | und
54
52
  declare function getStartedScheduleRuntime(): StartedScheduleRuntime | undefined;
55
53
  declare function loadScheduleDefinition(name: string): Promise<ScheduleRegistryDefinition | undefined>;
56
54
  declare function resetScheduleRuntime(): void;
57
- //#endregion
58
55
  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,8 +1,7 @@
1
- import { n as isRecordOrFunction } from "./object-Da1zyPCR.js";
2
- import { t as scheduleErrorDiagnostics } from "./error-diagnostics-BBtYYl8j.js";
1
+ import { t as scheduleErrorDiagnostics } from "./error-diagnostics-B1AaIGWP.js";
2
+ import { n as isRecordOrFunction } from "./object-CAK6UYR5.js";
3
3
  import { AsyncLocalStorage } from "node:async_hooks";
4
- //#region src/runtime/store.ts
5
- const maxLegacyHistoryRecords = 1e3;
4
+ // src/runtime/store.ts
6
5
  function trimPrefix(prefix) {
7
6
  return prefix.replace(/^\/+|\/+$/g, "");
8
7
  }
@@ -27,9 +26,6 @@ function scheduleRunAttemptKey(prefix, id) {
27
26
  function scheduleRunBase(prefix) {
28
27
  return `${joinKey(prefix, "schedule-runs")}/`;
29
28
  }
30
- function scheduleRunAttemptBase(prefix) {
31
- return `${joinKey(prefix, "schedule-run-attempts")}/`;
32
- }
33
29
  const keyLocks = /* @__PURE__ */ new Map();
34
30
  async function withKVKeyLock(key, operation) {
35
31
  const previous = keyLocks.get(key) ?? Promise.resolve();
@@ -148,115 +144,91 @@ function selectRuns(runs, options = {}) {
148
144
  selected.sort((left, right) => right.scheduledAt.getTime() - left.scheduledAt.getTime());
149
145
  return options.limit === void 0 ? selected : selected.slice(0, options.limit);
150
146
  }
151
- /** Index keys carry metadata from the stored record, independent of caller-selected run IDs. */
152
- function runIndexMetadata(key) {
147
+ function listedKeyId(key) {
153
148
  try {
154
- const parts = keySegments(key, 3);
155
- const scheduledAt = Date.parse(parts[2]);
156
- if (!Number.isFinite(scheduledAt)) return;
157
- return {
158
- id: parts[0],
159
- scheduleId: parts[1],
160
- scheduledAt
161
- };
149
+ return keySegments(key, 1)[0];
162
150
  } catch {
163
151
  return;
164
152
  }
165
153
  }
154
+ async function mapInBatches(items, map) {
155
+ const result = [];
156
+ for (let offset = 0; offset < items.length; offset += 16) result.push(...await Promise.all(items.slice(offset, offset + 16).map(map)));
157
+ return result;
158
+ }
166
159
  function createKVScheduleRunStore(options) {
167
160
  const prefix = options.prefix ?? "vitehub:schedule";
168
161
  const store = options.kvStore;
169
- async function indexRun(run) {
170
- try {
171
- await store.set(joinKey(prefix, "schedule-run-index", run.id, run.scheduleId, run.scheduledAt.toISOString()), true);
172
- } catch {}
162
+ const runIndex = joinKey(prefix, "run-index");
163
+ const attemptIndex = joinKey(prefix, "attempt-index");
164
+ const upgradeKey = joinKey(prefix, "indexes-v2");
165
+ let indexesReady = false;
166
+ function writeRunIndex(key, run) {
167
+ return store.set(key, {
168
+ scheduleId: run.scheduleId,
169
+ scheduledAt: new Date(run.scheduledAt).getTime()
170
+ });
173
171
  }
174
- async function indexAttempt(attempt) {
175
- try {
176
- await store.set(joinKey(prefix, "schedule-run-attempt-index", attempt.runId, attempt.id), true);
177
- } catch {}
172
+ async function ensureIndexes() {
173
+ if (indexesReady) return;
174
+ await withKVKeyLock(upgradeKey, async () => {
175
+ if (indexesReady) return;
176
+ if (!await store.has(upgradeKey)) {
177
+ await mapInBatches(await store.keys(scheduleRunBase(prefix)), async (key) => {
178
+ const id = listedKeyId(key);
179
+ if (id === void 0) return;
180
+ const indexKey = joinKey(runIndex, id);
181
+ if (await store.has(indexKey)) return;
182
+ const run = await store.get(key);
183
+ if (run) await writeRunIndex(indexKey, run);
184
+ });
185
+ await mapInBatches(await store.keys(`${joinKey(prefix, "schedule-run-attempts")}/`), async (key) => {
186
+ const id = listedKeyId(key);
187
+ if (id === void 0) return;
188
+ const indexKey = joinKey(attemptIndex, id);
189
+ if (await store.has(indexKey)) return;
190
+ const attempt = await store.get(key);
191
+ if (attempt) await store.set(indexKey, { runId: attempt.runId });
192
+ });
193
+ await store.set(upgradeKey, true);
194
+ }
195
+ indexesReady = true;
196
+ });
178
197
  }
179
198
  async function readSnapshot() {
180
- const keys = await store.keys(scheduleRunBase(prefix));
181
- const indexKeys = await store.keys(`${joinKey(prefix, "schedule-run-index")}/`);
182
- const metadataById = new Map(indexKeys.flatMap((key) => {
183
- const metadata = runIndexMetadata(key);
184
- return metadata ? [[metadata.id, metadata]] : [];
185
- }));
186
- const known = [];
187
- const bySchedule = /* @__PURE__ */ new Map();
188
- const unknown = [];
189
- const recordsByKey = /* @__PURE__ */ new Map();
190
- function groupRun(key, metadata) {
191
- const entry = {
192
- id: metadata.id,
199
+ await ensureIndexes();
200
+ const indexed = await mapInBatches(await store.keys(scheduleRunBase(prefix)), async (key) => {
201
+ const id = listedKeyId(key);
202
+ if (id === void 0) return;
203
+ const metadata = await store.get(joinKey(runIndex, id));
204
+ if (metadata) return {
205
+ id,
193
206
  key,
194
- scheduledAt: metadata.scheduledAt
207
+ ...metadata
195
208
  };
209
+ });
210
+ const known = [];
211
+ const bySchedule = /* @__PURE__ */ new Map();
212
+ for (const entry of indexed) {
213
+ if (!entry) continue;
196
214
  known.push(entry);
197
- const group = bySchedule.get(metadata.scheduleId) ?? [];
198
- bySchedule.set(metadata.scheduleId, group);
215
+ const group = bySchedule.get(entry.scheduleId) ?? [];
216
+ bySchedule.set(entry.scheduleId, group);
199
217
  group.push(entry);
200
218
  }
201
- for (const key of keys) {
202
- let id;
203
- try {
204
- id = keySegments(key, 1)[0];
205
- } catch {
206
- unknown.push(key);
207
- continue;
208
- }
209
- const metadata = metadataById.get(id);
210
- if (!metadata) {
211
- unknown.push(key);
212
- continue;
213
- }
214
- groupRun(key, metadata);
215
- }
216
- if (unknown.length > maxLegacyHistoryRecords) return {
217
- keys,
218
- known,
219
- bySchedule,
220
- recordsByKey,
221
- unknown
222
- };
223
- const legacyKeys = unknown;
224
- for (let offset = 0; offset < legacyKeys.length; offset += 16) {
225
- const batch = await Promise.all(legacyKeys.slice(offset, offset + 16).map(async (key) => {
226
- const stored = await store.get(key);
227
- const run = stored ? cloneScheduleRun(stored) : void 0;
228
- recordsByKey.set(key, run);
229
- if (run) await indexRun(run);
230
- return {
231
- key,
232
- run
233
- };
234
- }));
235
- for (const { key, run } of batch) if (run) groupRun(key, {
236
- id: run.id,
237
- scheduleId: run.scheduleId,
238
- scheduledAt: run.scheduledAt.getTime()
239
- });
240
- }
241
219
  for (const entries of [known, ...bySchedule.values()]) entries.sort((left, right) => right.scheduledAt - left.scheduledAt);
242
220
  return {
243
- keys,
244
221
  known,
245
222
  bySchedule,
246
- recordsByKey,
247
- unknown
223
+ recordsByKey: /* @__PURE__ */ new Map()
248
224
  };
249
225
  }
250
226
  async function listRuns(options, snapshot) {
251
- if (snapshot.unknown.length > maxLegacyHistoryRecords) throw scheduleErrorDiagnostics.SCHEDULE_HISTORY_INCOMPLETE({
252
- maxRecords: maxLegacyHistoryRecords,
253
- recordCount: snapshot.unknown.length
254
- });
255
- const { keys, recordsByKey } = snapshot;
227
+ const { recordsByKey } = snapshot;
256
228
  const known = options.scheduleId === void 0 ? snapshot.known : snapshot.bySchedule.get(options.scheduleId) ?? [];
257
229
  const matching = options.idPrefix === void 0 ? known : known.filter((entry) => entry.id.startsWith(options.idPrefix));
258
230
  const records = [];
259
- const selectedKeys = options.scheduleId === void 0 && options.idPrefix === void 0 && options.limit === void 0 ? keys : matching.slice(0, options.limit ?? matching.length).map((entry) => entry.key);
231
+ const selectedKeys = matching.slice(0, options.limit ?? matching.length).map((entry) => entry.key);
260
232
  for (let index = 0; index < selectedKeys.length; index += 16) {
261
233
  const batch = await Promise.all(selectedKeys.slice(index, index + 16).map(async (key) => {
262
234
  if (recordsByKey.has(key)) return recordsByKey.get(key);
@@ -274,8 +246,8 @@ function createKVScheduleRunStore(options) {
274
246
  const key = scheduleRunAttemptKey(prefix, attempt.id);
275
247
  return await withKVKeyLock(key, async () => {
276
248
  if (await store.has(key)) throw scheduleErrorDiagnostics.SCHEDULE_R0032({ message: `Schedule Run Attempt already exists: ${attempt.id}` });
249
+ await store.set(joinKey(attemptIndex, attempt.id), { runId: attempt.runId });
277
250
  await store.set(key, serializeScheduleRunAttempt(attempt));
278
- await indexAttempt(attempt);
279
251
  return cloneScheduleRunAttempt(attempt);
280
252
  });
281
253
  },
@@ -283,8 +255,8 @@ function createKVScheduleRunStore(options) {
283
255
  const key = scheduleRunKey(prefix, run.id);
284
256
  return await withKVKeyLock(key, async () => {
285
257
  if (await store.has(key)) throw scheduleErrorDiagnostics.SCHEDULE_R0033({ message: `Schedule Run already exists: ${run.id}` });
258
+ await writeRunIndex(joinKey(runIndex, run.id), run);
286
259
  await store.set(key, serializeScheduleRun(run));
287
- await indexRun(run);
288
260
  return cloneScheduleRun(run);
289
261
  });
290
262
  },
@@ -297,33 +269,14 @@ function createKVScheduleRunStore(options) {
297
269
  return run ? cloneScheduleRun(run) : void 0;
298
270
  },
299
271
  async listAttempts(runId) {
300
- const [keys, indexKeys] = await Promise.all([store.keys(scheduleRunAttemptBase(prefix)), store.keys(`${joinKey(prefix, "schedule-run-attempt-index")}/`)]);
301
- const runByAttemptId = /* @__PURE__ */ new Map();
302
- for (const key of indexKeys) try {
303
- const parts = keySegments(key, 2);
304
- runByAttemptId.set(parts[1], parts[0]);
305
- } catch {}
306
- const selectedKeys = keys.filter((key) => {
307
- try {
308
- const id = keySegments(key, 1)[0];
309
- const indexedRunId = runByAttemptId.get(id);
310
- return indexedRunId === void 0 || indexedRunId === runId;
311
- } catch {
312
- return true;
313
- }
314
- });
315
- const attempts = [];
316
- for (let offset = 0; offset < selectedKeys.length; offset += 16) {
317
- const batch = await Promise.all(selectedKeys.slice(offset, offset + 16).map(async (key) => {
318
- const stored = await store.get(key);
319
- if (!stored) return;
320
- const attempt = cloneScheduleRunAttempt(stored);
321
- if (!runByAttemptId.has(attempt.id)) await indexAttempt(attempt);
322
- return attempt.runId === runId ? attempt : void 0;
323
- }));
324
- attempts.push(...batch.flatMap((attempt) => attempt ? [attempt] : []));
325
- }
326
- return attempts;
272
+ await ensureIndexes();
273
+ return (await mapInBatches(await store.keys(`${attemptIndex}/`), async (key) => {
274
+ if ((await store.get(key))?.runId !== runId) return;
275
+ const id = listedKeyId(key);
276
+ if (id === void 0) return;
277
+ const attempt = await store.get(scheduleRunAttemptKey(prefix, id));
278
+ if (attempt?.runId === runId) return cloneScheduleRunAttempt(attempt);
279
+ })).flatMap((attempt) => attempt ? [attempt] : []);
327
280
  },
328
281
  async listRuns(options = {}) {
329
282
  return listRuns(options, await readSnapshot());
@@ -365,8 +318,7 @@ function createKVScheduleRunStore(options) {
365
318
  }
366
319
  };
367
320
  }
368
- //#endregion
369
- //#region src/runtime/state.ts
321
+ // src/runtime/state.ts
370
322
  let runtimeRegistry;
371
323
  let hostInventory;
372
324
  const processDefinitions = /* @__PURE__ */ new Set();
@@ -472,5 +424,4 @@ function resetScheduleRuntime() {
472
424
  loadedRegistryEntries.clear();
473
425
  loadingRegistryEntries.clear();
474
426
  }
475
- //#endregion
476
427
  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 };