@lunora/scheduler 1.0.0-alpha.2 → 1.0.0-alpha.21
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.md +6 -0
- package/README.md +1 -0
- package/dist/index.d.mts +551 -385
- package/dist/index.d.ts +551 -385
- package/dist/index.mjs +1 -8
- package/dist/packem_shared/CRON_SCHEDULE_KINDS-DMu4DfhG.mjs +1 -0
- package/dist/packem_shared/SchedulerDO-CCP9LV5x.mjs +1 -0
- package/dist/packem_shared/assertValidCronExpression-B2lN8n3O.mjs +1 -0
- package/dist/packem_shared/createCronTrigger-C4iUW_zz.mjs +1 -0
- package/dist/packem_shared/createQueueConsumer-BnX7wkJO.mjs +1 -0
- package/dist/packem_shared/createScheduler-CsRAEtdb.mjs +1 -0
- package/dist/packem_shared/createSchedulerHost-C5XacwXv.mjs +1 -0
- package/dist/packem_shared/createWorkpool-DyVdhB6o.mjs +1 -0
- package/dist/packem_shared/do-client-BFps7NIj.mjs +1 -0
- package/dist/packem_shared/isWorkflowReference-CT3tdefh.mjs +1 -0
- package/package.json +5 -3
- package/dist/packem_shared/CRON_SCHEDULE_KINDS-BaLlXJiN.mjs +0 -123
- package/dist/packem_shared/SchedulerDO-BNzXNnS4.mjs +0 -678
- package/dist/packem_shared/assertValidCronExpression-BLfrDgmK.mjs +0 -20
- package/dist/packem_shared/createCronTrigger-Cq9IBcWQ.mjs +0 -27
- package/dist/packem_shared/createQueueConsumer-DWahNPfz.mjs +0 -59
- package/dist/packem_shared/createScheduler-CWMn70nv.mjs +0 -69
- package/dist/packem_shared/createWorkpool-CEnqCafM.mjs +0 -62
- package/dist/packem_shared/isWorkflowReference-C9mQkMXt.mjs +0 -3
- package/dist/packem_shared/jurisdiction-CR2zC3Et.mjs +0 -13
package/dist/index.d.mts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
+
import { SchedulerHost } from '@lunora/platform';
|
|
1
2
|
/**
|
|
2
|
-
* Opaque reference to a Lunora function. Mirrors the `FunctionReference` shape
|
|
3
|
-
* emitted by `@lunora/codegen` (and consumed by `@lunora/client`). We avoid a
|
|
4
|
-
* direct dependency to keep this package usable from the codegen pipeline
|
|
5
|
-
* itself.
|
|
6
|
-
*
|
|
7
|
-
* The runtime identifier lives in `__lunoraRef` — this MUST stay in lockstep
|
|
8
|
-
* with the codegen emit + `@lunora/client`'s `FunctionReference`.
|
|
9
|
-
*/
|
|
3
|
+
* Opaque reference to a Lunora function. Mirrors the `FunctionReference` shape
|
|
4
|
+
* emitted by `@lunora/codegen` (and consumed by `@lunora/client`). We avoid a
|
|
5
|
+
* direct dependency to keep this package usable from the codegen pipeline
|
|
6
|
+
* itself.
|
|
7
|
+
*
|
|
8
|
+
* The runtime identifier lives in `__lunoraRef` — this MUST stay in lockstep
|
|
9
|
+
* with the codegen emit + `@lunora/client`'s `FunctionReference`.
|
|
10
|
+
*/
|
|
10
11
|
interface FunctionReference {
|
|
11
12
|
readonly __lunoraRef: string;
|
|
12
13
|
/** Marker phantom type — discriminates queries / mutations / actions. */
|
|
@@ -16,26 +17,26 @@ type ArgsOf<F extends FunctionReference> = F extends {
|
|
|
16
17
|
_args?: infer A;
|
|
17
18
|
} ? A : Record<string, unknown>;
|
|
18
19
|
/**
|
|
19
|
-
* Typed reference to a Lunora durable workflow — either the generated
|
|
20
|
-
* `workflows
|
|
21
|
-
* `WORKFLOW_*` binding + export name) or, structurally, a `defineWorkflow()`
|
|
22
|
-
* result imported directly. Both are matched by the `isLunoraWorkflow` brand and
|
|
23
|
-
* carry the workflow's `params` in the phantom `__params`, so a `cronJobs()`
|
|
24
|
-
* registration infers them.
|
|
25
|
-
*
|
|
26
|
-
* Declared structurally here so `@lunora/scheduler` can let a `cronJobs()`
|
|
27
|
-
* builder target a workflow without depending on `@lunora/workflow` (and so the
|
|
28
|
-
* generated `workflows.*` object needs no `@lunora/scheduler` import — it
|
|
29
|
-
* matches structurally). A cron whose target is a {@link WorkflowReference}
|
|
30
|
-
* starts a new workflow INSTANCE on each fire (the args become its `params`)
|
|
31
|
-
* instead of dispatching a one-shot function. `@lunora/codegen` resolves the
|
|
32
|
-
* concrete `lunora/workflows.ts` export statically; the runtime brand here is
|
|
33
|
-
* the authoring-time guard.
|
|
34
|
-
*/
|
|
20
|
+
* Typed reference to a Lunora durable workflow — either the generated
|
|
21
|
+
* `workflows.<name>` reference object (`_generated/api.ts`, which carries the
|
|
22
|
+
* `WORKFLOW_*` binding + export name) or, structurally, a `defineWorkflow()`
|
|
23
|
+
* result imported directly. Both are matched by the `isLunoraWorkflow` brand and
|
|
24
|
+
* carry the workflow's `params` in the phantom `__params`, so a `cronJobs()`
|
|
25
|
+
* registration infers them.
|
|
26
|
+
*
|
|
27
|
+
* Declared structurally here so `@lunora/scheduler` can let a `cronJobs()`
|
|
28
|
+
* builder target a workflow without depending on `@lunora/workflow` (and so the
|
|
29
|
+
* generated `workflows.*` object needs no `@lunora/scheduler` import — it
|
|
30
|
+
* matches structurally). A cron whose target is a {@link WorkflowReference}
|
|
31
|
+
* starts a new workflow INSTANCE on each fire (the args become its `params`)
|
|
32
|
+
* instead of dispatching a one-shot function. `@lunora/codegen` resolves the
|
|
33
|
+
* concrete `lunora/workflows.ts` export statically; the runtime brand here is
|
|
34
|
+
* the authoring-time guard.
|
|
35
|
+
*/
|
|
35
36
|
interface WorkflowReference<Params = Record<string, unknown>> {
|
|
36
37
|
/** Phantom carrier for the workflow's `params` type — drives `cronJobs()` arg inference. Never read at runtime. */
|
|
37
38
|
readonly __params?: Params;
|
|
38
|
-
/** The `WORKFLOW_*` binding name (present on a generated `workflows
|
|
39
|
+
/** The `WORKFLOW_*` binding name (present on a generated `workflows.<name>` ref). */
|
|
39
40
|
readonly binding?: string;
|
|
40
41
|
readonly isLunoraWorkflow: true;
|
|
41
42
|
/** The workflow's export/stable name (present on a generated ref; a `defineWorkflow({ name })` override otherwise). */
|
|
@@ -45,23 +46,32 @@ interface WorkflowReference<Params = Record<string, unknown>> {
|
|
|
45
46
|
type CronTarget = FunctionReference | WorkflowReference;
|
|
46
47
|
/** The arguments a cron's target accepts: a workflow's inferred `params`, else an open record (function args aren't inferred). */
|
|
47
48
|
type CronTargetArgs<T extends CronTarget> = T extends WorkflowReference<infer Params> ? Params : Record<string, unknown>;
|
|
49
|
+
/**
|
|
50
|
+
* The arguments a one-shot schedule target ({@link Scheduler.runAfter} /
|
|
51
|
+
* {@link Scheduler.runAt}) accepts. Unlike {@link CronTargetArgs} it preserves a
|
|
52
|
+
* {@link FunctionReference}'s inferred `args` (via {@link ArgsOf}) as well as a
|
|
53
|
+
* {@link WorkflowReference}'s inferred `params`, so scheduling a plain function
|
|
54
|
+
* keeps its today's arg checking while scheduling a workflow/agent infers its
|
|
55
|
+
* `params`.
|
|
56
|
+
*/
|
|
57
|
+
type ScheduleTargetArgs<T extends CronTarget> = T extends WorkflowReference<infer Params> ? Params : T extends FunctionReference ? ArgsOf<T> : Record<string, unknown>;
|
|
48
58
|
/** Narrow a {@link CronTarget} to a {@link WorkflowReference} by its runtime brand. */
|
|
49
59
|
declare const isWorkflowReference: (target: unknown) => target is WorkflowReference;
|
|
50
60
|
/**
|
|
51
|
-
* Per-job retry policy. Wired into the SchedulerDO's existing attempts/backoff
|
|
52
|
-
* machinery. When omitted, the DO falls back to its built-in defaults
|
|
53
|
-
* (`maxAttempts: 5`, `backoff: "exponential"`, `baseMs: 30_000`) so existing
|
|
54
|
-
* `runAfter`/`runAt` callers keep today's behaviour unchanged.
|
|
55
|
-
*
|
|
56
|
-
* On exhaustion (attempts > `maxAttempts`) the record is parked under the
|
|
57
|
-
* `dead:` dead-letter key for inspection — never silently dropped.
|
|
58
|
-
*/
|
|
61
|
+
* Per-job retry policy. Wired into the SchedulerDO's existing attempts/backoff
|
|
62
|
+
* machinery. When omitted, the DO falls back to its built-in defaults
|
|
63
|
+
* (`maxAttempts: 5`, `backoff: "exponential"`, `baseMs: 30_000`) so existing
|
|
64
|
+
* `runAfter`/`runAt` callers keep today's behaviour unchanged.
|
|
65
|
+
*
|
|
66
|
+
* On exhaustion (attempts > `maxAttempts`) the record is parked under the
|
|
67
|
+
* `dead:` dead-letter key for inspection — never silently dropped.
|
|
68
|
+
*/
|
|
59
69
|
interface RetryPolicy {
|
|
60
70
|
/**
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
71
|
+
* Backoff growth across attempts. `"exponential"` doubles the delay each
|
|
72
|
+
* attempt (`baseMs * 2 ** (attempt - 1)`); `"linear"` grows it linearly
|
|
73
|
+
* (`baseMs * attempt`). Default `"exponential"`.
|
|
74
|
+
*/
|
|
65
75
|
backoff?: "exponential" | "linear";
|
|
66
76
|
/** Base delay in milliseconds for the first retry. Default `30_000`. */
|
|
67
77
|
baseMs?: number;
|
|
@@ -72,11 +82,11 @@ interface RetryPolicy {
|
|
|
72
82
|
}
|
|
73
83
|
interface RunOptions {
|
|
74
84
|
/**
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
85
|
+
* Logical workpool this job belongs to. When set, the SchedulerDO gates the
|
|
86
|
+
* job behind the pool's `maxConcurrency` (see {@link WorkpoolOptions}).
|
|
87
|
+
* Usually populated by {@link Workpool.enqueue}; callers rarely set it on a
|
|
88
|
+
* bare `runAfter`/`runAt`.
|
|
89
|
+
*/
|
|
80
90
|
pool?: string;
|
|
81
91
|
/** Per-job retry policy. Falls back to the DO's built-in defaults when omitted. */
|
|
82
92
|
retry?: RetryPolicy;
|
|
@@ -86,65 +96,100 @@ interface RunOptions {
|
|
|
86
96
|
interface ScheduleRecord {
|
|
87
97
|
args: Record<string, unknown>;
|
|
88
98
|
/**
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
99
|
+
* Number of dispatch attempts already made. Absent (treated as 0) until the
|
|
100
|
+
* first failure, after which `recordRetry()` persists it on both the
|
|
101
|
+
* `retry:` row and the `id:` header. Surfaced here so `/list` consumers and
|
|
102
|
+
* the studio see the field the storage layer actually writes.
|
|
103
|
+
*/
|
|
94
104
|
attempts?: number;
|
|
95
105
|
enqueuedAt: number;
|
|
96
|
-
|
|
106
|
+
/**
|
|
107
|
+
* The `ns:fn` path of the function to dispatch on fire. Absent when the job
|
|
108
|
+
* targets a durable workflow/agent instead — see {@link ScheduleRecord.workflow}.
|
|
109
|
+
* Exactly one of `functionPath` / `workflow` is set.
|
|
110
|
+
*/
|
|
111
|
+
functionPath?: string;
|
|
97
112
|
id: string;
|
|
98
113
|
/**
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
114
|
+
* Scheduler/workpool instance name the job was enqueued through. Echoed in
|
|
115
|
+
* the dispatch payload so the runtime can call back the SAME DO instance's
|
|
116
|
+
* `/complete` to release a pooled slot. Absent for the default instance.
|
|
117
|
+
*/
|
|
103
118
|
instanceName?: string;
|
|
104
119
|
/**
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
120
|
+
* Logical workpool this job belongs to (set by {@link Workpool.enqueue}).
|
|
121
|
+
* When present, the SchedulerDO only dispatches the job while the pool's
|
|
122
|
+
* in-flight count is below its `maxConcurrency`; otherwise it stays queued
|
|
123
|
+
* and drains as slots free. Absent for plain `runAfter`/`runAt` jobs, which
|
|
124
|
+
* are never concurrency-gated.
|
|
125
|
+
*/
|
|
111
126
|
pool?: string;
|
|
112
127
|
/** Per-job retry policy (see {@link RetryPolicy}); absent means DO defaults. */
|
|
113
128
|
retry?: RetryPolicy;
|
|
114
129
|
scheduledFor: number;
|
|
115
130
|
shardKey?: string;
|
|
131
|
+
/**
|
|
132
|
+
* The `WORKFLOW_*`/`AGENT_*` binding name to start a fresh durable instance
|
|
133
|
+
* of on fire (the {@link ScheduleRecord.args} become its `params`). Set
|
|
134
|
+
* instead of {@link ScheduleRecord.functionPath} when the job targets a
|
|
135
|
+
* workflow/agent {@link WorkflowReference}. The runtime — not the DO — owns
|
|
136
|
+
* the binding, so the dispatch payload carries this through to the Worker.
|
|
137
|
+
*/
|
|
138
|
+
workflow?: string;
|
|
116
139
|
}
|
|
117
140
|
interface Scheduler {
|
|
118
141
|
cancel: (id: string) => Promise<{
|
|
119
142
|
cancelled: boolean;
|
|
120
143
|
}>;
|
|
144
|
+
/**
|
|
145
|
+
* Jobs that exhausted their retry budget and were parked under `dead:`
|
|
146
|
+
* (the DO's `/dead` view). Deliberately absent from {@link Scheduler.list} —
|
|
147
|
+
* the park deletes the `id:` header — so this is the only view of a job
|
|
148
|
+
* that failed permanently rather than being silently dropped.
|
|
149
|
+
*/
|
|
150
|
+
dead: () => Promise<ScheduleRecord[]>;
|
|
151
|
+
/**
|
|
152
|
+
* Resurrect a parked job with a fresh attempt budget (the DO's
|
|
153
|
+
* `POST /dead/retry`). `false` when the id is not parked; a racing double
|
|
154
|
+
* recover is a no-op rather than an error.
|
|
155
|
+
*/
|
|
156
|
+
deadRetry: (id: string) => Promise<boolean>;
|
|
121
157
|
/** Resolve a single pending job by id, or `null` when absent (derived from {@link Scheduler.list}). */
|
|
122
158
|
get: (id: string) => Promise<ScheduleRecord | null>;
|
|
123
159
|
/** All pending scheduled jobs (the DO's `/list` view). */
|
|
124
160
|
list: () => Promise<ScheduleRecord[]>;
|
|
125
|
-
|
|
161
|
+
/**
|
|
162
|
+
* Schedule `target` to run once, `delayMs` from now. `target` is a function
|
|
163
|
+
* {@link FunctionReference} (dispatched as a one-shot) or a durable
|
|
164
|
+
* {@link WorkflowReference} — the generated `workflows.<name>` /
|
|
165
|
+
* `agents.<name>` ref — which starts a fresh instance on fire (args become
|
|
166
|
+
* its `params`). {@link ScheduleTargetArgs} infers the accepted args from
|
|
167
|
+
* whichever target was passed.
|
|
168
|
+
*/
|
|
169
|
+
runAfter: <T extends CronTarget>(delayMs: number, target: T, args: ScheduleTargetArgs<T>, options?: RunOptions) => Promise<{
|
|
126
170
|
id: string;
|
|
127
171
|
scheduledFor: number;
|
|
128
172
|
}>;
|
|
129
|
-
|
|
173
|
+
/** Like {@link Scheduler.runAfter} but fires at an absolute `date`/timestamp. */
|
|
174
|
+
runAt: <T extends CronTarget>(date: Date | number, target: T, args: ScheduleTargetArgs<T>, options?: RunOptions) => Promise<{
|
|
130
175
|
id: string;
|
|
131
176
|
scheduledFor: number;
|
|
132
177
|
}>;
|
|
133
178
|
}
|
|
134
179
|
/**
|
|
135
|
-
* Cloudflare Durable Object data-residency jurisdiction. Widening union —
|
|
136
|
-
* Cloudflare adds values over time.
|
|
137
|
-
* @see https://developers.cloudflare.com/durable-objects/reference/data-location/
|
|
138
|
-
*/
|
|
180
|
+
* Cloudflare Durable Object data-residency jurisdiction. Widening union —
|
|
181
|
+
* Cloudflare adds values over time.
|
|
182
|
+
* @see https://developers.cloudflare.com/durable-objects/reference/data-location/
|
|
183
|
+
*/
|
|
139
184
|
type DurableObjectJurisdiction = "eu" | "fedramp" | "us";
|
|
140
185
|
/** Subset of `DurableObjectNamespace` the package consumes. */
|
|
141
186
|
interface DurableObjectNamespaceLike {
|
|
142
187
|
get: (id: DurableObjectIdLike) => DurableObjectStubLike;
|
|
143
188
|
idFromName: (name: string) => DurableObjectIdLike;
|
|
144
189
|
/**
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
190
|
+
* Derive a jurisdiction-restricted subnamespace. Optional because older
|
|
191
|
+
* workers-types releases (and test doubles) may not expose it.
|
|
192
|
+
*/
|
|
148
193
|
jurisdiction?: (jurisdiction: DurableObjectJurisdiction) => DurableObjectNamespaceLike;
|
|
149
194
|
}
|
|
150
195
|
interface DurableObjectIdLike {
|
|
@@ -157,18 +202,18 @@ interface LunoraSchedulerOptions {
|
|
|
157
202
|
/** Optional named instance — useful for tenant isolation. Default `default`. */
|
|
158
203
|
instanceName?: string;
|
|
159
204
|
/**
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
205
|
+
* Pin the SchedulerDO (durable timers + cron state) to a Cloudflare
|
|
206
|
+
* data-residency jurisdiction. Pass the same value as the worker's
|
|
207
|
+
* `jurisdiction` so scheduled state co-resides with app data. Omit for the
|
|
208
|
+
* un-pinned global namespace.
|
|
209
|
+
*/
|
|
165
210
|
jurisdiction?: DurableObjectJurisdiction;
|
|
166
211
|
/** Binding to the `SchedulerDO` durable object namespace. */
|
|
167
212
|
namespace: DurableObjectNamespaceLike;
|
|
168
213
|
/**
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
214
|
+
* Origin where the Worker is mounted. SchedulerDO uses this base URL when
|
|
215
|
+
* dispatching scheduled functions back to the Worker on alarm fire.
|
|
216
|
+
*/
|
|
172
217
|
originUrl: string;
|
|
173
218
|
}
|
|
174
219
|
/** Per-enqueue options for a {@link Workpool}. Extends {@link RunOptions} minus the implicit `pool` (the pool sets that). */
|
|
@@ -181,46 +226,46 @@ interface EnqueueOptions {
|
|
|
181
226
|
shardKey?: string;
|
|
182
227
|
}
|
|
183
228
|
/**
|
|
184
|
-
* Options for `createWorkpool`. Mirrors {@link LunoraSchedulerOptions}
|
|
185
|
-
* (same `namespace` / `originUrl` / `instanceName`) plus the bounded-concurrency
|
|
186
|
-
* controls. A workpool is a NAMED logical pool inside the existing SchedulerDO —
|
|
187
|
-
* it needs no extra Durable Object or wrangler binding beyond the SchedulerDO
|
|
188
|
-
* the scheduler already uses.
|
|
189
|
-
*/
|
|
229
|
+
* Options for `createWorkpool`. Mirrors {@link LunoraSchedulerOptions}
|
|
230
|
+
* (same `namespace` / `originUrl` / `instanceName`) plus the bounded-concurrency
|
|
231
|
+
* controls. A workpool is a NAMED logical pool inside the existing SchedulerDO —
|
|
232
|
+
* it needs no extra Durable Object or wrangler binding beyond the SchedulerDO
|
|
233
|
+
* the scheduler already uses.
|
|
234
|
+
*/
|
|
190
235
|
interface WorkpoolOptions extends LunoraSchedulerOptions {
|
|
191
236
|
/**
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
237
|
+
* Maximum number of jobs from this pool that may be in flight at once.
|
|
238
|
+
* Excess enqueues are persisted and drain as slots free. Must be a positive
|
|
239
|
+
* integer.
|
|
240
|
+
*/
|
|
196
241
|
maxConcurrency: number;
|
|
197
242
|
/**
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
243
|
+
* Pool name — the concurrency counter is keyed by this inside the
|
|
244
|
+
* SchedulerDO storage (`pool:<name>`). Default `default`.
|
|
245
|
+
*/
|
|
201
246
|
name?: string;
|
|
202
247
|
}
|
|
203
248
|
/**
|
|
204
|
-
* Bounded-concurrency action queue (Lunora equivalent of `@convex-dev/workpool`).
|
|
205
|
-
* Built on the existing SchedulerDO: `enqueue` schedules a job tagged with this
|
|
206
|
-
* pool's name; the DO caps simultaneous dispatch at `maxConcurrency` and queues
|
|
207
|
-
* the rest durably.
|
|
208
|
-
*/
|
|
249
|
+
* Bounded-concurrency action queue (Lunora equivalent of `@convex-dev/workpool`).
|
|
250
|
+
* Built on the existing SchedulerDO: `enqueue` schedules a job tagged with this
|
|
251
|
+
* pool's name; the DO caps simultaneous dispatch at `maxConcurrency` and queues
|
|
252
|
+
* the rest durably.
|
|
253
|
+
*/
|
|
209
254
|
interface Workpool {
|
|
210
255
|
/** Cancel a queued/in-flight pool job by id. */
|
|
211
256
|
cancel: (id: string) => Promise<{
|
|
212
257
|
cancelled: boolean;
|
|
213
258
|
}>;
|
|
214
259
|
/**
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
260
|
+
* Enqueue `function_(args)` into the pool. Resolves with the durable job id
|
|
261
|
+
* and the time it was scheduled for (it may not run immediately if the pool
|
|
262
|
+
* is at capacity).
|
|
263
|
+
*/
|
|
219
264
|
enqueue: <F extends FunctionReference>(function_: F, args: ArgsOf<F>, options?: EnqueueOptions) => Promise<{
|
|
220
265
|
id: string;
|
|
221
266
|
scheduledFor: number;
|
|
222
267
|
}>;
|
|
223
|
-
/** The pool's name (the `pool
|
|
268
|
+
/** The pool's name (the `pool:<name>` storage key suffix). */
|
|
224
269
|
readonly name: string;
|
|
225
270
|
/** Inspect the pool's current state — `inFlight` slots used and the configured `maxConcurrency`. */
|
|
226
271
|
status: () => Promise<{
|
|
@@ -289,11 +334,11 @@ interface QueueWorkpoolOptions {
|
|
|
289
334
|
queue: QueueLike<QueueJob>;
|
|
290
335
|
}
|
|
291
336
|
/**
|
|
292
|
-
* Queues-backed producer: enqueue function dispatches onto a Cloudflare Queue.
|
|
293
|
-
* Concurrency, retries, and dead-lettering are configured on the queue consumer
|
|
294
|
-
* in `wrangler.jsonc` (`max_concurrency` / `max_retries` / `dead_letter_queue`),
|
|
295
|
-
* not here — that's the whole point of using Queues over the DO workpool.
|
|
296
|
-
*/
|
|
337
|
+
* Queues-backed producer: enqueue function dispatches onto a Cloudflare Queue.
|
|
338
|
+
* Concurrency, retries, and dead-lettering are configured on the queue consumer
|
|
339
|
+
* in `wrangler.jsonc` (`max_concurrency` / `max_retries` / `dead_letter_queue`),
|
|
340
|
+
* not here — that's the whole point of using Queues over the DO workpool.
|
|
341
|
+
*/
|
|
297
342
|
interface QueueWorkpool {
|
|
298
343
|
/** Enqueue a single `fn(args)` dispatch. */
|
|
299
344
|
enqueue: <F extends FunctionReference>(function_: F, args: ArgsOf<F>, options?: QueueEnqueueOptions) => Promise<void>;
|
|
@@ -321,41 +366,41 @@ interface HttpDispatcherOptions {
|
|
|
321
366
|
originUrl: string;
|
|
322
367
|
}
|
|
323
368
|
/**
|
|
324
|
-
* Client-side scheduler — forwards `runAfter` / `runAt` / `cancel` calls to a
|
|
325
|
-
* `SchedulerDO` over HTTP. The DO owns the alarm and the storage; this is a
|
|
326
|
-
* thin RPC wrapper.
|
|
327
|
-
*/
|
|
369
|
+
* Client-side scheduler — forwards `runAfter` / `runAt` / `cancel` calls to a
|
|
370
|
+
* `SchedulerDO` over HTTP. The DO owns the alarm and the storage; this is a
|
|
371
|
+
* thin RPC wrapper.
|
|
372
|
+
*/
|
|
328
373
|
declare const createScheduler: (options: LunoraSchedulerOptions) => Scheduler;
|
|
329
374
|
/**
|
|
330
|
-
* Bounded-concurrency action queue — the Lunora equivalent of
|
|
331
|
-
* `@convex-dev/workpool`. Mirrors `createScheduler`'s `namespace` /
|
|
332
|
-
* `originUrl` / `instanceName` options and is built on the SAME `SchedulerDO`:
|
|
333
|
-
* a workpool is just a NAMED logical pool inside that DO (concurrency counter
|
|
334
|
-
* keyed by {@link WorkpoolOptions.name} under the `pool
|
|
335
|
-
* It needs no extra Durable Object or wrangler binding beyond the SchedulerDO
|
|
336
|
-
* the scheduler already uses.
|
|
337
|
-
*
|
|
338
|
-
* `enqueue` schedules a job tagged with this pool; the DO dispatches at most
|
|
339
|
-
* `maxConcurrency` of the pool's jobs at once and queues the rest durably,
|
|
340
|
-
* draining them as the runtime reports completions (`POST /complete`).
|
|
341
|
-
*
|
|
342
|
-
* ```ts
|
|
343
|
-
* const pool = createWorkpool({ namespace: env.SCHEDULER, originUrl, maxConcurrency: 5 });
|
|
344
|
-
* await pool.enqueue(internal.stripe.sync, { invoiceId }, { retry: { maxAttempts: 3 } });
|
|
345
|
-
* ```
|
|
346
|
-
*
|
|
347
|
-
* Why not Cloudflare Queues? Queues natively cover concurrency-capped, retried,
|
|
348
|
-
* dead-lettered, delayed dispatch (`max_concurrency`, `max_retries`,
|
|
349
|
-
* `retry({ delaySeconds })`, `dead_letter_queue`), and are the right tool when
|
|
350
|
-
* you just want to rate-limit fire-and-forget background work. This workpool
|
|
351
|
-
* deliberately stays on `SchedulerDO` because it offers what a queue can't: a
|
|
352
|
-
* hard concurrency cap (the DO is the single serialization point — no
|
|
353
|
-
* cross-consumer overshoot), per-job cancellation, and per-job status
|
|
354
|
-
* introspection, all keyed by a stable job id. Reach for Queues when you don't
|
|
355
|
-
* need those; reach for this when you do. Either way, do NOT grow multi-step
|
|
356
|
-
* orchestration on top of this — that's Cloudflare **Workflows** (`step.do` /
|
|
357
|
-
* `step.sleep` / `step.waitForEvent`).
|
|
358
|
-
*/
|
|
375
|
+
* Bounded-concurrency action queue — the Lunora equivalent of
|
|
376
|
+
* `@convex-dev/workpool`. Mirrors `createScheduler`'s `namespace` /
|
|
377
|
+
* `originUrl` / `instanceName` options and is built on the SAME `SchedulerDO`:
|
|
378
|
+
* a workpool is just a NAMED logical pool inside that DO (concurrency counter
|
|
379
|
+
* keyed by {@link WorkpoolOptions.name} under the `pool:<name>` storage key).
|
|
380
|
+
* It needs no extra Durable Object or wrangler binding beyond the SchedulerDO
|
|
381
|
+
* the scheduler already uses.
|
|
382
|
+
*
|
|
383
|
+
* `enqueue` schedules a job tagged with this pool; the DO dispatches at most
|
|
384
|
+
* `maxConcurrency` of the pool's jobs at once and queues the rest durably,
|
|
385
|
+
* draining them as the runtime reports completions (`POST /complete`).
|
|
386
|
+
*
|
|
387
|
+
* ```ts
|
|
388
|
+
* const pool = createWorkpool({ namespace: env.SCHEDULER, originUrl, maxConcurrency: 5 });
|
|
389
|
+
* await pool.enqueue(internal.stripe.sync, { invoiceId }, { retry: { maxAttempts: 3 } });
|
|
390
|
+
* ```
|
|
391
|
+
*
|
|
392
|
+
* Why not Cloudflare Queues? Queues natively cover concurrency-capped, retried,
|
|
393
|
+
* dead-lettered, delayed dispatch (`max_concurrency`, `max_retries`,
|
|
394
|
+
* `retry({ delaySeconds })`, `dead_letter_queue`), and are the right tool when
|
|
395
|
+
* you just want to rate-limit fire-and-forget background work. This workpool
|
|
396
|
+
* deliberately stays on `SchedulerDO` because it offers what a queue can't: a
|
|
397
|
+
* hard concurrency cap (the DO is the single serialization point — no
|
|
398
|
+
* cross-consumer overshoot), per-job cancellation, and per-job status
|
|
399
|
+
* introspection, all keyed by a stable job id. Reach for Queues when you don't
|
|
400
|
+
* need those; reach for this when you do. Either way, do NOT grow multi-step
|
|
401
|
+
* orchestration on top of this — that's Cloudflare **Workflows** (`step.do` /
|
|
402
|
+
* `step.sleep` / `step.waitForEvent`).
|
|
403
|
+
*/
|
|
359
404
|
declare const createWorkpool: (options: WorkpoolOptions) => Workpool;
|
|
360
405
|
interface CronTriggerOptions {
|
|
361
406
|
/** Args passed to the function. */
|
|
@@ -377,10 +422,10 @@ interface CronTriggerSnippet {
|
|
|
377
422
|
wranglerJsonc: string;
|
|
378
423
|
}
|
|
379
424
|
/**
|
|
380
|
-
* Produces the wrangler.jsonc fragment + dispatcher metadata for a recurring
|
|
381
|
-
* function. The actual cron handler is mounted by `@lunora/runtime` — we only
|
|
382
|
-
* emit the configuration here.
|
|
383
|
-
*/
|
|
425
|
+
* Produces the wrangler.jsonc fragment + dispatcher metadata for a recurring
|
|
426
|
+
* function. The actual cron handler is mounted by `@lunora/runtime` — we only
|
|
427
|
+
* emit the configuration here.
|
|
428
|
+
*/
|
|
384
429
|
declare const createCronTrigger: (options: CronTriggerOptions) => CronTriggerSnippet;
|
|
385
430
|
/** Sub-day recurrence. Exactly one unit must be provided. */
|
|
386
431
|
interface IntervalSchedule {
|
|
@@ -395,6 +440,19 @@ interface DailySchedule {
|
|
|
395
440
|
/** 0–59. */
|
|
396
441
|
minuteUTC: number;
|
|
397
442
|
}
|
|
443
|
+
/**
|
|
444
|
+
* Hourly recurrence at a fixed minute past the hour.
|
|
445
|
+
*
|
|
446
|
+
* `crons.interval({ hours: 1 })` compiles to the same expression, but the
|
|
447
|
+
* asymmetry of having `daily`/`weekly`/`monthly` and no `hourly` is its own
|
|
448
|
+
* papercut — and unlike the interval form this one lets
|
|
449
|
+
* the caller place the job off the hour boundary, which is how you stop a
|
|
450
|
+
* dozen hourly jobs from stampeding at `:00`.
|
|
451
|
+
*/
|
|
452
|
+
interface HourlySchedule {
|
|
453
|
+
/** 0–59. */
|
|
454
|
+
minuteUTC: number;
|
|
455
|
+
}
|
|
398
456
|
/** Weekly recurrence at a fixed UTC time on a given weekday. */
|
|
399
457
|
interface WeeklySchedule extends DailySchedule {
|
|
400
458
|
/** Long weekday name, case-insensitive (e.g. `"monday"`). */
|
|
@@ -406,106 +464,119 @@ interface MonthlySchedule extends DailySchedule {
|
|
|
406
464
|
day: number;
|
|
407
465
|
}
|
|
408
466
|
/**
|
|
409
|
-
* One registered cron job, normalized to a compiled cron expression. Shared
|
|
410
|
-
* verbatim with `@lunora/codegen` (which lifts the same fields out of the AST)
|
|
411
|
-
* and the runtime dispatcher — keep the shape stable across all three.
|
|
412
|
-
*/
|
|
467
|
+
* One registered cron job, normalized to a compiled cron expression. Shared
|
|
468
|
+
* verbatim with `@lunora/codegen` (which lifts the same fields out of the AST)
|
|
469
|
+
* and the runtime dispatcher — keep the shape stable across all three.
|
|
470
|
+
*/
|
|
413
471
|
interface CronJob {
|
|
414
472
|
/** Args forwarded to the function (or, for a workflow target, used as its `params`) on each fire. */
|
|
415
473
|
args: Record<string, unknown>;
|
|
416
474
|
/** Compiled standard cron expression, e.g. `"0 9 * * *"`. */
|
|
417
475
|
cron: string;
|
|
418
476
|
/**
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
477
|
+
* `__lunoraRef` of the target function. Present for a function target;
|
|
478
|
+
* absent when the job targets a workflow ({@link CronJob.workflow} instead).
|
|
479
|
+
*/
|
|
422
480
|
functionPath?: string;
|
|
423
481
|
/** Human-readable identifier — must be unique within one `cronJobs()`. */
|
|
424
482
|
name: string;
|
|
425
483
|
/**
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
484
|
+
* Set when the job targets a durable workflow rather than a function: the
|
|
485
|
+
* workflow's stable name (`defineWorkflow({ name })`) when one was declared,
|
|
486
|
+
* otherwise `""`. `@lunora/codegen` statically resolves the concrete
|
|
487
|
+
* `lunora/workflows.ts` export + its `WORKFLOW_*` binding for the emitted
|
|
488
|
+
* dispatch map, so this authoring-time value is informational only.
|
|
489
|
+
*/
|
|
432
490
|
workflow?: string;
|
|
433
491
|
}
|
|
434
492
|
/** The ergonomic builder methods, excluding the raw `.cron` escape hatch. */
|
|
435
|
-
type CronScheduleKind = "daily" | "interval" | "monthly" | "weekly";
|
|
493
|
+
type CronScheduleKind = "daily" | "hourly" | "interval" | "monthly" | "weekly";
|
|
436
494
|
/** The ergonomic schedule kinds as a runtime set (codegen reads this to detect cron builder methods). */
|
|
437
495
|
declare const CRON_SCHEDULE_KINDS: ReadonlySet<CronScheduleKind>;
|
|
438
496
|
/**
|
|
439
|
-
* Compile one of the ergonomic schedule forms into a standard cron expression.
|
|
440
|
-
* Exposed as a pure function so `@lunora/codegen` can reuse the exact same
|
|
441
|
-
* compilation when it statically lifts a `crons.{kind}(...)` call out of the
|
|
442
|
-
* AST — codegen imports this directly (no duplicated mirror).
|
|
443
|
-
|
|
444
|
-
|
|
497
|
+
* Compile one of the ergonomic schedule forms into a standard cron expression.
|
|
498
|
+
* Exposed as a pure function so `@lunora/codegen` can reuse the exact same
|
|
499
|
+
* compilation when it statically lifts a `crons.{kind}(...)` call out of the
|
|
500
|
+
* AST — codegen imports this directly (no duplicated mirror). `jobName` is
|
|
501
|
+
* optional (see {@link compileInterval}) so codegen's existing 2-arg call site
|
|
502
|
+
* keeps working unchanged.
|
|
503
|
+
*/
|
|
504
|
+
declare const compileCronSchedule: (kind: CronScheduleKind, schedule: DailySchedule | HourlySchedule | IntervalSchedule | MonthlySchedule | WeeklySchedule, jobName?: string) => string;
|
|
445
505
|
/**
|
|
446
|
-
* Builder returned by {@link cronJobs}. Each method registers one recurring
|
|
447
|
-
* job; the compiled expression is validated immediately so authoring mistakes
|
|
448
|
-
* surface at definition time rather than at codegen.
|
|
449
|
-
*/
|
|
506
|
+
* Builder returned by {@link cronJobs}. Each method registers one recurring
|
|
507
|
+
* job; the compiled expression is validated immediately so authoring mistakes
|
|
508
|
+
* surface at definition time rather than at codegen.
|
|
509
|
+
*/
|
|
450
510
|
interface CronJobsBuilder {
|
|
451
511
|
/**
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
512
|
+
* Raw cron expression escape hatch (5- or 6-field, full cron-parser grammar).
|
|
513
|
+
* The target may be a function (`internal.file.fn`) or a durable workflow
|
|
514
|
+
* (`workflows.<name>`); a workflow's `args` are inferred from its `params`.
|
|
515
|
+
*/
|
|
456
516
|
cron: <T extends CronTarget>(name: string, cronExpr: string, target: T, args?: CronTargetArgs<T>) => CronJobsBuilder;
|
|
457
|
-
/** Daily at `hourUTC:minuteUTC` (UTC). The target may be a function or a durable workflow (`workflows
|
|
517
|
+
/** Daily at `hourUTC:minuteUTC` (UTC). The target may be a function or a durable workflow (`workflows.<name>`). */
|
|
458
518
|
daily: <T extends CronTarget>(name: string, schedule: DailySchedule, target: T, args?: CronTargetArgs<T>) => CronJobsBuilder;
|
|
459
|
-
/**
|
|
519
|
+
/** Hourly at `minuteUTC` past the hour. The target may be a function or a durable workflow (`workflows.<name>`). */
|
|
520
|
+
hourly: <T extends CronTarget>(name: string, schedule: HourlySchedule, target: T, args?: CronTargetArgs<T>) => CronJobsBuilder;
|
|
521
|
+
/** Every `{ seconds | minutes | hours }`. The target may be a function or a durable workflow (`workflows.<name>`). */
|
|
460
522
|
interval: <T extends CronTarget>(name: string, schedule: IntervalSchedule, target: T, args?: CronTargetArgs<T>) => CronJobsBuilder;
|
|
461
523
|
/** Snapshot of the registered jobs, in declaration order. */
|
|
462
524
|
jobs: () => ReadonlyArray<CronJob>;
|
|
463
|
-
/** Monthly on `day` at `hourUTC:minuteUTC` (UTC). The target may be a function or a durable workflow (`workflows
|
|
525
|
+
/** Monthly on `day` at `hourUTC:minuteUTC` (UTC). The target may be a function or a durable workflow (`workflows.<name>`). */
|
|
464
526
|
monthly: <T extends CronTarget>(name: string, schedule: MonthlySchedule, target: T, args?: CronTargetArgs<T>) => CronJobsBuilder;
|
|
465
|
-
/** Weekly on `dayOfWeek` at `hourUTC:minuteUTC` (UTC). The target may be a function or a durable workflow (`workflows
|
|
527
|
+
/** Weekly on `dayOfWeek` at `hourUTC:minuteUTC` (UTC). The target may be a function or a durable workflow (`workflows.<name>`). */
|
|
466
528
|
weekly: <T extends CronTarget>(name: string, schedule: WeeklySchedule, target: T, args?: CronTargetArgs<T>) => CronJobsBuilder;
|
|
467
529
|
}
|
|
468
530
|
/**
|
|
469
|
-
* Create a code-first cron registry. The returned builder is chainable;
|
|
470
|
-
* codegen discovers a `lunora/crons.ts` default export by AST, not a runtime
|
|
471
|
-
* brand.
|
|
472
|
-
*/
|
|
531
|
+
* Create a code-first cron registry. The returned builder is chainable;
|
|
532
|
+
* codegen discovers a `lunora/crons.ts` default export by AST, not a runtime
|
|
533
|
+
* brand.
|
|
534
|
+
*/
|
|
473
535
|
declare const cronJobs: () => CronJobsBuilder;
|
|
474
536
|
/**
|
|
475
|
-
* Build a Queues producer that enqueues Lunora function dispatches. Concurrency
|
|
476
|
-
* and retry policy live on the consumer's `wrangler.jsonc` config, not here.
|
|
477
|
-
*/
|
|
537
|
+
* Build a Queues producer that enqueues Lunora function dispatches. Concurrency
|
|
538
|
+
* and retry policy live on the consumer's `wrangler.jsonc` config, not here.
|
|
539
|
+
*/
|
|
478
540
|
declare const createQueueWorkpool: (options: QueueWorkpoolOptions) => QueueWorkpool;
|
|
479
541
|
/**
|
|
480
|
-
* Wrap a {@link QueueDispatch} into a Cloudflare `queue()` consumer handler.
|
|
481
|
-
*
|
|
482
|
-
* Each message is dispatched independently (concurrently across the batch). On
|
|
483
|
-
* success the message is `ack()`-ed; on any failure — a thrown dispatcher or a
|
|
484
|
-
* structurally-invalid body — it is `retry()`-ed, so Queues' own `max_retries`
|
|
485
|
-
* + `dead_letter_queue` settings decide when to give up. Nothing is silently
|
|
486
|
-
* dropped: a permanently-bad message rides retries into the dead-letter queue
|
|
487
|
-
* where you can inspect it.
|
|
488
|
-
*/
|
|
542
|
+
* Wrap a {@link QueueDispatch} into a Cloudflare `queue()` consumer handler.
|
|
543
|
+
*
|
|
544
|
+
* Each message is dispatched independently (concurrently across the batch). On
|
|
545
|
+
* success the message is `ack()`-ed; on any failure — a thrown dispatcher or a
|
|
546
|
+
* structurally-invalid body — it is `retry()`-ed, so Queues' own `max_retries`
|
|
547
|
+
* + `dead_letter_queue` settings decide when to give up. Nothing is silently
|
|
548
|
+
* dropped: a permanently-bad message rides retries into the dead-letter queue
|
|
549
|
+
* where you can inspect it.
|
|
550
|
+
*/
|
|
489
551
|
declare const createQueueConsumer: (options: QueueConsumerOptions) => ((batch: MessageBatchLike) => Promise<void>);
|
|
490
552
|
/**
|
|
491
|
-
* Default {@link QueueDispatch}: POST each job to the Worker's
|
|
492
|
-
* `/_lunora/scheduler/dispatch` endpoint (the same path SchedulerDO dispatches
|
|
493
|
-
* through), authenticated with the admin bearer. A non-2xx response throws so
|
|
494
|
-
* the consumer retries the message.
|
|
495
|
-
*/
|
|
553
|
+
* Default {@link QueueDispatch}: POST each job to the Worker's
|
|
554
|
+
* `/_lunora/scheduler/dispatch` endpoint (the same path SchedulerDO dispatches
|
|
555
|
+
* through), authenticated with the admin bearer. A non-2xx response throws so
|
|
556
|
+
* the consumer retries the message.
|
|
557
|
+
*/
|
|
496
558
|
declare const httpDispatcher: (options: HttpDispatcherOptions) => QueueDispatch;
|
|
497
559
|
/**
|
|
498
|
-
* Minimal projection of `DurableObjectState` for the SchedulerDO. Declared
|
|
499
|
-
* structurally so unit tests can pass a fake state without booting the
|
|
500
|
-
* workers runtime. The WebSocket methods are optional: they back the live
|
|
501
|
-
* `/ws` subscription (push the job list on every change) and are absent in the
|
|
502
|
-
* storage-only fakes, in which case the DO simply serves no live sockets.
|
|
503
|
-
*/
|
|
560
|
+
* Minimal projection of `DurableObjectState` for the SchedulerDO. Declared
|
|
561
|
+
* structurally so unit tests can pass a fake state without booting the
|
|
562
|
+
* workers runtime. The WebSocket methods are optional: they back the live
|
|
563
|
+
* `/ws` subscription (push the job list on every change) and are absent in the
|
|
564
|
+
* storage-only fakes, in which case the DO simply serves no live sockets.
|
|
565
|
+
*/
|
|
504
566
|
interface SchedulerDOState {
|
|
505
567
|
/** Accept a hibernatable server WebSocket (workers `state.acceptWebSocket`). */
|
|
506
568
|
acceptWebSocket?: (ws: WebSocket) => void;
|
|
507
569
|
/** Every accepted server WebSocket (workers `state.getWebSockets`). */
|
|
508
570
|
getWebSockets?: () => WebSocket[];
|
|
571
|
+
/**
|
|
572
|
+
* Register a constant ping/pong auto-response so the runtime answers a
|
|
573
|
+
* known keepalive frame on a hibernated socket WITHOUT waking this DO (no
|
|
574
|
+
* billable request, no dispatch). Optional: absent in the unit harness and
|
|
575
|
+
* older runtimes, present on the real `DurableObjectState`. Mirrors
|
|
576
|
+
* `@lunora/do`'s `ShardDOState.setWebSocketAutoResponse` — see
|
|
577
|
+
* {@link SchedulerDO.armWebSocketKeepalive}.
|
|
578
|
+
*/
|
|
579
|
+
setWebSocketAutoResponse?: (pair: WebSocketRequestResponsePair) => void;
|
|
509
580
|
storage: {
|
|
510
581
|
delete: (key: string | string[]) => Promise<number | boolean>;
|
|
511
582
|
deleteAlarm: () => Promise<void> | void;
|
|
@@ -515,6 +586,7 @@ interface SchedulerDOState {
|
|
|
515
586
|
end?: string;
|
|
516
587
|
limit?: number;
|
|
517
588
|
prefix?: string;
|
|
589
|
+
startAfter?: string;
|
|
518
590
|
}) => Promise<Map<string, T>>;
|
|
519
591
|
put: <T = unknown>(entries: Record<string, T> | string, value?: T) => Promise<void>;
|
|
520
592
|
setAlarm: (scheduledTime: number | Date) => Promise<void> | void;
|
|
@@ -523,100 +595,107 @@ interface SchedulerDOState {
|
|
|
523
595
|
interface SchedulerEnv {
|
|
524
596
|
[key: string]: unknown;
|
|
525
597
|
/**
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
598
|
+
* Fallback bearer token attached to the dispatch when
|
|
599
|
+
* {@link SchedulerEnv.LUNORA_SCHEDULER_SECRET} is not configured. Sent as
|
|
600
|
+
* `authorization: Bearer <token>`.
|
|
601
|
+
*/
|
|
530
602
|
LUNORA_ADMIN_TOKEN?: string;
|
|
531
603
|
/**
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
604
|
+
* Base URL where the Worker is mounted. SchedulerDO uses this at dispatch
|
|
605
|
+
* time to call back into the Worker. Read at fire time (NOT taken from the
|
|
606
|
+
* request body) to prevent SSRF via a forged `originUrl` field.
|
|
607
|
+
*/
|
|
536
608
|
LUNORA_ORIGIN_URL?: string;
|
|
537
609
|
/**
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
610
|
+
* Shared secret used to HMAC-sign the dispatch body so the runtime receiver
|
|
611
|
+
* can authenticate the call (header `x-lunora-scheduler-signature`). Without
|
|
612
|
+
* it the dispatch is sent unsigned (optionally bearer-authenticated via
|
|
613
|
+
* {@link SchedulerEnv.LUNORA_ADMIN_TOKEN}).
|
|
614
|
+
*/
|
|
543
615
|
LUNORA_SCHEDULER_SECRET?: string;
|
|
544
616
|
}
|
|
545
617
|
/**
|
|
546
|
-
* One pool's live backlog, as surfaced by `GET /status`. `inFlight`/
|
|
547
|
-
* `maxConcurrency` mirror the durable {@link PoolState} semaphore; `queued`
|
|
548
|
-
* is the number of pending (not-yet-dispatched) jobs routed to this pool.
|
|
549
|
-
*/
|
|
618
|
+
* One pool's live backlog, as surfaced by `GET /status`. `inFlight`/
|
|
619
|
+
* `maxConcurrency` mirror the durable {@link PoolState} semaphore; `queued`
|
|
620
|
+
* is the number of pending (not-yet-dispatched) jobs routed to this pool.
|
|
621
|
+
*/
|
|
550
622
|
interface SchedulerPoolStatus {
|
|
551
623
|
/** Jobs currently dispatched-but-not-yet-completed (the held slots). */
|
|
552
624
|
inFlight: number;
|
|
553
625
|
/** The pool's concurrency cap. */
|
|
554
626
|
maxConcurrency: number;
|
|
555
|
-
/** The logical workpool name (the `pool
|
|
627
|
+
/** The logical workpool name (the `pool:<name>` suffix). */
|
|
556
628
|
name: string;
|
|
557
629
|
/** Pending jobs routed to this pool but not yet dispatched. */
|
|
558
630
|
queued: number;
|
|
559
631
|
}
|
|
560
632
|
/**
|
|
561
|
-
* App-level scheduler backlog, as returned by `GET /status`. `pools` carries
|
|
562
|
-
* the per-pool breakdown; `backlog` and `inFlight` are the app-wide sums of
|
|
563
|
-
* `queued` and `inFlight` across every pool — the SLO view's headline numbers.
|
|
564
|
-
*/
|
|
633
|
+
* App-level scheduler backlog, as returned by `GET /status`. `pools` carries
|
|
634
|
+
* the per-pool breakdown; `backlog` and `inFlight` are the app-wide sums of
|
|
635
|
+
* `queued` and `inFlight` across every pool — the SLO view's headline numbers.
|
|
636
|
+
*/
|
|
565
637
|
interface SchedulerStatus {
|
|
566
638
|
/** Sum of every pool's `queued` count — the total pending backlog. */
|
|
567
639
|
backlog: number;
|
|
568
640
|
/** Sum of every pool's `inFlight` count — the total held concurrency slots. */
|
|
569
641
|
inFlight: number;
|
|
570
|
-
/** Per-pool backlog breakdown, one entry per `pool
|
|
642
|
+
/** Per-pool backlog breakdown, one entry per `pool:<name>` record. */
|
|
571
643
|
pools: SchedulerPoolStatus[];
|
|
572
644
|
}
|
|
573
645
|
/**
|
|
574
|
-
* Durable Object that stores pending scheduled invocations sorted by their
|
|
575
|
-
* `scheduledFor` time and fires them via HTTP on alarm. Storage layout:
|
|
576
|
-
* `id
|
|
577
|
-
* id (used as a sorted index).
|
|
578
|
-
*
|
|
579
|
-
* On every mutation the DO recomputes the earliest pending task and updates
|
|
580
|
-
* the alarm via `state.storage.setAlarm(time)`.
|
|
581
|
-
*/
|
|
646
|
+
* Durable Object that stores pending scheduled invocations sorted by their
|
|
647
|
+
* `scheduledFor` time and fires them via HTTP on alarm. Storage layout:
|
|
648
|
+
* `id:<id>` maps to {@link ScheduleRecord}; `t:<paddedTime>:<id>` maps to the
|
|
649
|
+
* id (used as a sorted index).
|
|
650
|
+
*
|
|
651
|
+
* On every mutation the DO recomputes the earliest pending task and updates
|
|
652
|
+
* the alarm via `state.storage.setAlarm(time)`.
|
|
653
|
+
*/
|
|
582
654
|
declare class SchedulerDO {
|
|
583
655
|
private static indexKey;
|
|
584
656
|
private static json;
|
|
585
657
|
private static error;
|
|
586
658
|
/**
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
659
|
+
* Resolve the effective retry parameters for a record: its per-job
|
|
660
|
+
* {@link RetryPolicy} merged over the DO's built-in defaults. Callers that
|
|
661
|
+
* never set `record.retry` get today's behaviour verbatim
|
|
662
|
+
* (`maxAttempts: 5`, exponential, `baseMs: 30_000`, no ceiling).
|
|
663
|
+
*/
|
|
592
664
|
private static resolveRetry;
|
|
593
665
|
/** Clamp an untrusted `maxConcurrency` to a positive integer, else fall back. */
|
|
594
666
|
private static normalizeConcurrency;
|
|
595
667
|
/**
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
668
|
+
* Sanitize an untrusted retry policy from the wire into a `RetryPolicy` (or
|
|
669
|
+
* `undefined` when nothing valid was provided). Keeps obviously-bad values
|
|
670
|
+
* out of storage so {@link SchedulerDO.resolveRetry} never has to re-guard.
|
|
671
|
+
* @returns The normalized policy, or `undefined` if no valid policy was found.
|
|
672
|
+
*/
|
|
601
673
|
private static normalizeRetry;
|
|
602
674
|
/**
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
675
|
+
* Idempotently release the slot held by `jobId`, returning the updated
|
|
676
|
+
* {@link PoolState} (pure — the caller persists it). A duplicate release for
|
|
677
|
+
* an id that no longer holds a slot is a no-op, so an at-least-once
|
|
678
|
+
* `/complete` (or a complete racing a failed-kick release) can never push
|
|
679
|
+
* `inFlight` below the true number of running jobs and oversubscribe the
|
|
680
|
+
* pool. Pools persisted before `inFlightIds` existed fall back to a clamped
|
|
681
|
+
* counter decrement.
|
|
682
|
+
*/
|
|
611
683
|
private static releaseSlot;
|
|
612
684
|
/**
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
685
|
+
* Best-effort release with no job id (legacy `/complete` payloads). Drops one
|
|
686
|
+
* tracked id if the set exists, else clamps the counter. Less precise than
|
|
687
|
+
* {@link SchedulerDO.releaseSlot} — a duplicate id-less complete CAN
|
|
688
|
+
* over-release — but every current client sends the id, so this is the
|
|
689
|
+
* compatibility shim, not the hot path.
|
|
690
|
+
*/
|
|
619
691
|
private static releaseFirstSlot;
|
|
692
|
+
/**
|
|
693
|
+
* Normalize the mutually-exclusive dispatch target off an untrusted body: a
|
|
694
|
+
* one-shot function path (`functionPath`) or a durable workflow/agent
|
|
695
|
+
* instance (`workflow`, a `WORKFLOW_*`/`AGENT_*` binding). Returns `undefined`
|
|
696
|
+
* when neither is present so the caller can reject the schedule.
|
|
697
|
+
*/
|
|
698
|
+
private static resolveScheduleTarget;
|
|
620
699
|
protected readonly state: SchedulerDOState;
|
|
621
700
|
protected readonly env: SchedulerEnv;
|
|
622
701
|
constructor(state: SchedulerDOState, env: SchedulerEnv);
|
|
@@ -624,180 +703,267 @@ declare class SchedulerDO {
|
|
|
624
703
|
/** Called by the Workers runtime when the alarm previously set by `_rescheduleAlarm()` fires. */
|
|
625
704
|
alarm(): Promise<void>;
|
|
626
705
|
/**
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
706
|
+
* Internal dispatch hook; overridden in unit tests to capture the outgoing
|
|
707
|
+
* request. Returns `true` ONLY on an explicit 2xx response (`response.ok`).
|
|
708
|
+
* Anything else — a network failure, a 5xx, OR a non-2xx such as 404
|
|
709
|
+
* (receiver route not mounted) / 401 / 403 / 4xx — returns `false` and
|
|
710
|
+
* enters the retry pipeline via {@link recordRetry}. Treating 4xx as
|
|
711
|
+
* success used to permanently delete the job; since the receiver may simply
|
|
712
|
+
* be missing (404) or transiently failing, we retry rather than silently
|
|
713
|
+
* drop. After {@link MAX_RETRY_ATTEMPTS} the record is parked under a
|
|
714
|
+
* `dead:` key for inspection — never silently deleted.
|
|
715
|
+
*
|
|
716
|
+
* The dispatch target is taken from `env.LUNORA_ORIGIN_URL` (NOT from the
|
|
717
|
+
* stored record) to prevent SSRF via a forged `originUrl` on the schedule
|
|
718
|
+
* request. If that env var is missing at fire time (a deploy/binding
|
|
719
|
+
* regression — schedule time already enforced its presence) we return
|
|
720
|
+
* `false` so the record is retried rather than silently dropped.
|
|
721
|
+
*/
|
|
643
722
|
protected dispatch(record: ScheduleRecord): Promise<boolean>;
|
|
644
723
|
/**
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
724
|
+
* Register the hibernation-safe ping/pong keepalive. The runtime answers a
|
|
725
|
+
* {@link WS_KEEPALIVE_PING} text frame with {@link WS_KEEPALIVE_PONG}
|
|
726
|
+
* WITHOUT waking this Durable Object, keeping an idle `/ws` subscription
|
|
727
|
+
* alive across hibernation with no billable wakeup and no dispatch. Without
|
|
728
|
+
* this, a client's heartbeat ping goes unanswered and its watchdog force-
|
|
729
|
+
* closes the socket every ~90s, defeating hibernation (each unanswered ping
|
|
730
|
+
* wakes the DO to reconnect) — mirrors `@lunora/do`'s
|
|
731
|
+
* `ShardDO.armWebSocketKeepalive`. The auto-response is per-instance, so
|
|
732
|
+
* this re-runs on every construction (including a post-hibernation wake).
|
|
733
|
+
* Guarded: the API and the `WebSocketRequestResponsePair` global are absent
|
|
734
|
+
* in the unit harness and on older runtimes, where it degrades to a no-op.
|
|
735
|
+
*/
|
|
736
|
+
private armWebSocketKeepalive;
|
|
737
|
+
/**
|
|
738
|
+
* Claim + drain one due record with per-record fault isolation, so a storage
|
|
739
|
+
* throw can never abort the whole alarm pass (which would skip the remaining
|
|
740
|
+
* due records and the `rescheduleAlarm()` that re-arms the clock).
|
|
741
|
+
*
|
|
742
|
+
* Claims the job by deleting its time-index entry BEFORE dispatch (an alarm
|
|
743
|
+
* re-fire then won't pick it up again), runs {@link drainRecord}, and on a
|
|
744
|
+
* thrown storage op re-asserts the claim so the job stays re-fireable.
|
|
745
|
+
*
|
|
746
|
+
* A throw reaching here always means the job was NOT dispatched:
|
|
747
|
+
* {@link drainRecord} swallows its own post-dispatch cleanup errors and
|
|
748
|
+
* returns instead of throwing once a kick succeeds, so every escaping throw
|
|
749
|
+
* comes from the pre-dispatch or failed-dispatch paths. We therefore always
|
|
750
|
+
* re-assert the time-index claim so a later alarm re-attempts it
|
|
751
|
+
* (at-least-once): the claim delete may have removed it and
|
|
752
|
+
* recordRetry()/requeuePooled() may not have re-armed it before throwing, and
|
|
753
|
+
* re-inserting the same key is idempotent, so a surviving claim is simply
|
|
754
|
+
* rewritten to its prior value.
|
|
755
|
+
*/
|
|
661
756
|
private drainRecordGuarded;
|
|
662
757
|
/**
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
758
|
+
* Process one due (already index-claimed) record within an alarm drain:
|
|
759
|
+
* apply the workpool concurrency gate, dispatch, and settle the result.
|
|
760
|
+
* A saturated pool re-arms the job (backpressure, no attempt charged); a
|
|
761
|
+
* free slot is reserved durably before dispatch and released immediately if
|
|
762
|
+
* the kick fails (success holds it until the runtime reports completion).
|
|
763
|
+
* Success clears the `id:`/`retry:` rows; failure routes to
|
|
764
|
+
* {@link recordRetry}. Pool state is read FRESH from storage per record (see
|
|
765
|
+
* {@link reservePoolSlot}) and never held across the dispatch() await, so a
|
|
766
|
+
* concurrent /complete landing mid-dispatch can't be clobbered.
|
|
767
|
+
* Once a kick succeeds, post-dispatch cleanup (clearing the `id:`/`retry:`
|
|
768
|
+
* rows) is swallowed rather than allowed to throw, so a successful dispatch
|
|
769
|
+
* NEVER propagates an error to {@link drainRecordGuarded}: every throw that
|
|
770
|
+
* escapes comes from the pre-dispatch or failed-dispatch paths, where the job
|
|
771
|
+
* is still re-fireable and the guard safely re-claims the time index.
|
|
772
|
+
* @returns `true` only when the record was successfully dispatched (a 2xx
|
|
773
|
+
* kick); `false` on pool backpressure or a failed dispatch (the job is still
|
|
774
|
+
* re-fireable — already re-armed here). The value is informational (the guard
|
|
775
|
+
* branches on throw/no-throw, not on this boolean).
|
|
776
|
+
*/
|
|
680
777
|
private drainRecord;
|
|
681
778
|
/**
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
779
|
+
* Concurrency gate for a pooled record. Returns `false` (and re-arms the
|
|
780
|
+
* job via {@link requeuePooled}) when the pool is at `maxConcurrency`;
|
|
781
|
+
* otherwise reserves a slot durably and returns `true`. Non-pooled records
|
|
782
|
+
* always return `true` without touching any pool state.
|
|
783
|
+
*
|
|
784
|
+
* The pool row is read FRESH from storage on every call — never cached
|
|
785
|
+
* across the drain. Each reservation durably `savePool()`s before the next
|
|
786
|
+
* record runs, so a same-pass reservation is still visible to the next
|
|
787
|
+
* record's fresh read (the budget carries forward); and because dispatch()
|
|
788
|
+
* awaits an outbound fetch between records, a concurrent /complete that
|
|
789
|
+
* decrements the row mid-drain IS reflected here instead of being clobbered
|
|
790
|
+
* by a stale in-memory copy (which would leak a slot permanently).
|
|
791
|
+
*/
|
|
687
792
|
private reservePoolSlot;
|
|
688
793
|
/**
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
794
|
+
* Accept a hibernatable live subscription to the job list. The scheduler has
|
|
795
|
+
* exactly one subscription shape (the whole list), so there's no per-socket
|
|
796
|
+
* registry or dependency tracking — every accepted socket gets the full list
|
|
797
|
+
* on connect and on every change. The worker is responsible for gating the
|
|
798
|
+
* upgrade behind the admin token before it reaches here.
|
|
799
|
+
*/
|
|
695
800
|
private handleWebSocketUpgrade;
|
|
696
801
|
/**
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
802
|
+
* Re-list the jobs (bounded — see {@link listRecords}) and push them to
|
|
803
|
+
* every connected subscriber. Called after any change (schedule / cancel /
|
|
804
|
+
* alarm-fire) so live studios reflect it immediately. A no-op when the
|
|
805
|
+
* runtime doesn't support hibernated sockets.
|
|
806
|
+
*/
|
|
701
807
|
private broadcastChange;
|
|
702
|
-
/**
|
|
808
|
+
/**
|
|
809
|
+
* The current pending job records (shared by `/list` and the live channel),
|
|
810
|
+
* bounded to `limit` (default {@link DEFAULT_LIST_LIMIT}) so a large backlog
|
|
811
|
+
* can't be JSON-serialized and fanned out to every socket in one shot. Lists
|
|
812
|
+
* `limit + 1` and slices back down so `truncated` reflects whether there was
|
|
813
|
+
* a next row, without a second round-trip.
|
|
814
|
+
*/
|
|
703
815
|
private listRecords;
|
|
704
816
|
/**
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
817
|
+
* Page through every `id:` header exactly once with bounded per-page memory
|
|
818
|
+
* (a `limit`+`startAfter` cursor loop), invoking `visit` for each record.
|
|
819
|
+
* Unlike {@link listRecords}, which intentionally truncates for the studio's
|
|
820
|
+
* live view, `/status` and `/pool` need EXACT counts — this walks the full
|
|
821
|
+
* set, but never materializes more than one page at a time.
|
|
822
|
+
*/
|
|
823
|
+
private countHeaders;
|
|
824
|
+
/**
|
|
825
|
+
* HMAC-SHA-256 sign the dispatch body with `env.LUNORA_SCHEDULER_SECRET`,
|
|
826
|
+
* returning a base64url signature, or `undefined` when no secret is
|
|
827
|
+
* configured. Mirrors `@lunora/storage`'s signed-URL HMAC pattern (WebCrypto
|
|
828
|
+
* `crypto.subtle`, available in workerd).
|
|
829
|
+
*/
|
|
710
830
|
private signDispatch;
|
|
711
831
|
/**
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
832
|
+
* Move a failed record into the retry pipeline with configurable backoff.
|
|
833
|
+
* The retry budget/backoff comes from the record's {@link RetryPolicy}
|
|
834
|
+
* (falling back to the DO defaults); on exhaustion the record is parked
|
|
835
|
+
* under a `dead:` key for manual inspection.
|
|
836
|
+
*/
|
|
717
837
|
private recordRetry;
|
|
718
|
-
/** Read the durable `pool
|
|
838
|
+
/** Read the durable `pool:<name>` row, defaulting to a fresh `inFlight: 0` pool. */
|
|
719
839
|
private loadPool;
|
|
720
840
|
private savePool;
|
|
721
841
|
/**
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
842
|
+
* Re-arm a pooled job that couldn't run because its pool was at capacity.
|
|
843
|
+
* No attempt is charged (this is backpressure, not a failure): the job is
|
|
844
|
+
* pushed `POOL_BACKPRESSURE_DELAY_MS` into the future so a later alarm
|
|
845
|
+
* drains it once a slot frees, keeping its `id:` header and retry policy.
|
|
846
|
+
*/
|
|
727
847
|
private requeuePooled;
|
|
728
848
|
/**
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
849
|
+
* Release a pool slot when the runtime reports an action finished. This is
|
|
850
|
+
* the durable-semaphore decrement: dispatch() only KICKS the action and
|
|
851
|
+
* holds the slot; the runtime calls back here (`POST /complete { id }`) once
|
|
852
|
+
* the action settles, freeing the slot for the next queued job. Idempotent
|
|
853
|
+
* and safe if the job/pool is already gone.
|
|
854
|
+
*/
|
|
735
855
|
private handleComplete;
|
|
736
856
|
/** `GET /pool?name=` — inspect a pool's slot usage + queued count. */
|
|
737
857
|
private handlePoolStatus;
|
|
738
858
|
/**
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
859
|
+
* `GET /status` — the app-level backlog signal that powers the studio's
|
|
860
|
+
* SLO view. Enumerates every durable `pool:<name>` row for its `inFlight`/
|
|
861
|
+
* `maxConcurrency` semaphore, counts the pending (not-yet-dispatched) jobs
|
|
862
|
+
* routed to each pool with the same single-pass scan {@link handlePoolStatus}
|
|
863
|
+
* uses, and rolls those up into app-wide `backlog` (sum of `queued`) and
|
|
864
|
+
* `inFlight` (sum of held slots) totals.
|
|
865
|
+
*
|
|
866
|
+
* Pools that have rows but no queued jobs still appear (with `queued: 0`) so
|
|
867
|
+
* a saturated-but-idle pool stays visible; a pool that only ever existed as
|
|
868
|
+
* queued jobs without a persisted row is unreachable here (the schedule path
|
|
869
|
+
* always writes a `pool:<name>` row before the job's header), so a single
|
|
870
|
+
* scan over `pool:` plus a cursor loop over `id:` is sufficient.
|
|
871
|
+
*/
|
|
752
872
|
private handleStatus;
|
|
753
873
|
private handleSchedule;
|
|
754
874
|
private handleCancel;
|
|
755
875
|
private handleList;
|
|
756
876
|
/**
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
877
|
+
* `GET /dead` — list the dead-letter records: jobs that exhausted their
|
|
878
|
+
* retry budget ({@link recordRetry}) and were parked under `dead:<id>`
|
|
879
|
+
* instead of being silently dropped. These never appear in `/list` (their
|
|
880
|
+
* `id:` header is deleted on park), so this is the ONLY way the studio can
|
|
881
|
+
* surface — and recover — a permanently-failed job.
|
|
882
|
+
*/
|
|
763
883
|
private handleDeadList;
|
|
764
884
|
/**
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
885
|
+
* `POST /dead/retry { id }` — resurrect a dead-letter record: reset its
|
|
886
|
+
* exhausted attempt count to 0 (a fresh retry budget), re-arm it for
|
|
887
|
+
* immediate dispatch via the standard time index, and drop the `dead:` row.
|
|
888
|
+
* The new `id:` header makes it visible to `/list` and the live `/ws`
|
|
889
|
+
* subscription again. A miss is a no-op (`{ retried: false }`).
|
|
890
|
+
*/
|
|
771
891
|
private handleDeadRetry;
|
|
772
892
|
/**
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
893
|
+
* `POST /dead/cancel { id }` — permanently drop a dead-letter record the
|
|
894
|
+
* operator has decided not to recover. Returns `{ removed }` (false when
|
|
895
|
+
* nothing matched). Idempotent: a repeated purge is a harmless no-op.
|
|
896
|
+
*/
|
|
777
897
|
private handleDeadCancel;
|
|
778
898
|
/**
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
899
|
+
* Resolve a single pending job by id via a direct `id:<id>` storage read —
|
|
900
|
+
* O(1), versus scanning the whole `/list` view. Responds `{ record }` on a
|
|
901
|
+
* hit and `{}` on a miss (an absent `record` field — JSON has no `undefined`
|
|
902
|
+
* — which the client reads back as `null`).
|
|
903
|
+
*/
|
|
784
904
|
private handleGet;
|
|
785
905
|
private removeRecord;
|
|
786
906
|
/**
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
907
|
+
* Arm the alarm for `scheduledFor` only if it is sooner than the currently
|
|
908
|
+
* set alarm (or none is set). Used on the schedule path: inserting a job
|
|
909
|
+
* can only ever pull the earliest-pending time *earlier*, never later, so a
|
|
910
|
+
* full `t:` rescan is unnecessary unless the new job is the new earliest.
|
|
911
|
+
*/
|
|
792
912
|
private armAlarmIfEarlier;
|
|
793
913
|
private rescheduleAlarm;
|
|
794
914
|
}
|
|
915
|
+
/** What the Cloudflare scheduler host needs from the Worker's environment. */
|
|
916
|
+
interface SchedulerHostOptions {
|
|
917
|
+
/**
|
|
918
|
+
* Named scheduler instance — one `SchedulerDO` per name, useful for tenant
|
|
919
|
+
* isolation. Defaults to `"default"`.
|
|
920
|
+
*/
|
|
921
|
+
instanceName?: string;
|
|
922
|
+
/**
|
|
923
|
+
* Data-residency jurisdiction for the `SchedulerDO`. Pass the same value as
|
|
924
|
+
* the worker's own jurisdiction so scheduled state co-resides with app data.
|
|
925
|
+
*/
|
|
926
|
+
jurisdiction?: "eu" | "fedramp" | "us";
|
|
927
|
+
/** The `SchedulerDO` namespace binding. */
|
|
928
|
+
namespace: Parameters<typeof createScheduler>[0]["namespace"];
|
|
929
|
+
/**
|
|
930
|
+
* Public origin the Worker is mounted at. `SchedulerDO` dispatches back to
|
|
931
|
+
* this base URL when an alarm fires, so a wrong value means jobs fire into
|
|
932
|
+
* nothing.
|
|
933
|
+
*/
|
|
934
|
+
originUrl: string;
|
|
935
|
+
}
|
|
936
|
+
/**
|
|
937
|
+
* Build the Cloudflare {@link SchedulerHost}.
|
|
938
|
+
*
|
|
939
|
+
* The returned host has no `cron` member — see the module docstring.
|
|
940
|
+
*/
|
|
941
|
+
declare const createSchedulerHost: (options: SchedulerHostOptions) => SchedulerHost;
|
|
795
942
|
/** Standard cron expression (5- or 6-field) or a supported `@macro`, per `cron-parser`. */
|
|
796
943
|
declare const isValidCronExpression: (schedule: string) => boolean;
|
|
797
944
|
/**
|
|
798
|
-
*
|
|
799
|
-
*
|
|
800
|
-
*
|
|
801
|
-
|
|
945
|
+
* A 6-field (seconds-leading) cron expression is legal generic cron grammar
|
|
946
|
+
* but not a Cloudflare Cron Trigger — the platform only understands the
|
|
947
|
+
* 5-field, minute-granularity form and rejects the rest at `wrangler deploy`
|
|
948
|
+
* with a message naming neither the job nor the file. Warn here instead of
|
|
949
|
+
* staying silent, without throwing: unlike the ergonomic `.interval()` form,
|
|
950
|
+
* the raw `.cron()` escape hatch is meant to accept cron grammar this module
|
|
951
|
+
* doesn't otherwise second-guess.
|
|
952
|
+
*
|
|
953
|
+
* Exported (not module-private) because it is the ONE place this advisory is
|
|
954
|
+
* written: the runtime `cronJobs()` builder reaches it via
|
|
955
|
+
* {@link assertValidCronExpression}, and `@lunora/codegen`'s static
|
|
956
|
+
* `discover-crons.ts` calls it directly after its own `isValidCronExpression`
|
|
957
|
+
* check — so a hand-authored 6-field `.cron()` warns whether it's discovered
|
|
958
|
+
* from source at build time or registered at runtime, with one shared message.
|
|
959
|
+
*/
|
|
960
|
+
declare const warnIfSecondsLeading: (schedule: string, context: string) => void;
|
|
961
|
+
/**
|
|
962
|
+
* Assert a raw cron expression is well-formed, throwing the same shaped error
|
|
963
|
+
* both cron surfaces use. The `context` prefix lets callers name the offending
|
|
964
|
+
* job (`cron job "send digest"`) vs. the bare trigger. Well-formed but
|
|
965
|
+
* Cloudflare-incompatible (6-field) expressions pass but log a warning — see
|
|
966
|
+
* {@link warnIfSecondsLeading}.
|
|
967
|
+
*/
|
|
802
968
|
declare const assertValidCronExpression: (schedule: string, context?: string) => void;
|
|
803
|
-
export { type ArgsOf, CRON_SCHEDULE_KINDS, type CronJob, type CronJobsBuilder, type CronScheduleKind, type CronTarget, type CronTriggerOptions, type CronTriggerSnippet, type DailySchedule, type DurableObjectIdLike, type DurableObjectJurisdiction, type DurableObjectNamespaceLike, type DurableObjectStubLike, type EnqueueOptions, type FunctionReference, type HttpDispatcherOptions, type IntervalSchedule, type LunoraSchedulerOptions, type MessageBatchLike, type MonthlySchedule, type QueueConsumerOptions, type QueueDispatch, type QueueEnqueueOptions, type QueueJob, type QueueLike, type QueueMessageLike, type QueueSendOptionsLike, type QueueSendRequestLike, type QueueWorkpool, type QueueWorkpoolOptions, type RetryPolicy, type RunOptions, type ScheduleRecord, type Scheduler, SchedulerDO, type SchedulerDOState, type SchedulerEnv, type SchedulerPoolStatus, type SchedulerStatus, type WeeklySchedule, type WorkflowReference, type Workpool, type WorkpoolOptions, assertValidCronExpression, compileCronSchedule, createCronTrigger, createQueueConsumer, createQueueWorkpool, createScheduler, createWorkpool, cronJobs, httpDispatcher, isValidCronExpression, isWorkflowReference };
|
|
969
|
+
export { type ArgsOf, CRON_SCHEDULE_KINDS, type CronJob, type CronJobsBuilder, type CronScheduleKind, type CronTarget, type CronTriggerOptions, type CronTriggerSnippet, type DailySchedule, type DurableObjectIdLike, type DurableObjectJurisdiction, type DurableObjectNamespaceLike, type DurableObjectStubLike, type EnqueueOptions, type FunctionReference, type HourlySchedule, type HttpDispatcherOptions, type IntervalSchedule, type LunoraSchedulerOptions, type MessageBatchLike, type MonthlySchedule, type QueueConsumerOptions, type QueueDispatch, type QueueEnqueueOptions, type QueueJob, type QueueLike, type QueueMessageLike, type QueueSendOptionsLike, type QueueSendRequestLike, type QueueWorkpool, type QueueWorkpoolOptions, type RetryPolicy, type RunOptions, type ScheduleRecord, type Scheduler, SchedulerDO, type SchedulerDOState, type SchedulerEnv, type SchedulerHostOptions, type SchedulerPoolStatus, type SchedulerStatus, type WeeklySchedule, type WorkflowReference, type Workpool, type WorkpoolOptions, assertValidCronExpression, compileCronSchedule, createCronTrigger, createQueueConsumer, createQueueWorkpool, createScheduler, createSchedulerHost, createWorkpool, cronJobs, httpDispatcher, isValidCronExpression, isWorkflowReference, warnIfSecondsLeading };
|