@lunora/scheduler 0.0.0 → 1.0.0-alpha.2
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 +105 -0
- package/README.md +140 -9
- package/__assets__/package-og.svg +14 -0
- package/dist/index.d.mts +803 -0
- package/dist/index.d.ts +803 -0
- package/dist/index.mjs +8 -0
- package/dist/packem_shared/CRON_SCHEDULE_KINDS-BaLlXJiN.mjs +123 -0
- package/dist/packem_shared/SchedulerDO-BNzXNnS4.mjs +678 -0
- package/dist/packem_shared/assertValidCronExpression-BLfrDgmK.mjs +20 -0
- package/dist/packem_shared/createCronTrigger-Cq9IBcWQ.mjs +27 -0
- package/dist/packem_shared/createQueueConsumer-DWahNPfz.mjs +59 -0
- package/dist/packem_shared/createScheduler-CWMn70nv.mjs +69 -0
- package/dist/packem_shared/createWorkpool-CEnqCafM.mjs +62 -0
- package/dist/packem_shared/isWorkflowReference-C9mQkMXt.mjs +3 -0
- package/dist/packem_shared/jurisdiction-CR2zC3Et.mjs +13 -0
- package/package.json +40 -17
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,803 @@
|
|
|
1
|
+
/**
|
|
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
|
+
*/
|
|
10
|
+
interface FunctionReference {
|
|
11
|
+
readonly __lunoraRef: string;
|
|
12
|
+
/** Marker phantom type — discriminates queries / mutations / actions. */
|
|
13
|
+
readonly _kind?: "query" | "mutation" | "action";
|
|
14
|
+
}
|
|
15
|
+
type ArgsOf<F extends FunctionReference> = F extends {
|
|
16
|
+
_args?: infer A;
|
|
17
|
+
} ? A : Record<string, unknown>;
|
|
18
|
+
/**
|
|
19
|
+
* Typed reference to a Lunora durable workflow — either the generated
|
|
20
|
+
* `workflows.<name>` reference object (`_generated/api.ts`, which carries the
|
|
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
|
+
*/
|
|
35
|
+
interface WorkflowReference<Params = Record<string, unknown>> {
|
|
36
|
+
/** Phantom carrier for the workflow's `params` type — drives `cronJobs()` arg inference. Never read at runtime. */
|
|
37
|
+
readonly __params?: Params;
|
|
38
|
+
/** The `WORKFLOW_*` binding name (present on a generated `workflows.<name>` ref). */
|
|
39
|
+
readonly binding?: string;
|
|
40
|
+
readonly isLunoraWorkflow: true;
|
|
41
|
+
/** The workflow's export/stable name (present on a generated ref; a `defineWorkflow({ name })` override otherwise). */
|
|
42
|
+
readonly name?: string;
|
|
43
|
+
}
|
|
44
|
+
/** A cron job's target: either a one-shot function dispatch or a durable workflow start. */
|
|
45
|
+
type CronTarget = FunctionReference | WorkflowReference;
|
|
46
|
+
/** The arguments a cron's target accepts: a workflow's inferred `params`, else an open record (function args aren't inferred). */
|
|
47
|
+
type CronTargetArgs<T extends CronTarget> = T extends WorkflowReference<infer Params> ? Params : Record<string, unknown>;
|
|
48
|
+
/** Narrow a {@link CronTarget} to a {@link WorkflowReference} by its runtime brand. */
|
|
49
|
+
declare const isWorkflowReference: (target: unknown) => target is WorkflowReference;
|
|
50
|
+
/**
|
|
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
|
+
*/
|
|
59
|
+
interface RetryPolicy {
|
|
60
|
+
/**
|
|
61
|
+
* Backoff growth across attempts. `"exponential"` doubles the delay each
|
|
62
|
+
* attempt (`baseMs * 2 ** (attempt - 1)`); `"linear"` grows it linearly
|
|
63
|
+
* (`baseMs * attempt`). Default `"exponential"`.
|
|
64
|
+
*/
|
|
65
|
+
backoff?: "exponential" | "linear";
|
|
66
|
+
/** Base delay in milliseconds for the first retry. Default `30_000`. */
|
|
67
|
+
baseMs?: number;
|
|
68
|
+
/** Maximum number of dispatch attempts before dead-lettering. Default `5`. */
|
|
69
|
+
maxAttempts?: number;
|
|
70
|
+
/** Optional ceiling clamping the computed backoff delay. */
|
|
71
|
+
maxMs?: number;
|
|
72
|
+
}
|
|
73
|
+
interface RunOptions {
|
|
74
|
+
/**
|
|
75
|
+
* Logical workpool this job belongs to. When set, the SchedulerDO gates the
|
|
76
|
+
* job behind the pool's `maxConcurrency` (see {@link WorkpoolOptions}).
|
|
77
|
+
* Usually populated by {@link Workpool.enqueue}; callers rarely set it on a
|
|
78
|
+
* bare `runAfter`/`runAt`.
|
|
79
|
+
*/
|
|
80
|
+
pool?: string;
|
|
81
|
+
/** Per-job retry policy. Falls back to the DO's built-in defaults when omitted. */
|
|
82
|
+
retry?: RetryPolicy;
|
|
83
|
+
/** Routing hint — forwarded to the Worker so the call lands on the right shard. */
|
|
84
|
+
shardKey?: string;
|
|
85
|
+
}
|
|
86
|
+
interface ScheduleRecord {
|
|
87
|
+
args: Record<string, unknown>;
|
|
88
|
+
/**
|
|
89
|
+
* Number of dispatch attempts already made. Absent (treated as 0) until the
|
|
90
|
+
* first failure, after which `recordRetry()` persists it on both the
|
|
91
|
+
* `retry:` row and the `id:` header. Surfaced here so `/list` consumers and
|
|
92
|
+
* the studio see the field the storage layer actually writes.
|
|
93
|
+
*/
|
|
94
|
+
attempts?: number;
|
|
95
|
+
enqueuedAt: number;
|
|
96
|
+
functionPath: string;
|
|
97
|
+
id: string;
|
|
98
|
+
/**
|
|
99
|
+
* Scheduler/workpool instance name the job was enqueued through. Echoed in
|
|
100
|
+
* the dispatch payload so the runtime can call back the SAME DO instance's
|
|
101
|
+
* `/complete` to release a pooled slot. Absent for the default instance.
|
|
102
|
+
*/
|
|
103
|
+
instanceName?: string;
|
|
104
|
+
/**
|
|
105
|
+
* Logical workpool this job belongs to (set by {@link Workpool.enqueue}).
|
|
106
|
+
* When present, the SchedulerDO only dispatches the job while the pool's
|
|
107
|
+
* in-flight count is below its `maxConcurrency`; otherwise it stays queued
|
|
108
|
+
* and drains as slots free. Absent for plain `runAfter`/`runAt` jobs, which
|
|
109
|
+
* are never concurrency-gated.
|
|
110
|
+
*/
|
|
111
|
+
pool?: string;
|
|
112
|
+
/** Per-job retry policy (see {@link RetryPolicy}); absent means DO defaults. */
|
|
113
|
+
retry?: RetryPolicy;
|
|
114
|
+
scheduledFor: number;
|
|
115
|
+
shardKey?: string;
|
|
116
|
+
}
|
|
117
|
+
interface Scheduler {
|
|
118
|
+
cancel: (id: string) => Promise<{
|
|
119
|
+
cancelled: boolean;
|
|
120
|
+
}>;
|
|
121
|
+
/** Resolve a single pending job by id, or `null` when absent (derived from {@link Scheduler.list}). */
|
|
122
|
+
get: (id: string) => Promise<ScheduleRecord | null>;
|
|
123
|
+
/** All pending scheduled jobs (the DO's `/list` view). */
|
|
124
|
+
list: () => Promise<ScheduleRecord[]>;
|
|
125
|
+
runAfter: <F extends FunctionReference>(delayMs: number, function_: F, args: ArgsOf<F>, options?: RunOptions) => Promise<{
|
|
126
|
+
id: string;
|
|
127
|
+
scheduledFor: number;
|
|
128
|
+
}>;
|
|
129
|
+
runAt: <F extends FunctionReference>(date: Date | number, function_: F, args: ArgsOf<F>, options?: RunOptions) => Promise<{
|
|
130
|
+
id: string;
|
|
131
|
+
scheduledFor: number;
|
|
132
|
+
}>;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
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
|
+
*/
|
|
139
|
+
type DurableObjectJurisdiction = "eu" | "fedramp" | "us";
|
|
140
|
+
/** Subset of `DurableObjectNamespace` the package consumes. */
|
|
141
|
+
interface DurableObjectNamespaceLike {
|
|
142
|
+
get: (id: DurableObjectIdLike) => DurableObjectStubLike;
|
|
143
|
+
idFromName: (name: string) => DurableObjectIdLike;
|
|
144
|
+
/**
|
|
145
|
+
* Derive a jurisdiction-restricted subnamespace. Optional because older
|
|
146
|
+
* workers-types releases (and test doubles) may not expose it.
|
|
147
|
+
*/
|
|
148
|
+
jurisdiction?: (jurisdiction: DurableObjectJurisdiction) => DurableObjectNamespaceLike;
|
|
149
|
+
}
|
|
150
|
+
interface DurableObjectIdLike {
|
|
151
|
+
toString: () => string;
|
|
152
|
+
}
|
|
153
|
+
interface DurableObjectStubLike {
|
|
154
|
+
fetch: (input: Request | string, init?: RequestInit) => Promise<Response>;
|
|
155
|
+
}
|
|
156
|
+
interface LunoraSchedulerOptions {
|
|
157
|
+
/** Optional named instance — useful for tenant isolation. Default `default`. */
|
|
158
|
+
instanceName?: string;
|
|
159
|
+
/**
|
|
160
|
+
* Pin the SchedulerDO (durable timers + cron state) to a Cloudflare
|
|
161
|
+
* data-residency jurisdiction. Pass the same value as the worker's
|
|
162
|
+
* `jurisdiction` so scheduled state co-resides with app data. Omit for the
|
|
163
|
+
* un-pinned global namespace.
|
|
164
|
+
*/
|
|
165
|
+
jurisdiction?: DurableObjectJurisdiction;
|
|
166
|
+
/** Binding to the `SchedulerDO` durable object namespace. */
|
|
167
|
+
namespace: DurableObjectNamespaceLike;
|
|
168
|
+
/**
|
|
169
|
+
* Origin where the Worker is mounted. SchedulerDO uses this base URL when
|
|
170
|
+
* dispatching scheduled functions back to the Worker on alarm fire.
|
|
171
|
+
*/
|
|
172
|
+
originUrl: string;
|
|
173
|
+
}
|
|
174
|
+
/** Per-enqueue options for a {@link Workpool}. Extends {@link RunOptions} minus the implicit `pool` (the pool sets that). */
|
|
175
|
+
interface EnqueueOptions {
|
|
176
|
+
/** Run the job no sooner than this delay (ms) from now. Default `0` (next drain). */
|
|
177
|
+
delayMs?: number;
|
|
178
|
+
/** Per-job retry policy. Falls back to the DO's built-in defaults when omitted. */
|
|
179
|
+
retry?: RetryPolicy;
|
|
180
|
+
/** Routing hint — forwarded to the Worker so the call lands on the right shard. */
|
|
181
|
+
shardKey?: string;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
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
|
+
*/
|
|
190
|
+
interface WorkpoolOptions extends LunoraSchedulerOptions {
|
|
191
|
+
/**
|
|
192
|
+
* Maximum number of jobs from this pool that may be in flight at once.
|
|
193
|
+
* Excess enqueues are persisted and drain as slots free. Must be a positive
|
|
194
|
+
* integer.
|
|
195
|
+
*/
|
|
196
|
+
maxConcurrency: number;
|
|
197
|
+
/**
|
|
198
|
+
* Pool name — the concurrency counter is keyed by this inside the
|
|
199
|
+
* SchedulerDO storage (`pool:<name>`). Default `default`.
|
|
200
|
+
*/
|
|
201
|
+
name?: string;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
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
|
+
*/
|
|
209
|
+
interface Workpool {
|
|
210
|
+
/** Cancel a queued/in-flight pool job by id. */
|
|
211
|
+
cancel: (id: string) => Promise<{
|
|
212
|
+
cancelled: boolean;
|
|
213
|
+
}>;
|
|
214
|
+
/**
|
|
215
|
+
* Enqueue `function_(args)` into the pool. Resolves with the durable job id
|
|
216
|
+
* and the time it was scheduled for (it may not run immediately if the pool
|
|
217
|
+
* is at capacity).
|
|
218
|
+
*/
|
|
219
|
+
enqueue: <F extends FunctionReference>(function_: F, args: ArgsOf<F>, options?: EnqueueOptions) => Promise<{
|
|
220
|
+
id: string;
|
|
221
|
+
scheduledFor: number;
|
|
222
|
+
}>;
|
|
223
|
+
/** The pool's name (the `pool:<name>` storage key suffix). */
|
|
224
|
+
readonly name: string;
|
|
225
|
+
/** Inspect the pool's current state — `inFlight` slots used and the configured `maxConcurrency`. */
|
|
226
|
+
status: () => Promise<{
|
|
227
|
+
inFlight: number;
|
|
228
|
+
maxConcurrency: number;
|
|
229
|
+
queued: number;
|
|
230
|
+
}>;
|
|
231
|
+
}
|
|
232
|
+
/** Per-message options for {@link QueueLike.send} — the subset Lunora uses. */
|
|
233
|
+
interface QueueSendOptionsLike {
|
|
234
|
+
/** Delay delivery to the consumer by this many seconds. */
|
|
235
|
+
delaySeconds?: number;
|
|
236
|
+
}
|
|
237
|
+
/** One message in a `sendBatch` call. */
|
|
238
|
+
interface QueueSendRequestLike<Body = unknown> {
|
|
239
|
+
body: Body;
|
|
240
|
+
delaySeconds?: number;
|
|
241
|
+
}
|
|
242
|
+
/** Producer side of a Cloudflare Queue binding (the `env` queue binding). */
|
|
243
|
+
interface QueueLike<Body = unknown> {
|
|
244
|
+
send: (body: Body, options?: QueueSendOptionsLike) => Promise<void>;
|
|
245
|
+
sendBatch: (messages: Iterable<QueueSendRequestLike<Body>>, options?: QueueSendOptionsLike) => Promise<void>;
|
|
246
|
+
}
|
|
247
|
+
/** One delivered Cloudflare Queue message (consumer side). */
|
|
248
|
+
interface QueueMessageLike<Body = unknown> {
|
|
249
|
+
/** Marks the message delivered (won't be retried). */
|
|
250
|
+
ack: () => void;
|
|
251
|
+
/** 1-based count of delivery attempts so far. */
|
|
252
|
+
readonly attempts: number;
|
|
253
|
+
readonly body: Body;
|
|
254
|
+
readonly id: string;
|
|
255
|
+
/** Marks the message for retry on a later batch (→ dead-letter after `max_retries`). */
|
|
256
|
+
retry: (options?: {
|
|
257
|
+
delaySeconds?: number;
|
|
258
|
+
}) => void;
|
|
259
|
+
readonly timestamp: Date;
|
|
260
|
+
}
|
|
261
|
+
/** A batch of messages handed to a `queue()` consumer handler. */
|
|
262
|
+
interface MessageBatchLike<Body = unknown> {
|
|
263
|
+
/** Mark every message delivered. */
|
|
264
|
+
ackAll: () => void;
|
|
265
|
+
readonly messages: ReadonlyArray<QueueMessageLike<Body>>;
|
|
266
|
+
readonly queue: string;
|
|
267
|
+
/** Mark every message for retry. */
|
|
268
|
+
retryAll: (options?: {
|
|
269
|
+
delaySeconds?: number;
|
|
270
|
+
}) => void;
|
|
271
|
+
}
|
|
272
|
+
/** The wire payload Lunora puts on the queue: a function dispatch. */
|
|
273
|
+
interface QueueJob {
|
|
274
|
+
args?: Record<string, unknown>;
|
|
275
|
+
functionPath: string;
|
|
276
|
+
/** Routing hint forwarded to the Worker so the call lands on the right shard. */
|
|
277
|
+
shardKey?: string;
|
|
278
|
+
}
|
|
279
|
+
/** Per-enqueue options for a {@link QueueWorkpool}. */
|
|
280
|
+
interface QueueEnqueueOptions {
|
|
281
|
+
/** Delay delivery by this many seconds (Queues-native). Default: immediate. */
|
|
282
|
+
delaySeconds?: number;
|
|
283
|
+
/** Routing hint — which shard the job should run against. */
|
|
284
|
+
shardKey?: string;
|
|
285
|
+
}
|
|
286
|
+
/** Options for `createQueueWorkpool`. */
|
|
287
|
+
interface QueueWorkpoolOptions {
|
|
288
|
+
/** The Cloudflare Queue producer binding to enqueue onto. */
|
|
289
|
+
queue: QueueLike<QueueJob>;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
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
|
+
*/
|
|
297
|
+
interface QueueWorkpool {
|
|
298
|
+
/** Enqueue a single `fn(args)` dispatch. */
|
|
299
|
+
enqueue: <F extends FunctionReference>(function_: F, args: ArgsOf<F>, options?: QueueEnqueueOptions) => Promise<void>;
|
|
300
|
+
/** Enqueue many dispatches in one `sendBatch`. Each job names its function `ref`. */
|
|
301
|
+
enqueueBatch: (jobs: ReadonlyArray<{
|
|
302
|
+
args?: Record<string, unknown>;
|
|
303
|
+
ref: FunctionReference;
|
|
304
|
+
shardKey?: string;
|
|
305
|
+
}>, options?: QueueSendOptionsLike) => Promise<void>;
|
|
306
|
+
}
|
|
307
|
+
/** Dispatches a single {@link QueueJob} — the consumer's per-message worker. */
|
|
308
|
+
type QueueDispatch = (job: QueueJob) => Promise<void>;
|
|
309
|
+
/** Options for `createQueueConsumer`. */
|
|
310
|
+
interface QueueConsumerOptions {
|
|
311
|
+
/** How each job is executed; e.g. the `httpDispatcher`. */
|
|
312
|
+
dispatch: QueueDispatch;
|
|
313
|
+
}
|
|
314
|
+
/** Options for the `httpDispatcher` — the default HTTP dispatcher. */
|
|
315
|
+
interface HttpDispatcherOptions {
|
|
316
|
+
/** Admin bearer token the dispatch endpoint accepts (`LUNORA_ADMIN_TOKEN`). */
|
|
317
|
+
adminToken: string;
|
|
318
|
+
/** Injectable fetch (tests); defaults to the global. */
|
|
319
|
+
fetchImpl?: typeof fetch;
|
|
320
|
+
/** Origin where the Worker is mounted (the `/_lunora/scheduler/dispatch` endpoint). */
|
|
321
|
+
originUrl: string;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
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
|
+
*/
|
|
328
|
+
declare const createScheduler: (options: LunoraSchedulerOptions) => Scheduler;
|
|
329
|
+
/**
|
|
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:<name>` storage key).
|
|
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
|
+
*/
|
|
359
|
+
declare const createWorkpool: (options: WorkpoolOptions) => Workpool;
|
|
360
|
+
interface CronTriggerOptions {
|
|
361
|
+
/** Args passed to the function. */
|
|
362
|
+
args?: Record<string, unknown>;
|
|
363
|
+
/** The Lunora function to invoke on each trigger fire. */
|
|
364
|
+
fn: FunctionReference;
|
|
365
|
+
/** Standard cron expression, e.g. `"0 * * * *"`. */
|
|
366
|
+
schedule: string;
|
|
367
|
+
}
|
|
368
|
+
interface CronTriggerSnippet {
|
|
369
|
+
/** Paste these into `wrangler.jsonc` under `triggers.crons`. */
|
|
370
|
+
crons: string[];
|
|
371
|
+
/** Routes the Worker should mount to receive the trigger dispatch. */
|
|
372
|
+
dispatcher: {
|
|
373
|
+
args: Record<string, unknown>;
|
|
374
|
+
functionPath: string;
|
|
375
|
+
};
|
|
376
|
+
/** Human-readable wrangler.jsonc snippet developers can copy/paste. */
|
|
377
|
+
wranglerJsonc: string;
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
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
|
+
*/
|
|
384
|
+
declare const createCronTrigger: (options: CronTriggerOptions) => CronTriggerSnippet;
|
|
385
|
+
/** Sub-day recurrence. Exactly one unit must be provided. */
|
|
386
|
+
interface IntervalSchedule {
|
|
387
|
+
hours?: number;
|
|
388
|
+
minutes?: number;
|
|
389
|
+
seconds?: number;
|
|
390
|
+
}
|
|
391
|
+
/** Daily recurrence at a fixed UTC wall-clock time. */
|
|
392
|
+
interface DailySchedule {
|
|
393
|
+
/** 0–23. */
|
|
394
|
+
hourUTC: number;
|
|
395
|
+
/** 0–59. */
|
|
396
|
+
minuteUTC: number;
|
|
397
|
+
}
|
|
398
|
+
/** Weekly recurrence at a fixed UTC time on a given weekday. */
|
|
399
|
+
interface WeeklySchedule extends DailySchedule {
|
|
400
|
+
/** Long weekday name, case-insensitive (e.g. `"monday"`). */
|
|
401
|
+
dayOfWeek: "friday" | "monday" | "saturday" | "sunday" | "thursday" | "tuesday" | "wednesday";
|
|
402
|
+
}
|
|
403
|
+
/** Monthly recurrence at a fixed UTC time on a given day-of-month. */
|
|
404
|
+
interface MonthlySchedule extends DailySchedule {
|
|
405
|
+
/** 1–31. */
|
|
406
|
+
day: number;
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
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
|
+
*/
|
|
413
|
+
interface CronJob {
|
|
414
|
+
/** Args forwarded to the function (or, for a workflow target, used as its `params`) on each fire. */
|
|
415
|
+
args: Record<string, unknown>;
|
|
416
|
+
/** Compiled standard cron expression, e.g. `"0 9 * * *"`. */
|
|
417
|
+
cron: string;
|
|
418
|
+
/**
|
|
419
|
+
* `__lunoraRef` of the target function. Present for a function target;
|
|
420
|
+
* absent when the job targets a workflow ({@link CronJob.workflow} instead).
|
|
421
|
+
*/
|
|
422
|
+
functionPath?: string;
|
|
423
|
+
/** Human-readable identifier — must be unique within one `cronJobs()`. */
|
|
424
|
+
name: string;
|
|
425
|
+
/**
|
|
426
|
+
* Set when the job targets a durable workflow rather than a function: the
|
|
427
|
+
* workflow's stable name (`defineWorkflow({ name })`) when one was declared,
|
|
428
|
+
* otherwise `""`. `@lunora/codegen` statically resolves the concrete
|
|
429
|
+
* `lunora/workflows.ts` export + its `WORKFLOW_*` binding for the emitted
|
|
430
|
+
* dispatch map, so this authoring-time value is informational only.
|
|
431
|
+
*/
|
|
432
|
+
workflow?: string;
|
|
433
|
+
}
|
|
434
|
+
/** The ergonomic builder methods, excluding the raw `.cron` escape hatch. */
|
|
435
|
+
type CronScheduleKind = "daily" | "interval" | "monthly" | "weekly";
|
|
436
|
+
/** The ergonomic schedule kinds as a runtime set (codegen reads this to detect cron builder methods). */
|
|
437
|
+
declare const CRON_SCHEDULE_KINDS: ReadonlySet<CronScheduleKind>;
|
|
438
|
+
/**
|
|
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
|
+
declare const compileCronSchedule: (kind: CronScheduleKind, schedule: DailySchedule | IntervalSchedule | MonthlySchedule | WeeklySchedule) => string;
|
|
445
|
+
/**
|
|
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
|
+
*/
|
|
450
|
+
interface CronJobsBuilder {
|
|
451
|
+
/**
|
|
452
|
+
* Raw cron expression escape hatch (5- or 6-field, full cron-parser grammar).
|
|
453
|
+
* The target may be a function (`internal.file.fn`) or a durable workflow
|
|
454
|
+
* (`workflows.<name>`); a workflow's `args` are inferred from its `params`.
|
|
455
|
+
*/
|
|
456
|
+
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.<name>`). */
|
|
458
|
+
daily: <T extends CronTarget>(name: string, schedule: DailySchedule, target: T, args?: CronTargetArgs<T>) => CronJobsBuilder;
|
|
459
|
+
/** Every `{ seconds | minutes | hours }`. The target may be a function or a durable workflow (`workflows.<name>`). */
|
|
460
|
+
interval: <T extends CronTarget>(name: string, schedule: IntervalSchedule, target: T, args?: CronTargetArgs<T>) => CronJobsBuilder;
|
|
461
|
+
/** Snapshot of the registered jobs, in declaration order. */
|
|
462
|
+
jobs: () => ReadonlyArray<CronJob>;
|
|
463
|
+
/** Monthly on `day` at `hourUTC:minuteUTC` (UTC). The target may be a function or a durable workflow (`workflows.<name>`). */
|
|
464
|
+
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.<name>`). */
|
|
466
|
+
weekly: <T extends CronTarget>(name: string, schedule: WeeklySchedule, target: T, args?: CronTargetArgs<T>) => CronJobsBuilder;
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
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
|
+
*/
|
|
473
|
+
declare const cronJobs: () => CronJobsBuilder;
|
|
474
|
+
/**
|
|
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
|
+
*/
|
|
478
|
+
declare const createQueueWorkpool: (options: QueueWorkpoolOptions) => QueueWorkpool;
|
|
479
|
+
/**
|
|
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
|
+
*/
|
|
489
|
+
declare const createQueueConsumer: (options: QueueConsumerOptions) => ((batch: MessageBatchLike) => Promise<void>);
|
|
490
|
+
/**
|
|
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
|
+
*/
|
|
496
|
+
declare const httpDispatcher: (options: HttpDispatcherOptions) => QueueDispatch;
|
|
497
|
+
/**
|
|
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
|
+
*/
|
|
504
|
+
interface SchedulerDOState {
|
|
505
|
+
/** Accept a hibernatable server WebSocket (workers `state.acceptWebSocket`). */
|
|
506
|
+
acceptWebSocket?: (ws: WebSocket) => void;
|
|
507
|
+
/** Every accepted server WebSocket (workers `state.getWebSockets`). */
|
|
508
|
+
getWebSockets?: () => WebSocket[];
|
|
509
|
+
storage: {
|
|
510
|
+
delete: (key: string | string[]) => Promise<number | boolean>;
|
|
511
|
+
deleteAlarm: () => Promise<void> | void;
|
|
512
|
+
get: <T = unknown>(key: string) => Promise<T | undefined>;
|
|
513
|
+
getAlarm: () => Promise<number | null>;
|
|
514
|
+
list: <T = unknown>(options?: {
|
|
515
|
+
end?: string;
|
|
516
|
+
limit?: number;
|
|
517
|
+
prefix?: string;
|
|
518
|
+
}) => Promise<Map<string, T>>;
|
|
519
|
+
put: <T = unknown>(entries: Record<string, T> | string, value?: T) => Promise<void>;
|
|
520
|
+
setAlarm: (scheduledTime: number | Date) => Promise<void> | void;
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
interface SchedulerEnv {
|
|
524
|
+
[key: string]: unknown;
|
|
525
|
+
/**
|
|
526
|
+
* Fallback bearer token attached to the dispatch when
|
|
527
|
+
* {@link SchedulerEnv.LUNORA_SCHEDULER_SECRET} is not configured. Sent as
|
|
528
|
+
* `authorization: Bearer <token>`.
|
|
529
|
+
*/
|
|
530
|
+
LUNORA_ADMIN_TOKEN?: string;
|
|
531
|
+
/**
|
|
532
|
+
* Base URL where the Worker is mounted. SchedulerDO uses this at dispatch
|
|
533
|
+
* time to call back into the Worker. Read at fire time (NOT taken from the
|
|
534
|
+
* request body) to prevent SSRF via a forged `originUrl` field.
|
|
535
|
+
*/
|
|
536
|
+
LUNORA_ORIGIN_URL?: string;
|
|
537
|
+
/**
|
|
538
|
+
* Shared secret used to HMAC-sign the dispatch body so the runtime receiver
|
|
539
|
+
* can authenticate the call (header `x-lunora-scheduler-signature`). Without
|
|
540
|
+
* it the dispatch is sent unsigned (optionally bearer-authenticated via
|
|
541
|
+
* {@link SchedulerEnv.LUNORA_ADMIN_TOKEN}).
|
|
542
|
+
*/
|
|
543
|
+
LUNORA_SCHEDULER_SECRET?: string;
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
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
|
+
*/
|
|
550
|
+
interface SchedulerPoolStatus {
|
|
551
|
+
/** Jobs currently dispatched-but-not-yet-completed (the held slots). */
|
|
552
|
+
inFlight: number;
|
|
553
|
+
/** The pool's concurrency cap. */
|
|
554
|
+
maxConcurrency: number;
|
|
555
|
+
/** The logical workpool name (the `pool:<name>` suffix). */
|
|
556
|
+
name: string;
|
|
557
|
+
/** Pending jobs routed to this pool but not yet dispatched. */
|
|
558
|
+
queued: number;
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
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
|
+
*/
|
|
565
|
+
interface SchedulerStatus {
|
|
566
|
+
/** Sum of every pool's `queued` count — the total pending backlog. */
|
|
567
|
+
backlog: number;
|
|
568
|
+
/** Sum of every pool's `inFlight` count — the total held concurrency slots. */
|
|
569
|
+
inFlight: number;
|
|
570
|
+
/** Per-pool backlog breakdown, one entry per `pool:<name>` record. */
|
|
571
|
+
pools: SchedulerPoolStatus[];
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
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:<id>` maps to {@link ScheduleRecord}; `t:<paddedTime>:<id>` maps to the
|
|
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
|
+
*/
|
|
582
|
+
declare class SchedulerDO {
|
|
583
|
+
private static indexKey;
|
|
584
|
+
private static json;
|
|
585
|
+
private static error;
|
|
586
|
+
/**
|
|
587
|
+
* Resolve the effective retry parameters for a record: its per-job
|
|
588
|
+
* {@link RetryPolicy} merged over the DO's built-in defaults. Callers that
|
|
589
|
+
* never set `record.retry` get today's behaviour verbatim
|
|
590
|
+
* (`maxAttempts: 5`, exponential, `baseMs: 30_000`, no ceiling).
|
|
591
|
+
*/
|
|
592
|
+
private static resolveRetry;
|
|
593
|
+
/** Clamp an untrusted `maxConcurrency` to a positive integer, else fall back. */
|
|
594
|
+
private static normalizeConcurrency;
|
|
595
|
+
/**
|
|
596
|
+
* Sanitize an untrusted retry policy from the wire into a `RetryPolicy` (or
|
|
597
|
+
* `undefined` when nothing valid was provided). Keeps obviously-bad values
|
|
598
|
+
* out of storage so {@link SchedulerDO.resolveRetry} never has to re-guard.
|
|
599
|
+
* @returns The normalized policy, or `undefined` if no valid policy was found.
|
|
600
|
+
*/
|
|
601
|
+
private static normalizeRetry;
|
|
602
|
+
/**
|
|
603
|
+
* Idempotently release the slot held by `jobId`, returning the updated
|
|
604
|
+
* {@link PoolState} (pure — the caller persists it). A duplicate release for
|
|
605
|
+
* an id that no longer holds a slot is a no-op, so an at-least-once
|
|
606
|
+
* `/complete` (or a complete racing a failed-kick release) can never push
|
|
607
|
+
* `inFlight` below the true number of running jobs and oversubscribe the
|
|
608
|
+
* pool. Pools persisted before `inFlightIds` existed fall back to a clamped
|
|
609
|
+
* counter decrement.
|
|
610
|
+
*/
|
|
611
|
+
private static releaseSlot;
|
|
612
|
+
/**
|
|
613
|
+
* Best-effort release with no job id (legacy `/complete` payloads). Drops one
|
|
614
|
+
* tracked id if the set exists, else clamps the counter. Less precise than
|
|
615
|
+
* {@link SchedulerDO.releaseSlot} — a duplicate id-less complete CAN
|
|
616
|
+
* over-release — but every current client sends the id, so this is the
|
|
617
|
+
* compatibility shim, not the hot path.
|
|
618
|
+
*/
|
|
619
|
+
private static releaseFirstSlot;
|
|
620
|
+
protected readonly state: SchedulerDOState;
|
|
621
|
+
protected readonly env: SchedulerEnv;
|
|
622
|
+
constructor(state: SchedulerDOState, env: SchedulerEnv);
|
|
623
|
+
fetch(request: Request): Promise<Response>;
|
|
624
|
+
/** Called by the Workers runtime when the alarm previously set by `_rescheduleAlarm()` fires. */
|
|
625
|
+
alarm(): Promise<void>;
|
|
626
|
+
/**
|
|
627
|
+
* Internal dispatch hook; overridden in unit tests to capture the outgoing
|
|
628
|
+
* request. Returns `true` ONLY on an explicit 2xx response (`response.ok`).
|
|
629
|
+
* Anything else — a network failure, a 5xx, OR a non-2xx such as 404
|
|
630
|
+
* (receiver route not mounted) / 401 / 403 / 4xx — returns `false` and
|
|
631
|
+
* enters the retry pipeline via {@link recordRetry}. Treating 4xx as
|
|
632
|
+
* success used to permanently delete the job; since the receiver may simply
|
|
633
|
+
* be missing (404) or transiently failing, we retry rather than silently
|
|
634
|
+
* drop. After {@link MAX_RETRY_ATTEMPTS} the record is parked under a
|
|
635
|
+
* `dead:` key for inspection — never silently deleted.
|
|
636
|
+
*
|
|
637
|
+
* The dispatch target is taken from `env.LUNORA_ORIGIN_URL` (NOT from the
|
|
638
|
+
* stored record) to prevent SSRF via a forged `originUrl` on the schedule
|
|
639
|
+
* request. If that env var is missing at fire time (a deploy/binding
|
|
640
|
+
* regression — schedule time already enforced its presence) we return
|
|
641
|
+
* `false` so the record is retried rather than silently dropped.
|
|
642
|
+
*/
|
|
643
|
+
protected dispatch(record: ScheduleRecord): Promise<boolean>;
|
|
644
|
+
/**
|
|
645
|
+
* Claim + drain one due record with per-record fault isolation, so a storage
|
|
646
|
+
* throw can never abort the whole alarm pass (which would skip the remaining
|
|
647
|
+
* due records and the `rescheduleAlarm()` that re-arms the clock).
|
|
648
|
+
*
|
|
649
|
+
* Claims the job by deleting its time-index entry BEFORE dispatch (an alarm
|
|
650
|
+
* re-fire then won't pick it up again), runs {@link drainRecord}, and on a
|
|
651
|
+
* thrown storage op decides whether the job stays re-fireable.
|
|
652
|
+
*
|
|
653
|
+
* When the record was NOT successfully dispatched, re-assert the time-index
|
|
654
|
+
* claim so a later alarm re-attempts it (at-least-once): the claim delete may
|
|
655
|
+
* have removed it and recordRetry()/requeuePooled() may not have re-armed it
|
|
656
|
+
* before throwing, and re-inserting the same key is idempotent, so a
|
|
657
|
+
* surviving claim is simply rewritten to its prior value. When the record WAS
|
|
658
|
+
* dispatched (the throw came from post-dispatch cleanup), leave the index
|
|
659
|
+
* deleted so the already-kicked, idempotent job is not re-fired.
|
|
660
|
+
*/
|
|
661
|
+
private drainRecordGuarded;
|
|
662
|
+
/**
|
|
663
|
+
* Process one due (already index-claimed) record within an alarm drain:
|
|
664
|
+
* apply the workpool concurrency gate, dispatch, and settle the result.
|
|
665
|
+
* A saturated pool re-arms the job (backpressure, no attempt charged); a
|
|
666
|
+
* free slot is reserved durably before dispatch and released immediately if
|
|
667
|
+
* the kick fails (success holds it until the runtime reports completion).
|
|
668
|
+
* Success clears the `id:`/`retry:` rows; failure routes to
|
|
669
|
+
* {@link recordRetry}. `pools` caches each pool's {@link PoolState} for the
|
|
670
|
+
* lifetime of the drain so the budget decrements without re-reading storage.
|
|
671
|
+
* @returns `true` only when the record was successfully dispatched (a 2xx
|
|
672
|
+
* kick). The caller ({@link drainRecordGuarded}) uses this in its per-record
|
|
673
|
+
* error guard: a record that returns `true` (or whose post-dispatch cleanup
|
|
674
|
+
* later throws) must NOT have its time-index claim restored, since re-firing
|
|
675
|
+
* an already-kicked job would break idempotency. A `false` return (pool
|
|
676
|
+
* backpressure or a failed dispatch) means the job is still re-fireable —
|
|
677
|
+
* either already re-armed here, or, if a throw escapes, re-claimed by the
|
|
678
|
+
* guard's catch.
|
|
679
|
+
*/
|
|
680
|
+
private drainRecord;
|
|
681
|
+
/**
|
|
682
|
+
* Concurrency gate for a pooled record. Returns `false` (and re-arms the
|
|
683
|
+
* job via {@link requeuePooled}) when the pool is at `maxConcurrency`;
|
|
684
|
+
* otherwise reserves a slot durably and returns `true`. Non-pooled records
|
|
685
|
+
* always return `true` without touching any pool state.
|
|
686
|
+
*/
|
|
687
|
+
private reservePoolSlot;
|
|
688
|
+
/**
|
|
689
|
+
* Accept a hibernatable live subscription to the job list. The scheduler has
|
|
690
|
+
* exactly one subscription shape (the whole list), so there's no per-socket
|
|
691
|
+
* registry or dependency tracking — every accepted socket gets the full list
|
|
692
|
+
* on connect and on every change. The worker is responsible for gating the
|
|
693
|
+
* upgrade behind the admin token before it reaches here.
|
|
694
|
+
*/
|
|
695
|
+
private handleWebSocketUpgrade;
|
|
696
|
+
/**
|
|
697
|
+
* Re-list the jobs and push them to every connected subscriber. Called after
|
|
698
|
+
* any change (schedule / cancel / alarm-fire) so live studios reflect it
|
|
699
|
+
* immediately. A no-op when the runtime doesn't support hibernated sockets.
|
|
700
|
+
*/
|
|
701
|
+
private broadcastChange;
|
|
702
|
+
/** The current pending job records (shared by `/list` and the live channel). */
|
|
703
|
+
private listRecords;
|
|
704
|
+
/**
|
|
705
|
+
* HMAC-SHA-256 sign the dispatch body with `env.LUNORA_SCHEDULER_SECRET`,
|
|
706
|
+
* returning a base64url signature, or `undefined` when no secret is
|
|
707
|
+
* configured. Mirrors `@lunora/storage`'s signed-URL HMAC pattern (WebCrypto
|
|
708
|
+
* `crypto.subtle`, available in workerd).
|
|
709
|
+
*/
|
|
710
|
+
private signDispatch;
|
|
711
|
+
/**
|
|
712
|
+
* Move a failed record into the retry pipeline with configurable backoff.
|
|
713
|
+
* The retry budget/backoff comes from the record's {@link RetryPolicy}
|
|
714
|
+
* (falling back to the DO defaults); on exhaustion the record is parked
|
|
715
|
+
* under a `dead:` key for manual inspection.
|
|
716
|
+
*/
|
|
717
|
+
private recordRetry;
|
|
718
|
+
/** Read the durable `pool:<name>` row, defaulting to a fresh `inFlight: 0` pool. */
|
|
719
|
+
private loadPool;
|
|
720
|
+
private savePool;
|
|
721
|
+
/**
|
|
722
|
+
* Re-arm a pooled job that couldn't run because its pool was at capacity.
|
|
723
|
+
* No attempt is charged (this is backpressure, not a failure): the job is
|
|
724
|
+
* pushed `POOL_BACKPRESSURE_DELAY_MS` into the future so a later alarm
|
|
725
|
+
* drains it once a slot frees, keeping its `id:` header and retry policy.
|
|
726
|
+
*/
|
|
727
|
+
private requeuePooled;
|
|
728
|
+
/**
|
|
729
|
+
* Release a pool slot when the runtime reports an action finished. This is
|
|
730
|
+
* the durable-semaphore decrement: dispatch() only KICKS the action and
|
|
731
|
+
* holds the slot; the runtime calls back here (`POST /complete { id }`) once
|
|
732
|
+
* the action settles, freeing the slot for the next queued job. Idempotent
|
|
733
|
+
* and safe if the job/pool is already gone.
|
|
734
|
+
*/
|
|
735
|
+
private handleComplete;
|
|
736
|
+
/** `GET /pool?name=` — inspect a pool's slot usage + queued count. */
|
|
737
|
+
private handlePoolStatus;
|
|
738
|
+
/**
|
|
739
|
+
* `GET /status` — the app-level backlog signal that powers the studio's
|
|
740
|
+
* SLO view. Enumerates every durable `pool:<name>` row for its `inFlight`/
|
|
741
|
+
* `maxConcurrency` semaphore, counts the pending (not-yet-dispatched) jobs
|
|
742
|
+
* routed to each pool with the same single-pass scan {@link handlePoolStatus}
|
|
743
|
+
* uses, and rolls those up into app-wide `backlog` (sum of `queued`) and
|
|
744
|
+
* `inFlight` (sum of held slots) totals.
|
|
745
|
+
*
|
|
746
|
+
* Pools that have rows but no queued jobs still appear (with `queued: 0`) so
|
|
747
|
+
* a saturated-but-idle pool stays visible; a pool that only ever existed as
|
|
748
|
+
* queued jobs without a persisted row is unreachable here (the schedule path
|
|
749
|
+
* always writes a `pool:<name>` row before the job's header), so a single
|
|
750
|
+
* scan over `pool:`/`id:` is sufficient.
|
|
751
|
+
*/
|
|
752
|
+
private handleStatus;
|
|
753
|
+
private handleSchedule;
|
|
754
|
+
private handleCancel;
|
|
755
|
+
private handleList;
|
|
756
|
+
/**
|
|
757
|
+
* `GET /dead` — list the dead-letter records: jobs that exhausted their
|
|
758
|
+
* retry budget ({@link recordRetry}) and were parked under `dead:<id>`
|
|
759
|
+
* instead of being silently dropped. These never appear in `/list` (their
|
|
760
|
+
* `id:` header is deleted on park), so this is the ONLY way the studio can
|
|
761
|
+
* surface — and recover — a permanently-failed job.
|
|
762
|
+
*/
|
|
763
|
+
private handleDeadList;
|
|
764
|
+
/**
|
|
765
|
+
* `POST /dead/retry { id }` — resurrect a dead-letter record: reset its
|
|
766
|
+
* exhausted attempt count to 0 (a fresh retry budget), re-arm it for
|
|
767
|
+
* immediate dispatch via the standard time index, and drop the `dead:` row.
|
|
768
|
+
* The new `id:` header makes it visible to `/list` and the live `/ws`
|
|
769
|
+
* subscription again. A miss is a no-op (`{ retried: false }`).
|
|
770
|
+
*/
|
|
771
|
+
private handleDeadRetry;
|
|
772
|
+
/**
|
|
773
|
+
* `POST /dead/cancel { id }` — permanently drop a dead-letter record the
|
|
774
|
+
* operator has decided not to recover. Returns `{ removed }` (false when
|
|
775
|
+
* nothing matched). Idempotent: a repeated purge is a harmless no-op.
|
|
776
|
+
*/
|
|
777
|
+
private handleDeadCancel;
|
|
778
|
+
/**
|
|
779
|
+
* Resolve a single pending job by id via a direct `id:<id>` storage read —
|
|
780
|
+
* O(1), versus scanning the whole `/list` view. Responds `{ record }` on a
|
|
781
|
+
* hit and `{}` on a miss (an absent `record` field — JSON has no `undefined`
|
|
782
|
+
* — which the client reads back as `null`).
|
|
783
|
+
*/
|
|
784
|
+
private handleGet;
|
|
785
|
+
private removeRecord;
|
|
786
|
+
/**
|
|
787
|
+
* Arm the alarm for `scheduledFor` only if it is sooner than the currently
|
|
788
|
+
* set alarm (or none is set). Used on the schedule path: inserting a job
|
|
789
|
+
* can only ever pull the earliest-pending time *earlier*, never later, so a
|
|
790
|
+
* full `t:` rescan is unnecessary unless the new job is the new earliest.
|
|
791
|
+
*/
|
|
792
|
+
private armAlarmIfEarlier;
|
|
793
|
+
private rescheduleAlarm;
|
|
794
|
+
}
|
|
795
|
+
/** Standard cron expression (5- or 6-field) or a supported `@macro`, per `cron-parser`. */
|
|
796
|
+
declare const isValidCronExpression: (schedule: string) => boolean;
|
|
797
|
+
/**
|
|
798
|
+
* Assert a raw cron expression is well-formed, throwing the same shaped error
|
|
799
|
+
* both cron surfaces use. The `context` prefix lets callers name the offending
|
|
800
|
+
* job (`cron job "send digest"`) vs. the bare trigger.
|
|
801
|
+
*/
|
|
802
|
+
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 };
|