logic-sdk 0.7.0 → 0.7.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/README.md +9 -9
- package/dist/adapters/cloudflare-queue.d.ts +14 -0
- package/dist/adapters/cloudflare-queue.js +48 -0
- package/dist/adapters/drizzle-pg.d.ts +250 -0
- package/dist/adapters/drizzle-pg.js +69 -0
- package/dist/adapters/drizzle-sqlite.d.ts +257 -0
- package/dist/adapters/drizzle-sqlite.js +196 -0
- package/dist/adapters/pg-boss.d.ts +45 -0
- package/dist/adapters/pg-boss.js +180 -0
- package/dist/adapters/postgres.d.ts +32 -0
- package/dist/adapters/postgres.js +105 -0
- package/dist/adapters/sqlite.d.ts +13 -0
- package/dist/adapters/sqlite.js +88 -0
- package/dist/{stored-jobs.d.ts → adapters/stored-jobs.d.ts} +27 -10
- package/dist/{stored-jobs.js → adapters/stored-jobs.js} +106 -20
- 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 +17 -0
- package/dist/core/jobs.d.ts +57 -0
- package/dist/{jobs.js → core/jobs.js} +161 -42
- package/dist/core/registry.d.ts +120 -0
- package/dist/core/registry.js +503 -0
- package/dist/core/testing.d.ts +75 -0
- package/dist/core/testing.js +230 -0
- package/dist/{types.d.ts → core/types.d.ts} +224 -49
- 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 +42 -0
- package/dist/hosts/cloudflare.js +39 -0
- package/dist/hosts/node.d.ts +26 -0
- package/dist/hosts/node.js +76 -0
- package/dist/hosts/pg-boss.d.ts +22 -0
- package/dist/hosts/pg-boss.js +55 -0
- package/dist/index.d.ts +5 -124
- package/dist/index.js +4 -495
- 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/pg-boss.d.ts +2 -55
- package/dist/pg-boss.js +2 -211
- 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 -67
- package/dist/testing.js +1 -214
- package/package.json +2 -2
- package/skills/logic-sdk/SKILL.md +7 -6
- package/src/adapters/cloudflare-queue.ts +56 -0
- package/src/adapters/drizzle-pg.ts +108 -0
- package/src/adapters/drizzle-sqlite.ts +234 -0
- package/src/adapters/pg-boss.ts +195 -0
- package/src/adapters/postgres.ts +112 -0
- package/src/adapters/sqlite.ts +96 -0
- package/src/{stored-jobs.ts → adapters/stored-jobs.ts} +133 -30
- package/src/ai.ts +1 -62
- package/src/cloudflare.ts +2 -107
- package/src/core/in-process.ts +18 -0
- package/src/{jobs.ts → core/jobs.ts} +166 -43
- package/src/core/registry.ts +671 -0
- package/src/core/testing.ts +307 -0
- package/src/{types.ts → core/types.ts} +219 -46
- 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 +54 -0
- package/src/hosts/node.ts +99 -0
- package/src/hosts/pg-boss.ts +71 -0
- package/src/index.ts +17 -655
- 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/pg-boss.ts +2 -242
- package/src/postgres.ts +2 -90
- package/src/sqlite.ts +2 -74
- package/src/testing.ts +1 -288
- package/dist/jobs.d.ts +0 -44
- /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
|
@@ -192,7 +192,7 @@ export interface CronJob {
|
|
|
192
192
|
/** When it may run (ISO), like a delivery's. Absent for crons: they run when ticked. */
|
|
193
193
|
runAt?: string;
|
|
194
194
|
}
|
|
195
|
-
/** A function `ctx.schedule` asked to run later: `id` is `<function id>@<uuid
|
|
195
|
+
/** A function `ctx.schedule` asked to run later: `id` is `<function id>@<uuid>`. */
|
|
196
196
|
export interface CallJob {
|
|
197
197
|
kind: "call";
|
|
198
198
|
id: string;
|
|
@@ -208,8 +208,8 @@ export interface CallJob {
|
|
|
208
208
|
* function that schedules itself with no delay stops like a listener loop. A delayed one starts a new chain (0).
|
|
209
209
|
*/
|
|
210
210
|
depth: number;
|
|
211
|
-
/**
|
|
212
|
-
replace?:
|
|
211
|
+
/** `true`: a newer call to `fn` with the same input replaces this one while it waits (the function and its input are the identity). */
|
|
212
|
+
replace?: true;
|
|
213
213
|
/** When it may run (ISO). */
|
|
214
214
|
runAt?: string;
|
|
215
215
|
}
|
|
@@ -218,18 +218,26 @@ export interface CallJob {
|
|
|
218
218
|
* run, or a call `ctx.schedule` asked for.
|
|
219
219
|
*/
|
|
220
220
|
export type Job = Delivery | CronJob | CallJob;
|
|
221
|
-
/**
|
|
221
|
+
/** Work on a schedule, as `scope`: a handler, like a listener's, run when the clock says. Export it from a module. */
|
|
222
222
|
export interface Cron<C extends Config = any> extends FromInit<C> {
|
|
223
223
|
readonly [BRAND]: "cron";
|
|
224
224
|
readonly kind: "cron";
|
|
225
225
|
readonly id?: string;
|
|
226
226
|
readonly schedule: string;
|
|
227
227
|
readonly timezone?: string;
|
|
228
|
-
/** The function it runs, read when the registry is built, like a listener's `event`. */
|
|
229
|
-
readonly run: () => Fn;
|
|
230
|
-
readonly input?: unknown;
|
|
231
228
|
readonly scope: object;
|
|
232
229
|
readonly attempts?: number;
|
|
230
|
+
readonly meta?: Readonly<Record<string, unknown>>;
|
|
231
|
+
readonly handler: (ctx: never) => Promise<unknown>;
|
|
232
|
+
}
|
|
233
|
+
/** The run a cron's handler is doing. No scope here: `ctx.scope` is the one it runs as. */
|
|
234
|
+
export interface CronRun {
|
|
235
|
+
/** The cron's id: `tasks.wakeDue`. */
|
|
236
|
+
id: string;
|
|
237
|
+
/** The minute this run is for (ISO), even when it runs late or again: which day a digest covers. */
|
|
238
|
+
scheduledAt: string;
|
|
239
|
+
/** 1 on the first try. */
|
|
240
|
+
attempt: number;
|
|
233
241
|
}
|
|
234
242
|
/**
|
|
235
243
|
* What running a job resolved to. `failed`: out of attempts (code this process doesn't have uses them too), a rule said
|
|
@@ -238,6 +246,17 @@ export interface Cron<C extends Config = any> extends FromInit<C> {
|
|
|
238
246
|
* `discarded`: `jobs.scope.load` returned null (who it ran as is gone). Acknowledge it too; it's been reported.
|
|
239
247
|
*/
|
|
240
248
|
export type JobOutcome = "done" | "failed" | "discarded";
|
|
249
|
+
/**
|
|
250
|
+
* What `ctx.emit` queued: the `id`, `name` and `occurredAt` its listeners see as `ctx.event`. `payload` is a copy of what
|
|
251
|
+
* you passed; listeners get it parsed by the event's schema.
|
|
252
|
+
*/
|
|
253
|
+
export interface EmittedEvent<I = unknown> {
|
|
254
|
+
readonly id: string;
|
|
255
|
+
readonly name: string;
|
|
256
|
+
readonly payload: I;
|
|
257
|
+
/** ISO. */
|
|
258
|
+
readonly occurredAt: string;
|
|
259
|
+
}
|
|
241
260
|
/** The event a listener is handling. No scope here: `ctx.scope` is the one it runs as. */
|
|
242
261
|
export interface ListenerEvent<O = unknown> {
|
|
243
262
|
id: string;
|
|
@@ -267,7 +286,7 @@ export interface Listener<O = unknown, C extends Config = any> extends FromInit<
|
|
|
267
286
|
export interface RunOptions<S = object> {
|
|
268
287
|
/**
|
|
269
288
|
* The whole scope for this call and everything it runs, never merged: `{ scope: { ...ctx.scope, locale: "es" } }`
|
|
270
|
-
* to render in another language, `{ scope:
|
|
289
|
+
* to render in another language, `{ scope: { kind: "system", job: "reports.weekly" } }` to act as someone else on purpose. The
|
|
271
290
|
* callee's policy sees it. Whole, so it's one of your scope's variants: a patch could build one that isn't.
|
|
272
291
|
*/
|
|
273
292
|
scope?: S;
|
|
@@ -316,19 +335,24 @@ export interface MutationCtx<C extends Config, S = ScopeOf<C>> extends BaseCtx<C
|
|
|
316
335
|
* `await ctx.run(availability.hold, { propertyId })`. If it fails, the whole transaction rolls back.
|
|
317
336
|
*/
|
|
318
337
|
run: Run<"query" | "mutation", ScopeOf<C>>;
|
|
319
|
-
/**
|
|
320
|
-
|
|
338
|
+
/**
|
|
339
|
+
* Queue an event. Delivered to listeners only after the transaction commits. Returns what it queued: the id, name and
|
|
340
|
+
* time its listeners see as `ctx.event`, so a row you write in this transaction can carry `event.id` and a listener
|
|
341
|
+
* finds it by `ctx.event.id`.
|
|
342
|
+
*/
|
|
343
|
+
emit<I>(event: EventDef<I, unknown>, payload: I): EmittedEvent<I>;
|
|
321
344
|
/**
|
|
322
345
|
* Run a mutation or action once, later, as this scope: `ctx.schedule(guests.remind, { bookingId }, { at })` or
|
|
323
|
-
* `{ delay: 3600 }` (seconds). Committed with this transaction, so a rollback never leaves one behind.
|
|
324
|
-
*
|
|
325
|
-
* `attempts`: tries in total, default 1; give an action that calls an API a few.
|
|
326
|
-
* with
|
|
346
|
+
* `{ delay: 3600 }` (seconds). Committed with this transaction, so a rollback never leaves one behind. Nothing to
|
|
347
|
+
* keep: there's no cancel. It runs whatever happens meanwhile, so it checks that its reason still holds ("still
|
|
348
|
+
* confirmed?") against your own row. `attempts`: tries in total, default 1; give an action that calls an API a few.
|
|
349
|
+
* `replace: true`: a newer call to this function with the same input replaces the one still waiting (the dates of
|
|
350
|
+
* `{ bookingId }` changed), like Oban's `replace`. Keep the input to ids, so it names one thing.
|
|
327
351
|
*/
|
|
328
|
-
schedule<F extends Fn<"mutation" | "action">>(fn: F, input: FnIn<F>, options: ScheduleOptions):
|
|
352
|
+
schedule<F extends Fn<"mutation" | "action">>(fn: F, input: FnIn<F>, options: ScheduleOptions<ScopeOf<C>>): void;
|
|
329
353
|
}
|
|
330
354
|
/** When a scheduled call runs (`at` or `delay` in seconds), and how many tries it gets. */
|
|
331
|
-
export type ScheduleOptions = ({
|
|
355
|
+
export type ScheduleOptions<S = object> = ({
|
|
332
356
|
at: Date;
|
|
333
357
|
delay?: never;
|
|
334
358
|
} | {
|
|
@@ -336,8 +360,13 @@ export type ScheduleOptions = ({
|
|
|
336
360
|
at?: never;
|
|
337
361
|
}) & {
|
|
338
362
|
attempts?: number;
|
|
339
|
-
/**
|
|
340
|
-
replace?:
|
|
363
|
+
/** `true`: the call to this function waiting with the same input (not started yet) is replaced by this one. */
|
|
364
|
+
replace?: true;
|
|
365
|
+
/**
|
|
366
|
+
* Who it runs as, whole, never merged, as `ctx.run` takes it: `{ scope: { kind: "system", job: "tasks.wake", organization } }`.
|
|
367
|
+
* Default: this mutation's scope. The function's policy checks it when it runs.
|
|
368
|
+
*/
|
|
369
|
+
scope?: S;
|
|
341
370
|
};
|
|
342
371
|
export interface ActionCtx<C extends Config, S = ScopeOf<C>> extends BaseCtx<C, S> {
|
|
343
372
|
services: ServicesOf<C>;
|
|
@@ -349,6 +378,10 @@ export interface ActionCtx<C extends Config, S = ScopeOf<C>> extends BaseCtx<C,
|
|
|
349
378
|
export interface ListenerCtx<C extends Config, O = unknown> extends ActionCtx<C> {
|
|
350
379
|
event: ListenerEvent<O>;
|
|
351
380
|
}
|
|
381
|
+
/** A cron's handler's ctx: an action's, as the cron's own scope, plus the run. */
|
|
382
|
+
export interface CronCtx<C extends Config, S = ScopeOf<C>> extends ActionCtx<C, S> {
|
|
383
|
+
cron: CronRun;
|
|
384
|
+
}
|
|
352
385
|
/**
|
|
353
386
|
* Who may call a function at all: signed in, admin, staff. Sync, reads only the scope, returns exactly `true` to allow.
|
|
354
387
|
* Write it as a type guard (`(s): s is Admin => ...`) to narrow `ctx.scope` in `handler`.
|
|
@@ -401,21 +434,24 @@ export interface DatabaseAdapter<Db = unknown, Tx = Db> {
|
|
|
401
434
|
nest?(tx: Tx): DatabaseAdapter<Tx, Tx>;
|
|
402
435
|
/** Did this transaction lose a race the database says to retry (deadlock, serialization failure)? */
|
|
403
436
|
retryable?(error: unknown): boolean;
|
|
404
|
-
/**
|
|
405
|
-
* Tries in total for a top-level mutation whose transaction is `retryable`, each in a fresh one, like Laravel's
|
|
406
|
-
* `DB::transaction(fn, attempts)`. Default 3; 1 turns it off. A job's own tries are its `attempts`.
|
|
407
|
-
*/
|
|
408
|
-
transactionAttempts?: number;
|
|
409
|
-
/**
|
|
410
|
-
* One SQL statement (`$1` parameters on Postgres, `?` on SQLite), on the database or on a transaction of it. Resolves the rows it
|
|
411
|
-
* returns. `databaseQueue` and `outbox` run their table through it, so any ORM that can run SQL has them.
|
|
412
|
-
*/
|
|
413
|
-
query?(client: Db | Tx, text: string, params: unknown[]): Promise<Record<string, unknown>[]>;
|
|
414
437
|
}
|
|
415
|
-
/** A database adapter
|
|
438
|
+
/** A database adapter that runs SQL: what `databaseQueue()`, `outbox(queue)` and `pgBoss(boss)` need. */
|
|
416
439
|
export interface SqlDatabase<Db = any, Tx = any> extends DatabaseAdapter<Db, Tx> {
|
|
440
|
+
/**
|
|
441
|
+
* One SQL statement (`$1` parameters on Postgres, `?` on SQLite), on the database or on a transaction of it. Resolves
|
|
442
|
+
* the rows it returns. The jobs table runs through it, so any ORM that can run SQL has it.
|
|
443
|
+
*/
|
|
417
444
|
query(client: Db | Tx, text: string, params: unknown[]): Promise<Record<string, unknown>[]>;
|
|
418
445
|
}
|
|
446
|
+
/**
|
|
447
|
+
* `createRegistry({ database })`: the adapter, and how the core uses it. `transactionAttempts`: tries in total for a
|
|
448
|
+
* top-level mutation whose transaction lost a race (`retryable`), each in a fresh one, like Laravel's
|
|
449
|
+
* `DB::transaction(fn, attempts)`. Default 3; 1 turns it off. A job's own tries are its `attempts`.
|
|
450
|
+
*/
|
|
451
|
+
export interface DatabaseOptions<A extends DatabaseAdapter<any, any> = DatabaseAdapter<unknown, unknown>> {
|
|
452
|
+
adapter: A;
|
|
453
|
+
transactionAttempts?: number;
|
|
454
|
+
}
|
|
419
455
|
/** Runs one job: resolves when it's done or failed for good (acknowledge it), throws when a retry might help. */
|
|
420
456
|
export type RunJob = (job: Job, options?: RunJobOptions) => Promise<JobOutcome>;
|
|
421
457
|
export interface RunJobOptions {
|
|
@@ -441,6 +477,18 @@ export interface Runner {
|
|
|
441
477
|
report(error: unknown, job: Job, attempt?: number): void;
|
|
442
478
|
}
|
|
443
479
|
export interface JobsAdapter {
|
|
480
|
+
/** How it's set up, for messages: `"databaseQueue()"`. The core names capabilities, the adapter itself. */
|
|
481
|
+
name?: string;
|
|
482
|
+
/**
|
|
483
|
+
* It can run a job again after a failed try: a queue redelivers it, a table backs off. Without it (`inProcess()`),
|
|
484
|
+
* `createRegistry` refuses `attempts` above 1 rather than ignore them, as it refuses a delay it can't hold.
|
|
485
|
+
*/
|
|
486
|
+
retries?: true;
|
|
487
|
+
/**
|
|
488
|
+
* It writes jobs with SQL in the mutation's transaction, through the registry's database: `createRegistry` refuses a
|
|
489
|
+
* registry whose `database.adapter` doesn't run SQL (`query`), and `connect` gets it as `setup.sql`.
|
|
490
|
+
*/
|
|
491
|
+
sql?: true;
|
|
444
492
|
/**
|
|
445
493
|
* It can hold a job until its `runAt` (a listener's `delay`, `ctx.schedule`): `true`, or for at most `max` seconds.
|
|
446
494
|
* Without it (`inProcess()`), `createRegistry` refuses delayed listeners and `ctx.schedule` throws, rather than run
|
|
@@ -450,16 +498,16 @@ export interface JobsAdapter {
|
|
|
450
498
|
max: number;
|
|
451
499
|
};
|
|
452
500
|
/**
|
|
453
|
-
* Can replace a call waiting to run: `enqueue` gets a call with `replace`, and a pending call
|
|
454
|
-
*
|
|
455
|
-
* Without it, `ctx.schedule` with `replace` is refused.
|
|
501
|
+
* Can replace a call waiting to run: `enqueue` gets a call with `replace`, and a pending call with the same key
|
|
502
|
+
* (`storedReplaceKey`) that hasn't started is replaced by it. `databaseQueue` and `outbox` can; a queue that already
|
|
503
|
+
* has the message can't. Without it, `ctx.schedule` with `replace` is refused.
|
|
456
504
|
*/
|
|
457
505
|
replaces?: true;
|
|
458
|
-
/**
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
506
|
+
/**
|
|
507
|
+
* At `createRegistry`, once: take what the adapter needs from the registry (its database, for an adapter that writes
|
|
508
|
+
* jobs in the mutation's transaction), and refuse a setup it can't honour.
|
|
509
|
+
*/
|
|
510
|
+
connect?(setup: AdapterSetup): void;
|
|
463
511
|
/** Inside the transaction, before commit. In-process: nothing. `databaseQueue` and `outbox`: insert one row per job with `tx`. */
|
|
464
512
|
enqueue?(tx: unknown, jobs: Job[]): Promise<void>;
|
|
465
513
|
/** After commit: run them, or hand them to a queue. A throw is reported to `onError`. */
|
|
@@ -470,6 +518,13 @@ export interface JobsAdapter {
|
|
|
470
518
|
}): Promise<SweepResult>;
|
|
471
519
|
/** Put a failed job back in line. `false` when there's no failed job with that id. Only adapters that store them. */
|
|
472
520
|
retry?(id: string): Promise<boolean>;
|
|
521
|
+
/** A page of failed jobs, newest first. Only adapters that store them and can filter them. */
|
|
522
|
+
failed?(options: FailedQuery): Promise<{
|
|
523
|
+
rows: FailedRow[];
|
|
524
|
+
cursor: string | null;
|
|
525
|
+
}>;
|
|
526
|
+
/** Delete one failed job. `false` when there's no failed job with that id: a waiting or running one is never touched. */
|
|
527
|
+
forget?(id: string): Promise<boolean>;
|
|
473
528
|
/**
|
|
474
529
|
* Keep a job that failed for good after it left the adapter: `registry.jobs.run` calls it from a queue consumer, so
|
|
475
530
|
* `outbox` stores the failure Cloudflare would otherwise drop. Not called for jobs the adapter ran itself.
|
|
@@ -489,6 +544,93 @@ export interface FailureInfo {
|
|
|
489
544
|
/** `retry`: tries are left and the adapter may run it again. `failed` and `discarded`: it's over. */
|
|
490
545
|
outcome?: "retry" | "failed" | "discarded";
|
|
491
546
|
}
|
|
547
|
+
/** What `createRegistry` gives an adapter's `connect`. */
|
|
548
|
+
export interface AdapterSetup {
|
|
549
|
+
listeners: readonly RegisteredListener[];
|
|
550
|
+
crons: readonly RegisteredCron[];
|
|
551
|
+
/** The registry's `database.adapter`. */
|
|
552
|
+
database: DatabaseAdapter<unknown, unknown> | undefined;
|
|
553
|
+
/** The same, when it runs SQL: always set for an adapter that declares `sql: true`. */
|
|
554
|
+
sql: SqlDatabase | undefined;
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* A store for failed jobs, apart from the queue that runs them, like Laravel's `failed` driver. `failedJobs()`
|
|
558
|
+
* keeps them in the jobs table. For adapters that keep their own, it isn't needed.
|
|
559
|
+
*/
|
|
560
|
+
export interface FailedJobs {
|
|
561
|
+
/** For messages: `"failedJobs()"`. */
|
|
562
|
+
name?: string;
|
|
563
|
+
/** Keeps failed jobs with SQL, through the registry's database (`setup.sql`): see `JobsAdapter.sql`. */
|
|
564
|
+
sql?: true;
|
|
565
|
+
/** At `createRegistry`, once: take the registry's database. */
|
|
566
|
+
connect?(setup: AdapterSetup): void;
|
|
567
|
+
/** Keep a job that failed for good. */
|
|
568
|
+
fail(job: Job, error: unknown): Promise<void>;
|
|
569
|
+
/** A page of failed jobs, newest first. */
|
|
570
|
+
failed(query: FailedQuery): Promise<{
|
|
571
|
+
rows: FailedRow[];
|
|
572
|
+
cursor: string | null;
|
|
573
|
+
}>;
|
|
574
|
+
/** Remove a failed job and return it, for `retry` to hand to the queue again. Undefined when there's none with that id. */
|
|
575
|
+
take(id: string): Promise<Job | undefined>;
|
|
576
|
+
/** Delete a failed job. `false` when there's none with that id. */
|
|
577
|
+
forget(id: string): Promise<boolean>;
|
|
578
|
+
}
|
|
579
|
+
/** One field of a stored scope a `failed({ scope })` filter matches exactly: `["organization", "id"]` and `"org_1"`. */
|
|
580
|
+
export interface ScopeMatch {
|
|
581
|
+
path: string[];
|
|
582
|
+
value: string | number | boolean | null;
|
|
583
|
+
}
|
|
584
|
+
/** What `registry.jobs.failed` asks an adapter for. The core checks and flattens the filter first. */
|
|
585
|
+
export interface FailedQuery {
|
|
586
|
+
/** The listener, cron or function id. */
|
|
587
|
+
source?: string;
|
|
588
|
+
/** Every one must equal the stored scope's field at `path` (the job's `scope`, or its envelope's). */
|
|
589
|
+
scope?: ScopeMatch[];
|
|
590
|
+
limit: number;
|
|
591
|
+
/** From the last page: where to go on. */
|
|
592
|
+
cursor?: string;
|
|
593
|
+
}
|
|
594
|
+
/** A failed job as an adapter keeps it. */
|
|
595
|
+
export interface FailedRow {
|
|
596
|
+
id: string;
|
|
597
|
+
job: Job;
|
|
598
|
+
error: string;
|
|
599
|
+
/** ISO. */
|
|
600
|
+
failedAt: string;
|
|
601
|
+
}
|
|
602
|
+
/** A failed job, as `registry.jobs.failed` lists it: pass its `id` to `retry` or `forget`. */
|
|
603
|
+
export interface FailedJob<Stored = Record<string, unknown>> {
|
|
604
|
+
id: string;
|
|
605
|
+
kind: Job["kind"];
|
|
606
|
+
/** The listener, cron or function that failed, as in `FailureInfo`. */
|
|
607
|
+
source: string;
|
|
608
|
+
/** What `jobs.scope.store` saved with it. Undefined for a cron run: it has none. */
|
|
609
|
+
scope: Stored | undefined;
|
|
610
|
+
/** The last error, with its stack. */
|
|
611
|
+
error: string;
|
|
612
|
+
/** ISO. */
|
|
613
|
+
failedAt: string;
|
|
614
|
+
job: Job;
|
|
615
|
+
}
|
|
616
|
+
export interface FailedOptions<Stored = Record<string, unknown>> {
|
|
617
|
+
/** Only this listener's, cron's or function's: `"emails.send"`. */
|
|
618
|
+
source?: string;
|
|
619
|
+
/**
|
|
620
|
+
* Only jobs whose stored scope has these values (what `jobs.scope.store` saved): `{ organizationId: "org_1" }`,
|
|
621
|
+
* nested fields too. Never a cron run: it stores none. Values are strings, numbers, booleans or null.
|
|
622
|
+
*/
|
|
623
|
+
scope?: DeepPartial<Stored>;
|
|
624
|
+
/** 1 to 1000. Default 100. */
|
|
625
|
+
limit?: number;
|
|
626
|
+
/** The `cursor` of the page before. */
|
|
627
|
+
cursor?: string;
|
|
628
|
+
}
|
|
629
|
+
export interface FailedPage<Stored = Record<string, unknown>> {
|
|
630
|
+
jobs: FailedJob<Stored>[];
|
|
631
|
+
/** Pass it back for the next page; null on the last one. */
|
|
632
|
+
cursor: string | null;
|
|
633
|
+
}
|
|
492
634
|
/**
|
|
493
635
|
* Who a job runs as, at rest. `store` picks what's saved with the job (ids, not whole objects); `load` rebuilds the
|
|
494
636
|
* scope when it runs, so policies see today's roles. `load` returning null discards the job (reported to `onError`),
|
|
@@ -511,6 +653,11 @@ export interface InProcess extends JobsAdapter {
|
|
|
511
653
|
* checked at startup: requiring it here would report a missing one as a wrong `store`.
|
|
512
654
|
*/
|
|
513
655
|
export type JobsOptions<S = object, T = any> = {
|
|
656
|
+
/**
|
|
657
|
+
* Where failed jobs are kept when your adapter can't keep them itself (a queue on its own), like Laravel's `failed`
|
|
658
|
+
* driver: `failed: failedJobs()`. `registry.jobs.failed`, `retry` and `forget` then work on it.
|
|
659
|
+
*/
|
|
660
|
+
failed?: FailedJobs;
|
|
514
661
|
/**
|
|
515
662
|
* Max chain of listener → mutation → event → listener… before queueing stops, reported. Calls `ctx.schedule` runs
|
|
516
663
|
* right away count too, so a function that schedules itself with no delay stops the same way. Default 8 (real chains
|
|
@@ -518,20 +665,25 @@ export type JobsOptions<S = object, T = any> = {
|
|
|
518
665
|
*/
|
|
519
666
|
maxEventDepth?: number;
|
|
520
667
|
} & ({
|
|
521
|
-
/** `inProcess()` (default), or a durable one like `databaseQueue(
|
|
668
|
+
/** `inProcess()` (default), or a durable one like `databaseQueue()`. Its own options go in its factory. */
|
|
522
669
|
adapter?: InProcess;
|
|
523
670
|
/** Applied by the registry in every adapter, `inProcess()` and tests included. */
|
|
524
671
|
scope?: JobScope<S, T>;
|
|
525
672
|
} | {
|
|
526
|
-
/** An adapter that keeps jobs to run later, like `databaseQueue(
|
|
673
|
+
/** An adapter that keeps jobs to run later, like `databaseQueue()`. */
|
|
527
674
|
adapter?: JobsAdapter;
|
|
528
675
|
/** Required: such a job runs as the scope `load` rebuilds, with today's roles. */
|
|
529
676
|
scope: JobScope<S, T>;
|
|
530
677
|
});
|
|
531
678
|
export interface Call<M = unknown> {
|
|
532
679
|
id: string;
|
|
533
|
-
kind: Kind | "listener";
|
|
534
|
-
/**
|
|
680
|
+
kind: Kind | "listener" | "cron";
|
|
681
|
+
/** 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). */
|
|
682
|
+
job?: {
|
|
683
|
+
id: string;
|
|
684
|
+
attempt: number;
|
|
685
|
+
};
|
|
686
|
+
/** The function's, listener's or cron's `meta` (undefined when it has none). */
|
|
535
687
|
meta: M | undefined;
|
|
536
688
|
/** Raw input as received (before validation). */
|
|
537
689
|
input: unknown;
|
|
@@ -541,11 +693,12 @@ export interface Call<M = unknown> {
|
|
|
541
693
|
export interface RegistryOptions<Mods extends Record<string, object>> {
|
|
542
694
|
/** One entry per module: `{ bookings, availability }` with `import * as bookings from "./bookings"`. */
|
|
543
695
|
modules: Mods;
|
|
544
|
-
database
|
|
696
|
+
/** `database: { adapter: drizzleAdapter(db) }`, with `transactionAttempts` when 3 isn't right. */
|
|
697
|
+
database?: DatabaseOptions;
|
|
545
698
|
services?: object;
|
|
546
699
|
/** Settings every function reads as `ctx.config`. Plain data: it's copied and frozen. */
|
|
547
700
|
config?: object;
|
|
548
|
-
/** Background work: `jobs: { adapter: databaseQueue(
|
|
701
|
+
/** Background work: `jobs: { adapter: databaseQueue(), scope: { store, load } }`. Default: in-process, scope as-is (nothing waits). */
|
|
549
702
|
jobs?: JobsOptions;
|
|
550
703
|
/** Wraps every call, including ones that fail validation or authorization: logging, tracing, audit. */
|
|
551
704
|
wrap?: (call: Call, next: () => Promise<unknown>) => Promise<unknown>;
|
|
@@ -582,6 +735,8 @@ export interface Overrides {
|
|
|
582
735
|
/** Merged over the registry's config, section by section. */
|
|
583
736
|
config?: unknown;
|
|
584
737
|
adapter?: JobsAdapter;
|
|
738
|
+
/** Where failed jobs go instead of `jobs.failed`; `null` keeps none (a test registry: failures throw). */
|
|
739
|
+
failed?: FailedJobs | null;
|
|
585
740
|
now?: () => Date;
|
|
586
741
|
stubs?: Map<Fn, (ctx: never, input: never) => Promise<unknown>>;
|
|
587
742
|
record?: (envelope: Envelope) => void;
|
|
@@ -598,9 +753,11 @@ export interface RegisteredListener extends Registered {
|
|
|
598
753
|
}
|
|
599
754
|
export interface RegisteredCron extends Registered {
|
|
600
755
|
cron: Cron;
|
|
601
|
-
run: Fn;
|
|
602
756
|
}
|
|
603
|
-
/**
|
|
757
|
+
/**
|
|
758
|
+
* `registry.jobs`: what you call from a queue consumer, a timer, a script or a staff route. Outside your functions, as
|
|
759
|
+
* Laravel's queue commands are: guard the route yourself. A job a user wants again is a mutation that emits again.
|
|
760
|
+
*/
|
|
604
761
|
export interface Jobs {
|
|
605
762
|
/**
|
|
606
763
|
* Run one stored job (queue consumers): `await registry.jobs.run(message.body, { attempt: message.attempts })`.
|
|
@@ -615,7 +772,7 @@ export interface Jobs {
|
|
|
615
772
|
*/
|
|
616
773
|
tick(at?: Date): Promise<string[]>;
|
|
617
774
|
/**
|
|
618
|
-
* Run what your adapter has due, from a cron or an interval: `await registry.jobs.sweep()`. With `databaseQueue(
|
|
775
|
+
* Run what your adapter has due, from a cron or an interval: `await registry.jobs.sweep()`. With `databaseQueue()`, that's
|
|
619
776
|
* retries whose backoff is over and jobs whose runner crashed. Adapters that keep nothing, or maintain themselves
|
|
620
777
|
* (pg-boss), resolve zeros.
|
|
621
778
|
*/
|
|
@@ -627,6 +784,25 @@ export interface Jobs {
|
|
|
627
784
|
* running, done, or never existed). Throws when your adapter keeps no jobs, like `inProcess()`.
|
|
628
785
|
*/
|
|
629
786
|
retry(id: string): Promise<boolean>;
|
|
787
|
+
/**
|
|
788
|
+
* Failed jobs, newest first, a page at a time: `await registry.jobs.failed({ source: "emails.send", scope: { organizationId } })`.
|
|
789
|
+
* Then `retry` or `forget` each. Pass your stored scope's type to check the filter: `failed<Stored>({ scope })`. Throws
|
|
790
|
+
* when your adapter keeps none (a queue alone without `jobs.failed`) or can't list them: its message says where they are.
|
|
791
|
+
*/
|
|
792
|
+
failed<Stored = Record<string, unknown>>(options?: FailedOptions<Stored>): Promise<FailedPage<Stored>>;
|
|
793
|
+
/**
|
|
794
|
+
* Delete a failed job, like Laravel's `queue:forget`. `false` when no failed job has that id: a waiting or running one
|
|
795
|
+
* is never touched. Throws when your adapter keeps no jobs.
|
|
796
|
+
*/
|
|
797
|
+
forget(id: string): Promise<boolean>;
|
|
798
|
+
/**
|
|
799
|
+
* Run a cron now, outside its schedule, through your jobs adapter like a tick: `await registry.jobs.invoke(orders.sendDigest)`,
|
|
800
|
+
* or its id from a script. `at`: the minute the run is for (`ctx.cron.scheduledAt`), default now, to redo a missed one.
|
|
801
|
+
* Its own job id, so the real tick for that minute still runs. Resolves the id; throws when the hand-off fails.
|
|
802
|
+
*/
|
|
803
|
+
invoke(cron: Cron | string, options?: {
|
|
804
|
+
at?: Date;
|
|
805
|
+
}): Promise<string>;
|
|
630
806
|
}
|
|
631
807
|
/** What `createRegistry` returns. `Cf` is your config's type, so tests can override parts of it. */
|
|
632
808
|
export interface Registry<S extends object = object, Mods extends Record<string, object> = Record<string, object>, Cf = unknown> {
|
|
@@ -646,12 +822,11 @@ export interface Registry<S extends object = object, Mods extends Record<string,
|
|
|
646
822
|
/** @internal used by `logic-sdk/testing` */
|
|
647
823
|
readonly [INTERNAL]: {
|
|
648
824
|
options: RegistryOptions<Mods>;
|
|
649
|
-
config: Frozen<Cf>;
|
|
650
825
|
derive(more: Overrides): Registry<S, Mods, Cf>;
|
|
651
826
|
/** An event as `emit` would make it, for `t.jobs.perform`. */
|
|
652
827
|
envelope(event: EventDef, payload: unknown, scope: S): Envelope;
|
|
653
|
-
/** The cron runs due in a minute, for `t.jobs.tick
|
|
654
|
-
due(at: Date): CronJob[];
|
|
828
|
+
/** The cron runs due in a minute, for `t.jobs.tick`; with `only`, that cron's run, due or not (`t.jobs.perform`). */
|
|
829
|
+
due(at: Date, only?: string): CronJob[];
|
|
655
830
|
};
|
|
656
831
|
}
|
|
657
832
|
/** The registry's internals, for `logic-sdk/testing`. */
|