logic-sdk 0.7.1 → 0.7.3
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/README.md +9 -10
- package/dist/adapters/cloudflare-queue.d.ts +15 -0
- package/dist/adapters/cloudflare-queue.js +58 -0
- package/dist/adapters/drizzle-pg.d.ts +280 -0
- package/dist/adapters/drizzle-pg.js +78 -0
- package/dist/adapters/drizzle-sqlite.d.ts +273 -0
- package/dist/adapters/drizzle-sqlite.js +203 -0
- package/dist/adapters/postgres.d.ts +33 -0
- package/dist/adapters/postgres.js +129 -0
- package/dist/adapters/sqlite.d.ts +13 -0
- package/dist/adapters/sqlite.js +108 -0
- package/dist/adapters/stored-jobs.d.ts +89 -0
- package/dist/adapters/stored-jobs.js +370 -0
- package/dist/ai.d.ts +1 -32
- package/dist/ai.js +1 -40
- package/dist/cloudflare.d.ts +2 -55
- package/dist/cloudflare.js +2 -85
- package/dist/core/in-process.d.ts +9 -0
- package/dist/core/in-process.js +37 -0
- package/dist/core/jobs.d.ts +63 -0
- package/dist/core/jobs.js +514 -0
- package/dist/core/registry.d.ts +156 -0
- package/dist/core/registry.js +515 -0
- package/dist/core/testing.d.ts +75 -0
- package/dist/core/testing.js +232 -0
- package/dist/{types.d.ts → core/types.d.ts} +289 -64
- package/dist/drizzle/pg.d.ts +3 -236
- package/dist/drizzle/pg.js +2 -67
- package/dist/drizzle/sqlite.d.ts +3 -242
- package/dist/drizzle/sqlite.js +2 -194
- package/dist/fetch.d.ts +1 -19
- package/dist/fetch.js +1 -30
- package/dist/hosts/cloudflare.d.ts +43 -0
- package/dist/hosts/cloudflare.js +40 -0
- package/dist/hosts/node.d.ts +36 -0
- package/dist/hosts/node.js +103 -0
- package/dist/index.d.ts +5 -122
- package/dist/index.js +4 -492
- package/dist/integrations/ai.d.ts +32 -0
- package/dist/integrations/ai.js +40 -0
- package/dist/integrations/fetch.d.ts +19 -0
- package/dist/integrations/fetch.js +30 -0
- package/dist/integrations/orpc.d.ts +22 -0
- package/dist/integrations/orpc.js +37 -0
- package/dist/node.d.ts +1 -26
- package/dist/node.js +1 -76
- package/dist/orpc.d.ts +1 -21
- package/dist/orpc.js +1 -37
- package/dist/postgres.d.ts +2 -28
- package/dist/postgres.js +1 -82
- package/dist/sqlite.d.ts +2 -9
- package/dist/sqlite.js +1 -65
- package/dist/testing.d.ts +1 -75
- package/dist/testing.js +1 -227
- package/package.json +2 -12
- package/skills/logic-sdk/SKILL.md +6 -5
- package/src/adapters/cloudflare-queue.ts +65 -0
- package/src/adapters/drizzle-pg.ts +117 -0
- package/src/adapters/drizzle-sqlite.ts +241 -0
- package/src/adapters/postgres.ts +136 -0
- package/src/adapters/sqlite.ts +116 -0
- package/src/adapters/stored-jobs.ts +463 -0
- package/src/ai.ts +1 -62
- package/src/cloudflare.ts +2 -107
- package/src/core/in-process.ts +38 -0
- package/src/core/jobs.ts +548 -0
- package/src/core/registry.ts +719 -0
- package/src/core/testing.ts +309 -0
- package/src/{types.ts → core/types.ts} +280 -60
- package/src/drizzle/pg.ts +3 -110
- package/src/drizzle/sqlite.ts +3 -236
- package/src/fetch.ts +1 -31
- package/src/hosts/cloudflare.ts +55 -0
- package/src/hosts/node.ts +126 -0
- package/src/index.ts +17 -652
- package/src/integrations/ai.ts +63 -0
- package/src/integrations/fetch.ts +31 -0
- package/src/integrations/orpc.ts +47 -0
- package/src/node.ts +1 -99
- package/src/orpc.ts +1 -46
- package/src/postgres.ts +2 -90
- package/src/sqlite.ts +2 -74
- package/src/testing.ts +1 -305
- package/dist/jobs.d.ts +0 -44
- package/dist/jobs.js +0 -260
- package/dist/pg-boss.d.ts +0 -53
- package/dist/pg-boss.js +0 -213
- package/dist/stored-jobs.d.ts +0 -64
- package/dist/stored-jobs.js +0 -188
- package/src/jobs.ts +0 -307
- package/src/pg-boss.ts +0 -241
- package/src/stored-jobs.ts +0 -260
- /package/dist/{errors.d.ts → core/errors.d.ts} +0 -0
- /package/dist/{errors.js → core/errors.js} +0 -0
- /package/dist/{schedule.d.ts → core/schedule.d.ts} +0 -0
- /package/dist/{schedule.js → core/schedule.js} +0 -0
- /package/dist/{types.js → core/types.js} +0 -0
- /package/src/{errors.ts → core/errors.ts} +0 -0
- /package/src/{schedule.ts → core/schedule.ts} +0 -0
|
@@ -28,7 +28,7 @@ export interface Config {
|
|
|
28
28
|
*/
|
|
29
29
|
config?: object;
|
|
30
30
|
/**
|
|
31
|
-
* The shape of `meta`: yours, plus what your adapters read (`AppMeta & AIMeta
|
|
31
|
+
* The shape of `meta`: yours, plus what your adapters read (`AppMeta & AIMeta`). Yours goes on functions
|
|
32
32
|
* and listeners, each adapter's only where it reads it.
|
|
33
33
|
*/
|
|
34
34
|
meta?: object;
|
|
@@ -56,7 +56,7 @@ declare const FOR_LISTENERS: unique symbol;
|
|
|
56
56
|
export interface ForFunctions<M> {
|
|
57
57
|
readonly [FOR_FUNCTIONS]?: M;
|
|
58
58
|
}
|
|
59
|
-
/** An adapter's part of `meta`, read on listeners: `type
|
|
59
|
+
/** An adapter's part of `meta`, read on listeners: `type QueueMeta = ForListeners<{ queue?: ... }>`. */
|
|
60
60
|
export interface ForListeners<M> {
|
|
61
61
|
readonly [FOR_LISTENERS]?: M;
|
|
62
62
|
}
|
|
@@ -171,6 +171,12 @@ export interface Delivery {
|
|
|
171
171
|
attempts: number;
|
|
172
172
|
/** When it may run (ISO), for a listener with a `delay`. Absent: right away. */
|
|
173
173
|
runAt?: string;
|
|
174
|
+
/** Its listener's `key`, as `<listener id>@<key>`: jobs with the same one run one at a time, in order. */
|
|
175
|
+
key?: string;
|
|
176
|
+
/** Its listener's `unique`, as `<listener id>@<value>`: a later job with the same one is dropped while this one waits. */
|
|
177
|
+
unique?: string;
|
|
178
|
+
/** Its queue, when it isn't `"default"`: a worker drains its queues in the order it lists them. */
|
|
179
|
+
queue?: string;
|
|
174
180
|
}
|
|
175
181
|
|
|
176
182
|
/** One run of a cron: `id` is `<cron id>@<minute>` (`reports.weekly@20261123T0900Z`), so a tick runs once. */
|
|
@@ -185,9 +191,11 @@ export interface CronJob {
|
|
|
185
191
|
attempts: number;
|
|
186
192
|
/** When it may run (ISO), like a delivery's. Absent for crons: they run when ticked. */
|
|
187
193
|
runAt?: string;
|
|
194
|
+
/** Its queue, when it isn't `"default"`: a worker drains its queues in the order it lists them. */
|
|
195
|
+
queue?: string;
|
|
188
196
|
}
|
|
189
197
|
|
|
190
|
-
/** A function `ctx.schedule` asked to run later: `id` is `<function id>@<uuid
|
|
198
|
+
/** A function `ctx.schedule` asked to run later: `id` is `<function id>@<uuid>`. */
|
|
191
199
|
export interface CallJob {
|
|
192
200
|
kind: "call";
|
|
193
201
|
id: string;
|
|
@@ -203,10 +211,12 @@ export interface CallJob {
|
|
|
203
211
|
* function that schedules itself with no delay stops like a listener loop. A delayed one starts a new chain (0).
|
|
204
212
|
*/
|
|
205
213
|
depth: number;
|
|
206
|
-
/**
|
|
207
|
-
replace?:
|
|
214
|
+
/** `true`: a newer call to `fn` with the same input replaces this one while it waits (the function and its input are the identity). */
|
|
215
|
+
replace?: true;
|
|
208
216
|
/** When it may run (ISO). */
|
|
209
217
|
runAt?: string;
|
|
218
|
+
/** Its queue, when it isn't `"default"`: a worker drains its queues in the order it lists them. */
|
|
219
|
+
queue?: string;
|
|
210
220
|
}
|
|
211
221
|
|
|
212
222
|
/**
|
|
@@ -224,8 +234,12 @@ export interface Cron<C extends Config = any> extends FromInit<C> {
|
|
|
224
234
|
readonly timezone?: string;
|
|
225
235
|
readonly scope: object;
|
|
226
236
|
readonly attempts?: number;
|
|
237
|
+
/** Its queue, one of `jobs.queues`. Default `"default"`. */
|
|
238
|
+
readonly queue?: string;
|
|
227
239
|
readonly meta?: Readonly<Record<string, unknown>>;
|
|
228
240
|
readonly handler: (ctx: never) => Promise<unknown>;
|
|
241
|
+
/** Runs once its last try has failed: `(ctx, error)`. */
|
|
242
|
+
readonly failed?: (ctx: never, error: unknown) => Promise<unknown>;
|
|
229
243
|
}
|
|
230
244
|
|
|
231
245
|
/** The run a cron's handler is doing. No scope here: `ctx.scope` is the one it runs as. */
|
|
@@ -246,6 +260,18 @@ export interface CronRun {
|
|
|
246
260
|
*/
|
|
247
261
|
export type JobOutcome = "done" | "failed" | "discarded";
|
|
248
262
|
|
|
263
|
+
/**
|
|
264
|
+
* What `ctx.emit` queued: the `id`, `name` and `occurredAt` its listeners see as `ctx.event`. `payload` is a copy of what
|
|
265
|
+
* you passed; listeners get it parsed by the event's schema.
|
|
266
|
+
*/
|
|
267
|
+
export interface EmittedEvent<I = unknown> {
|
|
268
|
+
readonly id: string;
|
|
269
|
+
readonly name: string;
|
|
270
|
+
readonly payload: I;
|
|
271
|
+
/** ISO. */
|
|
272
|
+
readonly occurredAt: string;
|
|
273
|
+
}
|
|
274
|
+
|
|
249
275
|
/** The event a listener is handling. No scope here: `ctx.scope` is the one it runs as. */
|
|
250
276
|
export interface ListenerEvent<O = unknown> {
|
|
251
277
|
id: string;
|
|
@@ -269,8 +295,18 @@ export interface Listener<O = unknown, C extends Config = any> extends FromInit<
|
|
|
269
295
|
readonly delay?: number;
|
|
270
296
|
/** Whether this event gets a job for this listener, from the parsed payload, at commit. Default: every event. */
|
|
271
297
|
readonly when?: (payload: O) => boolean;
|
|
298
|
+
/** Jobs with the same key run one at a time, in the order their events were emitted. */
|
|
299
|
+
readonly key?: (payload: O) => string;
|
|
300
|
+
/** While a job with the same value hasn't started, later ones are dropped. */
|
|
301
|
+
readonly unique?: (payload: O) => string;
|
|
302
|
+
/** Seconds a try may run before it's stopped and counted as failed. */
|
|
303
|
+
readonly timeout?: number;
|
|
304
|
+
/** Its queue, one of `jobs.queues`. Default `"default"`. */
|
|
305
|
+
readonly queue?: string;
|
|
272
306
|
readonly meta?: Readonly<Record<string, unknown>>;
|
|
273
307
|
readonly handler: (ctx: never, payload: O) => Promise<unknown>;
|
|
308
|
+
/** Runs once its last try has failed: `(ctx, payload, error)`. */
|
|
309
|
+
readonly failed?: (ctx: never, payload: O, error: unknown) => Promise<unknown>;
|
|
274
310
|
}
|
|
275
311
|
|
|
276
312
|
/** What a single call may change: who it runs as (part of the scope) and when to give up. Nothing else. */
|
|
@@ -329,28 +365,40 @@ export interface MutationCtx<C extends Config, S = ScopeOf<C>> extends BaseCtx<C
|
|
|
329
365
|
* `await ctx.run(availability.hold, { propertyId })`. If it fails, the whole transaction rolls back.
|
|
330
366
|
*/
|
|
331
367
|
run: Run<"query" | "mutation", ScopeOf<C>>;
|
|
332
|
-
/**
|
|
333
|
-
|
|
368
|
+
/**
|
|
369
|
+
* Queue an event. Delivered to listeners only after the transaction commits. Returns what it queued: the id, name and
|
|
370
|
+
* time its listeners see as `ctx.event`, so a row you write in this transaction can carry `event.id` and a listener
|
|
371
|
+
* finds it by `ctx.event.id`.
|
|
372
|
+
*/
|
|
373
|
+
emit<I>(event: EventDef<I, unknown>, payload: I): EmittedEvent<I>;
|
|
334
374
|
/**
|
|
335
375
|
* Run a mutation or action once, later, as this scope: `ctx.schedule(guests.remind, { bookingId }, { at })` or
|
|
336
|
-
* `{ delay: 3600 }` (seconds). Committed with this transaction, so a rollback never leaves one behind.
|
|
337
|
-
*
|
|
338
|
-
* `attempts`: tries in total, default 1; give an action that calls an API a few.
|
|
339
|
-
* with
|
|
376
|
+
* `{ delay: 3600 }` (seconds). Committed with this transaction, so a rollback never leaves one behind. Nothing to
|
|
377
|
+
* keep: there's no cancel. It runs whatever happens meanwhile, so it checks that its reason still holds ("still
|
|
378
|
+
* confirmed?") against your own row. `attempts`: tries in total, default 1; give an action that calls an API a few.
|
|
379
|
+
* `replace: true`: a newer call to this function with the same input replaces the one still waiting (the dates of
|
|
380
|
+
* `{ bookingId }` changed), like Oban's `replace`. Keep the input to ids, so it names one thing.
|
|
340
381
|
*/
|
|
341
|
-
schedule<F extends Fn<"mutation" | "action">>(fn: F, input: FnIn<F>, options: ScheduleOptions):
|
|
382
|
+
schedule<F extends Fn<"mutation" | "action">>(fn: F, input: FnIn<F>, options: ScheduleOptions<ScopeOf<C>>): void;
|
|
342
383
|
}
|
|
343
384
|
/** When a scheduled call runs (`at` or `delay` in seconds), and how many tries it gets. */
|
|
344
|
-
export type ScheduleOptions = ({ at: Date; delay?: never } | { delay: number; at?: never }) & {
|
|
385
|
+
export type ScheduleOptions<S = object> = ({ at: Date; delay?: never } | { delay: number; at?: never }) & {
|
|
345
386
|
attempts?: number;
|
|
346
|
-
/**
|
|
347
|
-
replace?:
|
|
387
|
+
/** `true`: the call to this function waiting with the same input (not started yet) is replaced by this one. */
|
|
388
|
+
replace?: true;
|
|
389
|
+
/** Its queue, one of `jobs.queues`. Default `"default"`. */
|
|
390
|
+
queue?: string;
|
|
391
|
+
/**
|
|
392
|
+
* Who it runs as, whole, never merged, as `ctx.run` takes it: `{ scope: { kind: "system", job: "tasks.wake", organization } }`.
|
|
393
|
+
* Default: this mutation's scope. The function's policy checks it when it runs.
|
|
394
|
+
*/
|
|
395
|
+
scope?: S;
|
|
348
396
|
};
|
|
349
397
|
export interface ActionCtx<C extends Config, S = ScopeOf<C>> extends BaseCtx<C, S> {
|
|
350
398
|
services: ServicesOf<C>;
|
|
351
399
|
/** Call any function: `await ctx.run(bookings.confirmPaid, { bookingId })`. Each mutation you run commits on its own. */
|
|
352
400
|
run: Run<Kind, ScopeOf<C>>;
|
|
353
|
-
/** The caller's abort signal: pass it on, `fetch(url, { signal: ctx.signal })`.
|
|
401
|
+
/** The caller's abort signal: pass it on, `fetch(url, { signal: ctx.signal })`. In a listener, aborted when its `timeout` runs out (else undefined: listeners outlive the request). */
|
|
354
402
|
signal: AbortSignal | undefined;
|
|
355
403
|
}
|
|
356
404
|
export interface ListenerCtx<C extends Config, O = unknown> extends ActionCtx<C> {
|
|
@@ -432,33 +480,34 @@ export interface DatabaseAdapter<Db = unknown, Tx = Db> {
|
|
|
432
480
|
nest?(tx: Tx): DatabaseAdapter<Tx, Tx>;
|
|
433
481
|
/** Did this transaction lose a race the database says to retry (deadlock, serialization failure)? */
|
|
434
482
|
retryable?(error: unknown): boolean;
|
|
435
|
-
/**
|
|
436
|
-
* Tries in total for a top-level mutation whose transaction is `retryable`, each in a fresh one, like Laravel's
|
|
437
|
-
* `DB::transaction(fn, attempts)`. Default 3; 1 turns it off. A job's own tries are its `attempts`.
|
|
438
|
-
*/
|
|
439
|
-
transactionAttempts?: number;
|
|
440
|
-
/**
|
|
441
|
-
* One SQL statement (`$1` parameters on Postgres, `?` on SQLite), on the database or on a transaction of it. Resolves the rows it
|
|
442
|
-
* returns. `databaseQueue` and `outbox` run their table through it, so any ORM that can run SQL has them.
|
|
443
|
-
*/
|
|
444
|
-
query?(client: Db | Tx, text: string, params: unknown[]): Promise<Record<string, unknown>[]>;
|
|
445
483
|
}
|
|
446
484
|
|
|
447
|
-
/** A database adapter
|
|
485
|
+
/** A database adapter that runs SQL: what `databaseQueue()` and `outbox(queue)` need. */
|
|
448
486
|
export interface SqlDatabase<Db = any, Tx = any> extends DatabaseAdapter<Db, Tx> {
|
|
487
|
+
/**
|
|
488
|
+
* One SQL statement (`$1` parameters on Postgres, `?` on SQLite), on the database or on a transaction of it. Resolves
|
|
489
|
+
* the rows it returns. The jobs table runs through it, so any ORM that can run SQL has it.
|
|
490
|
+
*/
|
|
449
491
|
query(client: Db | Tx, text: string, params: unknown[]): Promise<Record<string, unknown>[]>;
|
|
450
492
|
}
|
|
451
493
|
|
|
494
|
+
/**
|
|
495
|
+
* `createRegistry({ database })`: the adapter, and how the core uses it. `transactionAttempts`: tries in total for a
|
|
496
|
+
* top-level mutation whose transaction lost a race (`retryable`), each in a fresh one, like Laravel's
|
|
497
|
+
* `DB::transaction(fn, attempts)`. Default 3; 1 turns it off. A job's own tries are its `attempts`.
|
|
498
|
+
*/
|
|
499
|
+
export interface DatabaseOptions<A extends DatabaseAdapter<any, any> = DatabaseAdapter<unknown, unknown>> {
|
|
500
|
+
adapter: A;
|
|
501
|
+
transactionAttempts?: number;
|
|
502
|
+
}
|
|
503
|
+
|
|
452
504
|
/** Runs one job: resolves when it's done or failed for good (acknowledge it), throws when a retry might help. */
|
|
453
505
|
export type RunJob = (job: Job, options?: RunJobOptions) => Promise<JobOutcome>;
|
|
454
506
|
|
|
455
507
|
export interface RunJobOptions {
|
|
456
508
|
/** 1 on the first try; the adapter counts. */
|
|
457
509
|
attempt?: number;
|
|
458
|
-
/**
|
|
459
|
-
* Called with the error when the job fails for good, before `run` resolves `"failed"`: keep it. Default: the adapter's
|
|
460
|
-
* `fail` (`databaseQueue` and `outbox` write a failed row).
|
|
461
|
-
*/
|
|
510
|
+
/** Called with the error when the job fails for good, before `run` resolves `"failed"`: keep it (a failed row). */
|
|
462
511
|
fail?: (error: unknown) => Promise<void>;
|
|
463
512
|
}
|
|
464
513
|
|
|
@@ -478,6 +527,23 @@ export interface Runner {
|
|
|
478
527
|
}
|
|
479
528
|
|
|
480
529
|
export interface JobsAdapter {
|
|
530
|
+
/** How it's set up, for messages: `"databaseQueue()"`. The core names capabilities, the adapter itself. */
|
|
531
|
+
name?: string;
|
|
532
|
+
/**
|
|
533
|
+
* It can run a job again after a failed try: a queue redelivers it, a table backs off. Without it (`inProcess()`),
|
|
534
|
+
* `createRegistry` refuses `attempts` above 1 rather than ignore them, as it refuses a delay it can't hold.
|
|
535
|
+
*/
|
|
536
|
+
retries?: true;
|
|
537
|
+
/**
|
|
538
|
+
* It runs jobs with the same `key` one at a time, in the order they were queued, like an SQS FIFO queue's message
|
|
539
|
+
* groups. Without it, `createRegistry` refuses a listener with a `key`.
|
|
540
|
+
*/
|
|
541
|
+
orders?: true;
|
|
542
|
+
/**
|
|
543
|
+
* It writes jobs with SQL in the mutation's transaction, through the registry's database: `createRegistry` refuses a
|
|
544
|
+
* registry whose `database.adapter` doesn't run SQL (`query`), and `connect` gets it as `setup.sql`.
|
|
545
|
+
*/
|
|
546
|
+
sql?: true;
|
|
481
547
|
/**
|
|
482
548
|
* It can hold a job until its `runAt` (a listener's `delay`, `ctx.schedule`): `true`, or for at most `max` seconds.
|
|
483
549
|
* Without it (`inProcess()`), `createRegistry` refuses delayed listeners and `ctx.schedule` throws, rather than run
|
|
@@ -485,30 +551,40 @@ export interface JobsAdapter {
|
|
|
485
551
|
*/
|
|
486
552
|
holds?: true | { max: number };
|
|
487
553
|
/**
|
|
488
|
-
* Can
|
|
489
|
-
*
|
|
490
|
-
* Without it, `ctx.schedule` with `replace` is refused
|
|
554
|
+
* Can keep one waiting job per key: `enqueue` gets a call with `replace`, and a pending call with the same key
|
|
555
|
+
* (`storedUniqueKey`) that hasn't started is replaced by it; a delivery with `unique` is dropped while one with the
|
|
556
|
+
* same key waits. A queue that already has the message can't. Without it, `ctx.schedule` with `replace` is refused,
|
|
557
|
+
* and so is `unique` on an adapter that holds jobs.
|
|
491
558
|
*/
|
|
492
559
|
replaces?: true;
|
|
493
|
-
/**
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
560
|
+
/**
|
|
561
|
+
* It keeps jobs of different queues apart: any name (`true`), or only these (a queue per binding). Without it,
|
|
562
|
+
* `createRegistry` refuses `jobs.queues`; with a list, a declared queue (or `"default"`) it doesn't have.
|
|
563
|
+
*/
|
|
564
|
+
queues?: true | readonly string[];
|
|
565
|
+
/**
|
|
566
|
+
* At `createRegistry`, once: take what the adapter needs from the registry (its database, for an adapter that writes
|
|
567
|
+
* jobs in the mutation's transaction), and refuse a setup it can't honour.
|
|
568
|
+
*/
|
|
569
|
+
connect?(setup: AdapterSetup): void;
|
|
499
570
|
/** Inside the transaction, before commit. In-process: nothing. `databaseQueue` and `outbox`: insert one row per job with `tx`. */
|
|
500
571
|
enqueue?(tx: unknown, jobs: Job[]): Promise<void>;
|
|
501
572
|
/** After commit: run them, or hand them to a queue. A throw is reported to `onError`. */
|
|
502
573
|
deliver(jobs: Job[], runner: Runner): Promise<void>;
|
|
503
574
|
/** Run what's due (retries, deliveries whose runner crashed). Only adapters that store deliveries have it. */
|
|
504
|
-
sweep?(runner: Runner, options?:
|
|
575
|
+
sweep?(runner: Runner, options?: SweepOptions): Promise<SweepResult>;
|
|
505
576
|
/** Put a failed job back in line. `false` when there's no failed job with that id. Only adapters that store them. */
|
|
506
577
|
retry?(id: string): Promise<boolean>;
|
|
578
|
+
/** A page of failed jobs, newest first. Only adapters that store them and can filter them. */
|
|
579
|
+
failed?(options: FailedQuery): Promise<{ rows: FailedRow[]; cursor: string | null }>;
|
|
580
|
+
/** Delete one failed job. `false` when there's no failed job with that id: a waiting or running one is never touched. */
|
|
581
|
+
forget?(id: string): Promise<boolean>;
|
|
507
582
|
/**
|
|
508
|
-
*
|
|
509
|
-
*
|
|
583
|
+
* For an adapter that keeps its jobs as rows and sends a queue only signals: a consumer got this job's
|
|
584
|
+
* message, so run it from the rows (with a `key`, the key's next jobs, in order), recording each outcome itself.
|
|
585
|
+
* `registry.jobs.run` calls it in place of running the message.
|
|
510
586
|
*/
|
|
511
|
-
|
|
587
|
+
consume?(job: Job, runner: Runner): Promise<void>;
|
|
512
588
|
}
|
|
513
589
|
|
|
514
590
|
/**
|
|
@@ -525,6 +601,110 @@ export interface FailureInfo {
|
|
|
525
601
|
outcome?: "retry" | "failed" | "discarded";
|
|
526
602
|
}
|
|
527
603
|
|
|
604
|
+
/** What `createRegistry` gives an adapter's `connect`. */
|
|
605
|
+
export interface AdapterSetup {
|
|
606
|
+
/** The registry's `database.adapter`, when it runs SQL: always set for an adapter that declares `sql: true`. */
|
|
607
|
+
sql: SqlDatabase | undefined;
|
|
608
|
+
/**
|
|
609
|
+
* The loops running jobs in this process (`work()`, through `registry.jobs.attach`): whether one takes `job`, so the
|
|
610
|
+
* request leaves it to them instead of running it, and `wake(count)` once that many are committed.
|
|
611
|
+
*/
|
|
612
|
+
workers: { takes(job: Job): boolean; wake(count: number): void };
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/** What `registry.jobs.sweep` and an adapter's `sweep` take. */
|
|
616
|
+
export interface SweepOptions {
|
|
617
|
+
/** At most this many jobs. Default 100. */
|
|
618
|
+
limit?: number;
|
|
619
|
+
/** Only these queues, in this order: each claim takes the first one with a job due. Default: every queue. */
|
|
620
|
+
queues?: readonly string[];
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
/** A loop running jobs in this process: `registry.jobs.attach({ wake, queues })`. */
|
|
624
|
+
export interface LocalWorker {
|
|
625
|
+
/** How many jobs just committed for it: claim them now. */
|
|
626
|
+
wake(count: number): void;
|
|
627
|
+
/** The queues it runs. Default: all. */
|
|
628
|
+
queues?: readonly string[];
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* A store for failed jobs, apart from the queue that runs them, like Laravel's `failed` driver. `failedJobs()`
|
|
633
|
+
* keeps them in the jobs table. For adapters that keep their own, it isn't needed.
|
|
634
|
+
*/
|
|
635
|
+
export interface FailedJobs {
|
|
636
|
+
/** For messages: `"failedJobs()"`. */
|
|
637
|
+
name?: string;
|
|
638
|
+
/** Keeps failed jobs with SQL, through the registry's database (`setup.sql`): see `JobsAdapter.sql`. */
|
|
639
|
+
sql?: true;
|
|
640
|
+
/** At `createRegistry`, once: take the registry's database. */
|
|
641
|
+
connect?(setup: AdapterSetup): void;
|
|
642
|
+
/** Keep a job that failed for good. */
|
|
643
|
+
fail(job: Job, error: unknown): Promise<void>;
|
|
644
|
+
/** A page of failed jobs, newest first. */
|
|
645
|
+
failed(query: FailedQuery): Promise<{ rows: FailedRow[]; cursor: string | null }>;
|
|
646
|
+
/** Remove a failed job and return it, for `retry` to hand to the queue again. Undefined when there's none with that id. */
|
|
647
|
+
take(id: string): Promise<Job | undefined>;
|
|
648
|
+
/** Delete a failed job. `false` when there's none with that id. */
|
|
649
|
+
forget(id: string): Promise<boolean>;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/** One field of a stored scope a `failed({ scope })` filter matches exactly: `["organization", "id"]` and `"org_1"`. */
|
|
653
|
+
export interface ScopeMatch {
|
|
654
|
+
path: string[];
|
|
655
|
+
value: string | number | boolean | null;
|
|
656
|
+
}
|
|
657
|
+
/** What `registry.jobs.failed` asks an adapter for. The core checks and flattens the filter first. */
|
|
658
|
+
export interface FailedQuery {
|
|
659
|
+
/** The listener, cron or function id. */
|
|
660
|
+
source?: string;
|
|
661
|
+
/** Every one must equal the stored scope's field at `path` (the job's `scope`, or its envelope's). */
|
|
662
|
+
scope?: ScopeMatch[];
|
|
663
|
+
limit: number;
|
|
664
|
+
/** From the last page: where to go on. */
|
|
665
|
+
cursor?: string;
|
|
666
|
+
}
|
|
667
|
+
/** A failed job as an adapter keeps it. */
|
|
668
|
+
export interface FailedRow {
|
|
669
|
+
id: string;
|
|
670
|
+
job: Job;
|
|
671
|
+
error: string;
|
|
672
|
+
/** ISO. */
|
|
673
|
+
failedAt: string;
|
|
674
|
+
}
|
|
675
|
+
/** A failed job, as `registry.jobs.failed` lists it: pass its `id` to `retry` or `forget`. */
|
|
676
|
+
export interface FailedJob<Stored = Record<string, unknown>> {
|
|
677
|
+
id: string;
|
|
678
|
+
kind: Job["kind"];
|
|
679
|
+
/** The listener, cron or function that failed, as in `FailureInfo`. */
|
|
680
|
+
source: string;
|
|
681
|
+
/** What `jobs.scope.store` saved with it. Undefined for a cron run: it has none. */
|
|
682
|
+
scope: Stored | undefined;
|
|
683
|
+
/** The last error, with its stack. */
|
|
684
|
+
error: string;
|
|
685
|
+
/** ISO. */
|
|
686
|
+
failedAt: string;
|
|
687
|
+
job: Job;
|
|
688
|
+
}
|
|
689
|
+
export interface FailedOptions<Stored = Record<string, unknown>> {
|
|
690
|
+
/** Only this listener's, cron's or function's: `"emails.send"`. */
|
|
691
|
+
source?: string;
|
|
692
|
+
/**
|
|
693
|
+
* Only jobs whose stored scope has these values (what `jobs.scope.store` saved): `{ organizationId: "org_1" }`,
|
|
694
|
+
* nested fields too. Never a cron run: it stores none. Values are strings, numbers, booleans or null.
|
|
695
|
+
*/
|
|
696
|
+
scope?: DeepPartial<Stored>;
|
|
697
|
+
/** 1 to 1000. Default 100. */
|
|
698
|
+
limit?: number;
|
|
699
|
+
/** The `cursor` of the page before. */
|
|
700
|
+
cursor?: string;
|
|
701
|
+
}
|
|
702
|
+
export interface FailedPage<Stored = Record<string, unknown>> {
|
|
703
|
+
jobs: FailedJob<Stored>[];
|
|
704
|
+
/** Pass it back for the next page; null on the last one. */
|
|
705
|
+
cursor: string | null;
|
|
706
|
+
}
|
|
707
|
+
|
|
528
708
|
/**
|
|
529
709
|
* Who a job runs as, at rest. `store` picks what's saved with the job (ids, not whole objects); `load` rebuilds the
|
|
530
710
|
* scope when it runs, so policies see today's roles. `load` returning null discards the job (reported to `onError`),
|
|
@@ -549,21 +729,32 @@ export interface InProcess extends JobsAdapter {
|
|
|
549
729
|
* checked at startup: requiring it here would report a missing one as a wrong `store`.
|
|
550
730
|
*/
|
|
551
731
|
export type JobsOptions<S = object, T = any> = {
|
|
732
|
+
/**
|
|
733
|
+
* Where failed jobs are kept when your adapter can't keep them itself (a queue on its own), like Laravel's `failed`
|
|
734
|
+
* driver: `failed: failedJobs()`. `registry.jobs.failed`, `retry` and `forget` then work on it.
|
|
735
|
+
*/
|
|
736
|
+
failed?: FailedJobs;
|
|
552
737
|
/**
|
|
553
738
|
* Max chain of listener → mutation → event → listener… before queueing stops, reported. Calls `ctx.schedule` runs
|
|
554
739
|
* right away count too, so a function that schedules itself with no delay stops the same way. Default 8 (real chains
|
|
555
740
|
* are 2 or 3).
|
|
556
741
|
*/
|
|
557
742
|
maxEventDepth?: number;
|
|
743
|
+
/**
|
|
744
|
+
* Queues besides `"default"`, for listeners, crons and `ctx.schedule` (`queue: "mail"`): `queues: ["mail", "reports"]`.
|
|
745
|
+
* A worker takes them in the order it lists them (`work(registry, { queues: ["mail", "default"] })`). A name not
|
|
746
|
+
* declared here is refused, at `createRegistry` or at the call.
|
|
747
|
+
*/
|
|
748
|
+
queues?: readonly string[];
|
|
558
749
|
} & (
|
|
559
750
|
| {
|
|
560
|
-
/** `inProcess()` (default), or a durable one like `databaseQueue(
|
|
751
|
+
/** `inProcess()` (default), or a durable one like `databaseQueue()`. Its own options go in its factory. */
|
|
561
752
|
adapter?: InProcess;
|
|
562
753
|
/** Applied by the registry in every adapter, `inProcess()` and tests included. */
|
|
563
754
|
scope?: JobScope<S, T>;
|
|
564
755
|
}
|
|
565
756
|
| {
|
|
566
|
-
/** An adapter that keeps jobs to run later, like `databaseQueue(
|
|
757
|
+
/** An adapter that keeps jobs to run later, like `databaseQueue()`. */
|
|
567
758
|
adapter?: JobsAdapter;
|
|
568
759
|
/** Required: such a job runs as the scope `load` rebuilds, with today's roles. */
|
|
569
760
|
scope: JobScope<S, T>;
|
|
@@ -573,6 +764,8 @@ export type JobsOptions<S = object, T = any> = {
|
|
|
573
764
|
export interface Call<M = unknown> {
|
|
574
765
|
id: string;
|
|
575
766
|
kind: Kind | "listener" | "cron";
|
|
767
|
+
/** Set on a job's own run (a listener's, a cron's, a scheduled call's): which job, and which try (1 on the first). */
|
|
768
|
+
job?: { id: string; attempt: number };
|
|
576
769
|
/** The function's, listener's or cron's `meta` (undefined when it has none). */
|
|
577
770
|
meta: M | undefined;
|
|
578
771
|
/** Raw input as received (before validation). */
|
|
@@ -584,20 +777,20 @@ export interface Call<M = unknown> {
|
|
|
584
777
|
export interface RegistryOptions<Mods extends Record<string, object>> {
|
|
585
778
|
/** One entry per module: `{ bookings, availability }` with `import * as bookings from "./bookings"`. */
|
|
586
779
|
modules: Mods;
|
|
587
|
-
database
|
|
780
|
+
/** `database: { adapter: drizzleAdapter(db) }`, with `transactionAttempts` when 3 isn't right. */
|
|
781
|
+
database?: DatabaseOptions;
|
|
588
782
|
services?: object;
|
|
589
783
|
/** Settings every function reads as `ctx.config`. Plain data: it's copied and frozen. */
|
|
590
784
|
config?: object;
|
|
591
|
-
/** Background work: `jobs: { adapter: databaseQueue(
|
|
785
|
+
/** Background work: `jobs: { adapter: databaseQueue(), scope: { store, load } }`. Default: in-process, scope as-is (nothing waits). */
|
|
592
786
|
jobs?: JobsOptions;
|
|
593
787
|
/** Wraps every call, including ones that fail validation or authorization: logging, tracing, audit. */
|
|
594
788
|
wrap?: (call: Call, next: () => Promise<unknown>) => Promise<unknown>;
|
|
595
789
|
/**
|
|
596
790
|
* Serverless: let listeners finish after the response. Pass the platform's global:
|
|
597
791
|
* `import { waitUntil } from "cloudflare:workers"` or `import { waitUntil } from "@vercel/functions"`.
|
|
598
|
-
* Without it, `run` waits for listeners
|
|
599
|
-
*
|
|
600
|
-
* still running at a restart is lost.
|
|
792
|
+
* Without it, `run` waits for listeners, unless a worker in this process takes them (`work(registry)` on Node or Bun,
|
|
793
|
+
* with `databaseQueue`): then the request answers at once.
|
|
601
794
|
*/
|
|
602
795
|
waitUntil?: (promise: Promise<unknown>) => void;
|
|
603
796
|
/** Failures no caller waits for: jobs, and the work after commit that runs them. They never fail the caller. */
|
|
@@ -628,6 +821,8 @@ export interface Overrides {
|
|
|
628
821
|
/** Merged over the registry's config, section by section. */
|
|
629
822
|
config?: unknown;
|
|
630
823
|
adapter?: JobsAdapter;
|
|
824
|
+
/** Where failed jobs go instead of `jobs.failed`; `null` keeps none (a test registry: failures throw). */
|
|
825
|
+
failed?: FailedJobs | null;
|
|
631
826
|
now?: () => Date;
|
|
632
827
|
stubs?: Map<Fn, (ctx: never, input: never) => Promise<unknown>>;
|
|
633
828
|
record?: (envelope: Envelope) => void;
|
|
@@ -647,14 +842,18 @@ export interface RegisteredCron extends Registered {
|
|
|
647
842
|
cron: Cron;
|
|
648
843
|
}
|
|
649
844
|
|
|
650
|
-
/**
|
|
845
|
+
/**
|
|
846
|
+
* `registry.jobs`: what you call from a queue consumer, a timer, a script or a staff route. Outside your functions, as
|
|
847
|
+
* Laravel's queue commands are: guard the route yourself. A job a user wants again is a mutation that emits again.
|
|
848
|
+
*/
|
|
651
849
|
export interface Jobs {
|
|
652
850
|
/**
|
|
653
|
-
* Run one
|
|
851
|
+
* Run one job from a queue message (consumers): `await registry.jobs.run(message.body, { attempt: message.attempts })`.
|
|
654
852
|
* Resolves `"done"`, `"failed"` or `"discarded"` (acknowledge all three); throws when a retry might help and the
|
|
655
|
-
* job has tries left. A `failed` job
|
|
853
|
+
* job has tries left. A `failed` job is kept by `jobs.failed`, if you have one. On an adapter that only sends
|
|
854
|
+
* signals, the job runs from its row, which records how it went, and `run` resolves `"done"`.
|
|
656
855
|
*/
|
|
657
|
-
run(job: Job, options?:
|
|
856
|
+
run(job: Job, options?: { attempt?: number }): Promise<JobOutcome>;
|
|
658
857
|
/**
|
|
659
858
|
* Run the crons due in `at`'s minute (default now), from a timer or your platform's cron trigger:
|
|
660
859
|
* `await registry.jobs.tick(new Date(controller.scheduledTime))`. Resolves the ids of the crons that were due. Each run
|
|
@@ -662,16 +861,38 @@ export interface Jobs {
|
|
|
662
861
|
*/
|
|
663
862
|
tick(at?: Date): Promise<string[]>;
|
|
664
863
|
/**
|
|
665
|
-
* Run what your adapter has due, from a cron or an interval: `await registry.jobs.sweep()`. With `databaseQueue(
|
|
666
|
-
* retries whose backoff is over and jobs whose runner crashed. Adapters that keep nothing
|
|
667
|
-
* (pg-boss), resolve zeros.
|
|
864
|
+
* Run what your adapter has due, from a cron or an interval: `await registry.jobs.sweep()`. With `databaseQueue()`, that's
|
|
865
|
+
* retries whose backoff is over and jobs whose runner crashed. Adapters that keep nothing resolve zeros.
|
|
668
866
|
*/
|
|
669
|
-
sweep(options?:
|
|
867
|
+
sweep(options?: SweepOptions): Promise<SweepResult>;
|
|
868
|
+
/**
|
|
869
|
+
* For a host that runs jobs in this process, like `work()`: requests leave their jobs due now to it (in its `queues`,
|
|
870
|
+
* or all) and answer at once, and `wake(count)` tells it how many just committed, so it claims them right away rather
|
|
871
|
+
* than at its next poll. Returns the detach, for its stop. Throws on a queue `jobs.queues` doesn't declare.
|
|
872
|
+
*/
|
|
873
|
+
attach(worker: LocalWorker): () => void;
|
|
670
874
|
/**
|
|
671
875
|
* Put a failed job back in line with a fresh set of attempts. `false` when no failed job has that id (it's pending,
|
|
672
876
|
* running, done, or never existed). Throws when your adapter keeps no jobs, like `inProcess()`.
|
|
673
877
|
*/
|
|
674
878
|
retry(id: string): Promise<boolean>;
|
|
879
|
+
/**
|
|
880
|
+
* Failed jobs, newest first, a page at a time: `await registry.jobs.failed({ source: "emails.send", scope: { organizationId } })`.
|
|
881
|
+
* Then `retry` or `forget` each. Pass your stored scope's type to check the filter: `failed<Stored>({ scope })`. Throws
|
|
882
|
+
* when your adapter keeps none (a queue alone without `jobs.failed`).
|
|
883
|
+
*/
|
|
884
|
+
failed<Stored = Record<string, unknown>>(options?: FailedOptions<Stored>): Promise<FailedPage<Stored>>;
|
|
885
|
+
/**
|
|
886
|
+
* Delete a failed job, like Laravel's `queue:forget`. `false` when no failed job has that id: a waiting or running one
|
|
887
|
+
* is never touched. Throws when your adapter keeps no jobs.
|
|
888
|
+
*/
|
|
889
|
+
forget(id: string): Promise<boolean>;
|
|
890
|
+
/**
|
|
891
|
+
* Run a cron now, outside its schedule, through your jobs adapter like a tick: `await registry.jobs.invoke(orders.sendDigest)`,
|
|
892
|
+
* or its id from a script. `at`: the minute the run is for (`ctx.cron.scheduledAt`), default now, to redo a missed one.
|
|
893
|
+
* Its own job id, so the real tick for that minute still runs. Resolves the id; throws when the hand-off fails.
|
|
894
|
+
*/
|
|
895
|
+
invoke(cron: Cron | string, options?: { at?: Date }): Promise<string>;
|
|
675
896
|
}
|
|
676
897
|
|
|
677
898
|
/** What `createRegistry` returns. `Cf` is your config's type, so tests can override parts of it. */
|
|
@@ -692,7 +913,6 @@ export interface Registry<S extends object = object, Mods extends Record<string,
|
|
|
692
913
|
/** @internal used by `logic-sdk/testing` */
|
|
693
914
|
readonly [INTERNAL]: {
|
|
694
915
|
options: RegistryOptions<Mods>;
|
|
695
|
-
config: Frozen<Cf>;
|
|
696
916
|
derive(more: Overrides): Registry<S, Mods, Cf>;
|
|
697
917
|
/** An event as `emit` would make it, for `t.jobs.perform`. */
|
|
698
918
|
envelope(event: EventDef, payload: unknown, scope: S): Envelope;
|