@orkestrel/queue 0.0.12 → 0.0.14
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 -15
- package/dist/src/core/index.cjs +46 -42
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +78 -52
- package/dist/src/core/index.d.ts +78 -52
- package/dist/src/core/index.js +47 -43
- package/dist/src/core/index.js.map +1 -1
- package/package.json +17 -18
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import { ContractShape } from '@orkestrel/contract';
|
|
2
|
-
import { DriverInterface } from '@orkestrel/database';
|
|
3
|
-
import { EmitterErrorHandler } from '@orkestrel/emitter';
|
|
4
|
-
import { EmitterHooks } from '@orkestrel/emitter';
|
|
5
|
-
import { EmitterInterface } from '@orkestrel/emitter';
|
|
6
|
-
import { Guard } from '@orkestrel/contract';
|
|
7
|
-
import { Infer } from '@orkestrel/contract';
|
|
8
|
-
import { TableInterface } from '@orkestrel/database';
|
|
1
|
+
import type { ContractShape } from '@orkestrel/contract';
|
|
2
|
+
import type { DriverInterface } from '@orkestrel/database';
|
|
3
|
+
import type { EmitterErrorHandler } from '@orkestrel/emitter';
|
|
4
|
+
import type { EmitterHooks } from '@orkestrel/emitter';
|
|
5
|
+
import type { EmitterInterface } from '@orkestrel/emitter';
|
|
6
|
+
import type { Guard } from '@orkestrel/contract';
|
|
7
|
+
import type { Infer } from '@orkestrel/contract';
|
|
8
|
+
import type { TableInterface } from '@orkestrel/database';
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Creates a {@link DatabaseQueueStore} over any {@link DriverInterface} — the durable,
|
|
12
|
-
* driver-pluggable backing for a queue's outstanding entries
|
|
12
|
+
* driver-pluggable backing for a queue's outstanding entries, across a memory, JSON, or
|
|
13
|
+
* SQLite driver.
|
|
13
14
|
*
|
|
14
15
|
* @remarks
|
|
15
16
|
* Builds a one-table database (`entries`, keyed by `id`) over the supplied driver, with
|
|
@@ -43,8 +44,9 @@ import { TableInterface } from '@orkestrel/database';
|
|
|
43
44
|
export declare function createDatabaseQueueStore<TInput extends ContractShape>(input: TInput, driver: DriverInterface): QueueStoreInterface<Infer<TInput>>;
|
|
44
45
|
|
|
45
46
|
/**
|
|
46
|
-
* Creates an in-memory {@link MemoryQueueStore} — the zero-plumbing
|
|
47
|
-
*
|
|
47
|
+
* Creates an in-memory {@link MemoryQueueStore} over a plain `Map` — the zero-plumbing
|
|
48
|
+
* {@link QueueStoreInterface} a queue takes when its outstanding entries need not outlive
|
|
49
|
+
* the process, and the twin of {@link DatabaseQueueStore}.
|
|
48
50
|
*
|
|
49
51
|
* @remarks
|
|
50
52
|
* The entries live in a process-lifetime `Map` and are gone when the process exits. The
|
|
@@ -75,7 +77,7 @@ export declare function createMemoryQueueStore<TInput extends ContractShape>(inp
|
|
|
75
77
|
* timeout / abort, all over the L1 `Abort` / `Timeout` primitives.
|
|
76
78
|
*
|
|
77
79
|
* @remarks
|
|
78
|
-
* Worker loops
|
|
80
|
+
* Worker loops park on a wake list when no entry is ready and are woken one-at-a-time
|
|
79
81
|
* by `enqueue` / `resume`, so an idle queue consumes no CPU (no polling). `enqueue`
|
|
80
82
|
* returns a promise that settles when the entry finally completes, fails (after its
|
|
81
83
|
* retries), or is rejected by an abort. A queue-level `abort` never retries; the
|
|
@@ -90,26 +92,32 @@ export declare function createMemoryQueueStore<TInput extends ContractShape>(inp
|
|
|
90
92
|
* `timeout` `0`.
|
|
91
93
|
* @returns A working {@link QueueInterface}
|
|
92
94
|
*
|
|
93
|
-
* @example
|
|
95
|
+
* @example Create a queue
|
|
94
96
|
* ```ts
|
|
95
97
|
* import { createQueue } from '@orkestrel/queue'
|
|
96
98
|
*
|
|
97
|
-
*
|
|
99
|
+
* // Ordered (concurrency defaults to 1): each entry runs to completion before the next.
|
|
100
|
+
* const queue = createQueue<Job, Output>({ handler: (job) => run(job) })
|
|
101
|
+
*
|
|
102
|
+
* const output = await queue.enqueue(job)
|
|
103
|
+
*
|
|
104
|
+
* // Bounded, retried, and time-boxed: four in flight, two extra attempts, 5s per attempt.
|
|
105
|
+
* const fetches = createQueue<string, number>({
|
|
98
106
|
* handler: async (url, { signal }) => (await fetch(url, { signal })).status,
|
|
99
107
|
* concurrency: 4,
|
|
100
108
|
* retries: 2,
|
|
101
109
|
* timeout: 5_000,
|
|
102
110
|
* })
|
|
103
111
|
*
|
|
104
|
-
* const status = await
|
|
112
|
+
* const status = await fetches.enqueue('https://example.com')
|
|
105
113
|
* ```
|
|
106
114
|
*/
|
|
107
115
|
export declare function createQueue<TInput, TResult>(options: QueueOptions<TInput, TResult>): QueueInterface<TInput, TResult>;
|
|
108
116
|
|
|
109
117
|
/**
|
|
110
|
-
* Represents
|
|
111
|
-
* queue's durable state
|
|
112
|
-
* `TableInterface`.
|
|
118
|
+
* Represents the opt-in durable store for a queue's outstanding entries, backed by one table
|
|
119
|
+
* of the `@orkestrel/database` layer — a queue's durable state is a table, so persistence
|
|
120
|
+
* reduces to keyed CRUD over a `TableInterface`.
|
|
113
121
|
*
|
|
114
122
|
* @remarks
|
|
115
123
|
* The store is driver-agnostic: it holds a single {@link TableInterface} whose
|
|
@@ -119,7 +127,7 @@ export declare function createQueue<TInput, TResult>(options: QueueOptions<TInpu
|
|
|
119
127
|
* empties the table. Reads are narrowed through the table's contract, so `load`
|
|
120
128
|
* returns typed {@link StoredEntry}`<TInput>[]` with no cast — the table
|
|
121
129
|
* is created over the `{ id; input; attempts }` column map, which `Infer`s to exactly
|
|
122
|
-
* that row. The store holds only
|
|
130
|
+
* that row. The store holds only outstanding work (completed entries are `remove`d), so
|
|
123
131
|
* `load` on startup yields precisely the entries to resume.
|
|
124
132
|
*
|
|
125
133
|
* @typeParam TInput - The work input each {@link StoredEntry} carries
|
|
@@ -155,7 +163,7 @@ export declare class DatabaseQueueStore<TInput> implements QueueStoreInterface<T
|
|
|
155
163
|
}
|
|
156
164
|
|
|
157
165
|
/**
|
|
158
|
-
* Determines whether a value is a valid queue concurrency.
|
|
166
|
+
* Determines whether a value is a valid queue concurrency — a positive safe integer.
|
|
159
167
|
*
|
|
160
168
|
* @param value - Value to inspect
|
|
161
169
|
* @returns True if the value is a positive safe integer; false otherwise
|
|
@@ -169,7 +177,8 @@ export declare class DatabaseQueueStore<TInput> implements QueueStoreInterface<T
|
|
|
169
177
|
export declare function isQueueConcurrency(value: unknown): value is number;
|
|
170
178
|
|
|
171
179
|
/**
|
|
172
|
-
* Determines whether an unknown value is a {@link QueueError}
|
|
180
|
+
* Determines whether an unknown value is a {@link QueueError}, staying total for a hostile
|
|
181
|
+
* value.
|
|
173
182
|
*
|
|
174
183
|
* @param value - The value to inspect
|
|
175
184
|
* @returns True if the value is a real `QueueError` instance; false otherwise, including for
|
|
@@ -187,7 +196,7 @@ export declare function isQueueConcurrency(value: unknown): value is number;
|
|
|
187
196
|
export declare function isQueueError(value: unknown): value is QueueError;
|
|
188
197
|
|
|
189
198
|
/**
|
|
190
|
-
* Determines whether a value is a valid queue retry count.
|
|
199
|
+
* Determines whether a value is a valid queue retry count — a nonnegative safe integer.
|
|
191
200
|
*
|
|
192
201
|
* @param value - Value to inspect
|
|
193
202
|
* @returns True if the value is a nonnegative safe integer; false otherwise
|
|
@@ -201,7 +210,8 @@ export declare function isQueueError(value: unknown): value is QueueError;
|
|
|
201
210
|
export declare function isQueueRetries(value: unknown): value is number;
|
|
202
211
|
|
|
203
212
|
/**
|
|
204
|
-
* Determines whether a value is a native abort signal usable by the queue
|
|
213
|
+
* Determines whether a value is a native abort signal usable by the queue, testing the
|
|
214
|
+
* native brand rather than the shape.
|
|
205
215
|
*
|
|
206
216
|
* @param value - Value to inspect
|
|
207
217
|
* @returns True if the value carries the native `AbortSignal` internal slot; false otherwise
|
|
@@ -215,7 +225,8 @@ export declare function isQueueRetries(value: unknown): value is number;
|
|
|
215
225
|
export declare function isQueueSignal(value: unknown): value is AbortSignal;
|
|
216
226
|
|
|
217
227
|
/**
|
|
218
|
-
* Determines whether a value is a valid queue timeout
|
|
228
|
+
* Determines whether a value is a valid queue timeout — an integer count of milliseconds
|
|
229
|
+
* inside the native timer range.
|
|
219
230
|
*
|
|
220
231
|
* @param value - Value to inspect
|
|
221
232
|
* @returns True if the value is an integer within the native timer range,
|
|
@@ -230,7 +241,8 @@ export declare function isQueueSignal(value: unknown): value is AbortSignal;
|
|
|
230
241
|
export declare function isQueueTimeout(value: unknown): value is number;
|
|
231
242
|
|
|
232
243
|
/**
|
|
233
|
-
*
|
|
244
|
+
* Determines whether a value is a valid stored queue entry — a record holding a string
|
|
245
|
+
* `id`, an `input`, and a nonnegative safe-integer `attempts`.
|
|
234
246
|
*
|
|
235
247
|
* @remarks
|
|
236
248
|
* The single test for what a {@link StoredEntry} is, shared by the queue's `restore`
|
|
@@ -250,7 +262,8 @@ export declare function isQueueTimeout(value: unknown): value is number;
|
|
|
250
262
|
export declare function isStoredEntry(value: unknown): value is StoredEntry<unknown>;
|
|
251
263
|
|
|
252
264
|
/**
|
|
253
|
-
* Represents an in-memory store
|
|
265
|
+
* Represents an in-memory store of a queue's outstanding entries — a process-lifetime `Map`
|
|
266
|
+
* owning validated, immutable JSON snapshots.
|
|
254
267
|
*
|
|
255
268
|
* @typeParam TInput - The contract shape for each stored input
|
|
256
269
|
*
|
|
@@ -282,7 +295,8 @@ export declare class MemoryQueueStore<TInput extends ContractShape> implements Q
|
|
|
282
295
|
}
|
|
283
296
|
|
|
284
297
|
/**
|
|
285
|
-
* Represents a concurrent, cooperative FIFO job queue
|
|
298
|
+
* Represents a concurrent, cooperative FIFO job queue — a wake-park worker loop with
|
|
299
|
+
* retries, a per-attempt timeout, abort, and optional outstanding-work persistence.
|
|
286
300
|
*
|
|
287
301
|
* @typeParam TInput - The work input each entry carries
|
|
288
302
|
* @typeParam TResult - The value each entry resolves
|
|
@@ -412,7 +426,8 @@ export declare interface QueueEntryOptions {
|
|
|
412
426
|
}
|
|
413
427
|
|
|
414
428
|
/**
|
|
415
|
-
* Represents a failure carrying a machine-readable
|
|
429
|
+
* Represents a queue failure carrying a lowercase machine-readable `code`, optional
|
|
430
|
+
* structured context, and an optional cause.
|
|
416
431
|
*
|
|
417
432
|
* @example
|
|
418
433
|
* ```ts
|
|
@@ -435,6 +450,10 @@ export declare class QueueError extends Error {
|
|
|
435
450
|
/**
|
|
436
451
|
* Represents the structured context carried by a {@link QueueError}.
|
|
437
452
|
*
|
|
453
|
+
* @remarks
|
|
454
|
+
* `option` names the {@link QueueOption} the failure concerns, and `operation` names the store
|
|
455
|
+
* call that failed.
|
|
456
|
+
*
|
|
438
457
|
* @example
|
|
439
458
|
* ```ts
|
|
440
459
|
* const context: QueueErrorContext = { id: 'job-1', operation: 'remove' }
|
|
@@ -463,17 +482,17 @@ export declare interface QueueErrorOptions {
|
|
|
463
482
|
|
|
464
483
|
/**
|
|
465
484
|
* Represents the push observation surface of a {@link QueueInterface} — the lifecycle
|
|
466
|
-
* moments a fire-and-forget observer (logging, metrics, tracing) subscribes to,
|
|
467
|
-
*
|
|
485
|
+
* moments a fire-and-forget observer (logging, metrics, tracing) subscribes to, beside the
|
|
486
|
+
* per-entry `enqueue` promise.
|
|
468
487
|
*
|
|
469
488
|
* @typeParam TResult - The value an entry resolves (the `success` payload), mirroring the
|
|
470
489
|
* {@link QueueInterface}'s own `TResult` — so the map is `QueueEventMap<TResult>`.
|
|
471
490
|
*
|
|
472
491
|
* @remarks
|
|
473
492
|
* Listener isolation is the emitter's: every event is emitted directly and a
|
|
474
|
-
* listener throw is routed to the emitter's
|
|
493
|
+
* listener throw is routed to the emitter's own `error` handler (the `error` option), never
|
|
475
494
|
* onto this domain map and never into the cooperative wake-park / settle-once engine — so a
|
|
476
|
-
* buggy observer can never reorder, throw into, or corrupt the queue. Every emit sits
|
|
495
|
+
* buggy observer can never reorder, throw into, or corrupt the queue. Every emit sits after
|
|
477
496
|
* the relevant wake / park / settle transition, so observation is purely a side-channel: a
|
|
478
497
|
* throwing observer cannot unbalance `active` or strand a parked worker. Subscribe through
|
|
479
498
|
* `queue.emitter.on(...)`.
|
|
@@ -506,7 +525,7 @@ export declare type QueueEventMap<TResult> = {
|
|
|
506
525
|
};
|
|
507
526
|
|
|
508
527
|
/**
|
|
509
|
-
* Runs one queued entry's work;
|
|
528
|
+
* Runs one queued entry's work; a rejection triggers a retry while attempts remain.
|
|
510
529
|
*
|
|
511
530
|
* @typeParam TInput - The work input
|
|
512
531
|
* @typeParam TResult - The resolved work result
|
|
@@ -519,12 +538,13 @@ export declare type QueueEventMap<TResult> = {
|
|
|
519
538
|
export declare type QueueHandler<TInput, TResult> = (input: TInput, context: QueueContext) => Promise<TResult> | TResult;
|
|
520
539
|
|
|
521
540
|
/**
|
|
522
|
-
* Represents a
|
|
541
|
+
* Represents the contract a queue consumer holds — the readings of its live work, the
|
|
542
|
+
* typed observation surface, and the calls that submit, restore, and wind it down.
|
|
523
543
|
*
|
|
524
544
|
* @remarks
|
|
525
545
|
* Exposes a typed {@link emitter} carrying its lifecycle moments
|
|
526
|
-
* ({@link QueueEventMap}) for fire-and-forget observers,
|
|
527
|
-
* Emitting is observation-only — every event fires
|
|
546
|
+
* ({@link QueueEventMap}) for fire-and-forget observers, beside each `enqueue` promise.
|
|
547
|
+
* Emitting is observation-only — every event fires after the relevant wake / park / settle
|
|
528
548
|
* transition, so a buggy observer can never reorder or corrupt the wake-park / settle-once
|
|
529
549
|
* engine: the emitter isolates a listener throw and routes it to its `error` handler (the
|
|
530
550
|
* `error` option), never the engine. Subscribe through `queue.emitter.on(...)`.
|
|
@@ -545,23 +565,23 @@ export declare interface QueueInterface<TInput, TResult> {
|
|
|
545
565
|
readonly paused: boolean;
|
|
546
566
|
/** Reports the halt state: true after `stop` or `abort` has halted the queue; false otherwise. */
|
|
547
567
|
readonly stopped: boolean;
|
|
548
|
-
/** Reserves
|
|
568
|
+
/** Reserves one FIFO entry, submits it, and wakes a worker; the returned promise settles when the entry finally settles. */
|
|
549
569
|
enqueue(input: TInput, options?: QueueEntryOptions): Promise<TResult>;
|
|
550
|
-
/** Re-enqueues outstanding entries
|
|
570
|
+
/** Re-enqueues the store's outstanding entries at their persisted attempt count so they run again; without a store it does nothing. */
|
|
551
571
|
restore(): Promise<void>;
|
|
552
|
-
/** Begins or restarts worker execution. */
|
|
572
|
+
/** Begins or restarts worker execution after a `stop`, spawning loops for accepted demand up to the queue's concurrency; after an abort it does nothing. */
|
|
553
573
|
start(): void;
|
|
554
|
-
/** Rejects non-active work and awaits current-loop
|
|
574
|
+
/** Rejects non-active work and awaits durable removals plus current-loop quiescence, while in-flight entries settle normally. */
|
|
555
575
|
stop(): Promise<void>;
|
|
556
|
-
/** Suspends
|
|
576
|
+
/** Suspends dequeuing until `resume`, parking the workers while in-flight entries keep running. */
|
|
557
577
|
pause(): void;
|
|
558
|
-
/** Continues
|
|
578
|
+
/** Continues a paused queue, waking the parked workers. */
|
|
559
579
|
resume(): void;
|
|
560
|
-
/** Cancels active work, rejects pending work, and awaits cleanup. */
|
|
580
|
+
/** Cancels active work through its signal, rejects pending work immediately, and awaits the owned persistence cleanup. */
|
|
561
581
|
abort(reason?: unknown): Promise<void>;
|
|
562
|
-
/** Rejects non-active work and awaits its durable
|
|
582
|
+
/** Rejects non-active work immediately and awaits its durable removal, leaving active entries untouched. */
|
|
563
583
|
clear(): Promise<void>;
|
|
564
|
-
/**
|
|
584
|
+
/** Blocks admissions, aborts, awaits the cleanup, then destroys the emitter last; idempotent. */
|
|
565
585
|
destroy(): Promise<void>;
|
|
566
586
|
}
|
|
567
587
|
|
|
@@ -609,10 +629,11 @@ export declare interface QueueOptions<TInput, TResult> {
|
|
|
609
629
|
}
|
|
610
630
|
|
|
611
631
|
/**
|
|
612
|
-
* Represents the durable backing for a
|
|
632
|
+
* Represents the durable backing for a queue's outstanding entries — the small keyed
|
|
633
|
+
* surface a restart resumes from.
|
|
613
634
|
*
|
|
614
635
|
* @remarks
|
|
615
|
-
* The store holds
|
|
636
|
+
* The store holds only work that has not yet completed: `save` upserts an entry
|
|
616
637
|
* (by its `id`), `remove` drops a finished one, `load` returns everything
|
|
617
638
|
* outstanding (to restore a queue after a restart), and `clear` empties it. It is
|
|
618
639
|
* a minimal interface over the `@orkestrel/database` layer — a queue's durable
|
|
@@ -627,18 +648,22 @@ export declare interface QueueOptions<TInput, TResult> {
|
|
|
627
648
|
* ```
|
|
628
649
|
*/
|
|
629
650
|
export declare interface QueueStoreInterface<TInput> {
|
|
651
|
+
/** Upserts one entry by its `id`, overwriting an entry already stored under that id. */
|
|
630
652
|
save(entry: StoredEntry<TInput>): Promise<void>;
|
|
653
|
+
/** Drops one finished entry by `id`, doing nothing when the store holds no such id. */
|
|
631
654
|
remove(id: string): Promise<void>;
|
|
655
|
+
/** Returns every outstanding entry — the work a restart resumes. */
|
|
632
656
|
load(): Promise<ReadonlyArray<StoredEntry<TInput>>>;
|
|
657
|
+
/** Empties the store, dropping every outstanding entry. */
|
|
633
658
|
clear(): Promise<void>;
|
|
634
659
|
}
|
|
635
660
|
|
|
636
661
|
/**
|
|
637
|
-
* Reads one named option from a caller-supplied entry options object
|
|
662
|
+
* Reads one named option from a caller-supplied entry options object exactly once,
|
|
663
|
+
* containing a throwing getter as a coded failure.
|
|
638
664
|
*
|
|
639
665
|
* @remarks
|
|
640
|
-
*
|
|
641
|
-
* getter. An absent options object reads nothing and yields `undefined`.
|
|
666
|
+
* An absent options object reads nothing and yields `undefined`.
|
|
642
667
|
*
|
|
643
668
|
* @param options - The caller's own entry options, or `undefined`
|
|
644
669
|
* @param option - The option key to read
|
|
@@ -658,7 +683,7 @@ export declare function readOption(options: QueueEntryOptions | undefined, optio
|
|
|
658
683
|
* Represents a durably persisted, still-outstanding queue entry — re-run after a restart.
|
|
659
684
|
*
|
|
660
685
|
* @remarks
|
|
661
|
-
* The store holds only entries that have
|
|
686
|
+
* The store holds only entries that have not yet completed, so what `load`
|
|
662
687
|
* returns on startup is exactly the work to resume. `id` keys the entry (the
|
|
663
688
|
* store upserts by it); `input` is the handler's work payload (it must be
|
|
664
689
|
* JSON-serializable to survive a JSON / SQLite driver); `attempts` is how many
|
|
@@ -676,7 +701,8 @@ export declare interface StoredEntry<TInput> {
|
|
|
676
701
|
}
|
|
677
702
|
|
|
678
703
|
/**
|
|
679
|
-
* Validates one already-read queue option against its guard
|
|
704
|
+
* Validates one already-read queue option against its guard, and throws the coded invalid
|
|
705
|
+
* failure carrying the option and the refused value when the guard refuses it.
|
|
680
706
|
*
|
|
681
707
|
* @param value - The option value to check
|
|
682
708
|
* @param guard - The total guard the value must satisfy
|
package/dist/src/core/index.d.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import { ContractShape } from '@orkestrel/contract';
|
|
2
|
-
import { DriverInterface } from '@orkestrel/database';
|
|
3
|
-
import { EmitterErrorHandler } from '@orkestrel/emitter';
|
|
4
|
-
import { EmitterHooks } from '@orkestrel/emitter';
|
|
5
|
-
import { EmitterInterface } from '@orkestrel/emitter';
|
|
6
|
-
import { Guard } from '@orkestrel/contract';
|
|
7
|
-
import { Infer } from '@orkestrel/contract';
|
|
8
|
-
import { TableInterface } from '@orkestrel/database';
|
|
1
|
+
import type { ContractShape } from '@orkestrel/contract';
|
|
2
|
+
import type { DriverInterface } from '@orkestrel/database';
|
|
3
|
+
import type { EmitterErrorHandler } from '@orkestrel/emitter';
|
|
4
|
+
import type { EmitterHooks } from '@orkestrel/emitter';
|
|
5
|
+
import type { EmitterInterface } from '@orkestrel/emitter';
|
|
6
|
+
import type { Guard } from '@orkestrel/contract';
|
|
7
|
+
import type { Infer } from '@orkestrel/contract';
|
|
8
|
+
import type { TableInterface } from '@orkestrel/database';
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Creates a {@link DatabaseQueueStore} over any {@link DriverInterface} — the durable,
|
|
12
|
-
* driver-pluggable backing for a queue's outstanding entries
|
|
12
|
+
* driver-pluggable backing for a queue's outstanding entries, across a memory, JSON, or
|
|
13
|
+
* SQLite driver.
|
|
13
14
|
*
|
|
14
15
|
* @remarks
|
|
15
16
|
* Builds a one-table database (`entries`, keyed by `id`) over the supplied driver, with
|
|
@@ -43,8 +44,9 @@ import { TableInterface } from '@orkestrel/database';
|
|
|
43
44
|
export declare function createDatabaseQueueStore<TInput extends ContractShape>(input: TInput, driver: DriverInterface): QueueStoreInterface<Infer<TInput>>;
|
|
44
45
|
|
|
45
46
|
/**
|
|
46
|
-
* Creates an in-memory {@link MemoryQueueStore} — the zero-plumbing
|
|
47
|
-
*
|
|
47
|
+
* Creates an in-memory {@link MemoryQueueStore} over a plain `Map` — the zero-plumbing
|
|
48
|
+
* {@link QueueStoreInterface} a queue takes when its outstanding entries need not outlive
|
|
49
|
+
* the process, and the twin of {@link DatabaseQueueStore}.
|
|
48
50
|
*
|
|
49
51
|
* @remarks
|
|
50
52
|
* The entries live in a process-lifetime `Map` and are gone when the process exits. The
|
|
@@ -75,7 +77,7 @@ export declare function createMemoryQueueStore<TInput extends ContractShape>(inp
|
|
|
75
77
|
* timeout / abort, all over the L1 `Abort` / `Timeout` primitives.
|
|
76
78
|
*
|
|
77
79
|
* @remarks
|
|
78
|
-
* Worker loops
|
|
80
|
+
* Worker loops park on a wake list when no entry is ready and are woken one-at-a-time
|
|
79
81
|
* by `enqueue` / `resume`, so an idle queue consumes no CPU (no polling). `enqueue`
|
|
80
82
|
* returns a promise that settles when the entry finally completes, fails (after its
|
|
81
83
|
* retries), or is rejected by an abort. A queue-level `abort` never retries; the
|
|
@@ -90,26 +92,32 @@ export declare function createMemoryQueueStore<TInput extends ContractShape>(inp
|
|
|
90
92
|
* `timeout` `0`.
|
|
91
93
|
* @returns A working {@link QueueInterface}
|
|
92
94
|
*
|
|
93
|
-
* @example
|
|
95
|
+
* @example Create a queue
|
|
94
96
|
* ```ts
|
|
95
97
|
* import { createQueue } from '@orkestrel/queue'
|
|
96
98
|
*
|
|
97
|
-
*
|
|
99
|
+
* // Ordered (concurrency defaults to 1): each entry runs to completion before the next.
|
|
100
|
+
* const queue = createQueue<Job, Output>({ handler: (job) => run(job) })
|
|
101
|
+
*
|
|
102
|
+
* const output = await queue.enqueue(job)
|
|
103
|
+
*
|
|
104
|
+
* // Bounded, retried, and time-boxed: four in flight, two extra attempts, 5s per attempt.
|
|
105
|
+
* const fetches = createQueue<string, number>({
|
|
98
106
|
* handler: async (url, { signal }) => (await fetch(url, { signal })).status,
|
|
99
107
|
* concurrency: 4,
|
|
100
108
|
* retries: 2,
|
|
101
109
|
* timeout: 5_000,
|
|
102
110
|
* })
|
|
103
111
|
*
|
|
104
|
-
* const status = await
|
|
112
|
+
* const status = await fetches.enqueue('https://example.com')
|
|
105
113
|
* ```
|
|
106
114
|
*/
|
|
107
115
|
export declare function createQueue<TInput, TResult>(options: QueueOptions<TInput, TResult>): QueueInterface<TInput, TResult>;
|
|
108
116
|
|
|
109
117
|
/**
|
|
110
|
-
* Represents
|
|
111
|
-
* queue's durable state
|
|
112
|
-
* `TableInterface`.
|
|
118
|
+
* Represents the opt-in durable store for a queue's outstanding entries, backed by one table
|
|
119
|
+
* of the `@orkestrel/database` layer — a queue's durable state is a table, so persistence
|
|
120
|
+
* reduces to keyed CRUD over a `TableInterface`.
|
|
113
121
|
*
|
|
114
122
|
* @remarks
|
|
115
123
|
* The store is driver-agnostic: it holds a single {@link TableInterface} whose
|
|
@@ -119,7 +127,7 @@ export declare function createQueue<TInput, TResult>(options: QueueOptions<TInpu
|
|
|
119
127
|
* empties the table. Reads are narrowed through the table's contract, so `load`
|
|
120
128
|
* returns typed {@link StoredEntry}`<TInput>[]` with no cast — the table
|
|
121
129
|
* is created over the `{ id; input; attempts }` column map, which `Infer`s to exactly
|
|
122
|
-
* that row. The store holds only
|
|
130
|
+
* that row. The store holds only outstanding work (completed entries are `remove`d), so
|
|
123
131
|
* `load` on startup yields precisely the entries to resume.
|
|
124
132
|
*
|
|
125
133
|
* @typeParam TInput - The work input each {@link StoredEntry} carries
|
|
@@ -155,7 +163,7 @@ export declare class DatabaseQueueStore<TInput> implements QueueStoreInterface<T
|
|
|
155
163
|
}
|
|
156
164
|
|
|
157
165
|
/**
|
|
158
|
-
* Determines whether a value is a valid queue concurrency.
|
|
166
|
+
* Determines whether a value is a valid queue concurrency — a positive safe integer.
|
|
159
167
|
*
|
|
160
168
|
* @param value - Value to inspect
|
|
161
169
|
* @returns True if the value is a positive safe integer; false otherwise
|
|
@@ -169,7 +177,8 @@ export declare class DatabaseQueueStore<TInput> implements QueueStoreInterface<T
|
|
|
169
177
|
export declare function isQueueConcurrency(value: unknown): value is number;
|
|
170
178
|
|
|
171
179
|
/**
|
|
172
|
-
* Determines whether an unknown value is a {@link QueueError}
|
|
180
|
+
* Determines whether an unknown value is a {@link QueueError}, staying total for a hostile
|
|
181
|
+
* value.
|
|
173
182
|
*
|
|
174
183
|
* @param value - The value to inspect
|
|
175
184
|
* @returns True if the value is a real `QueueError` instance; false otherwise, including for
|
|
@@ -187,7 +196,7 @@ export declare function isQueueConcurrency(value: unknown): value is number;
|
|
|
187
196
|
export declare function isQueueError(value: unknown): value is QueueError;
|
|
188
197
|
|
|
189
198
|
/**
|
|
190
|
-
* Determines whether a value is a valid queue retry count.
|
|
199
|
+
* Determines whether a value is a valid queue retry count — a nonnegative safe integer.
|
|
191
200
|
*
|
|
192
201
|
* @param value - Value to inspect
|
|
193
202
|
* @returns True if the value is a nonnegative safe integer; false otherwise
|
|
@@ -201,7 +210,8 @@ export declare function isQueueError(value: unknown): value is QueueError;
|
|
|
201
210
|
export declare function isQueueRetries(value: unknown): value is number;
|
|
202
211
|
|
|
203
212
|
/**
|
|
204
|
-
* Determines whether a value is a native abort signal usable by the queue
|
|
213
|
+
* Determines whether a value is a native abort signal usable by the queue, testing the
|
|
214
|
+
* native brand rather than the shape.
|
|
205
215
|
*
|
|
206
216
|
* @param value - Value to inspect
|
|
207
217
|
* @returns True if the value carries the native `AbortSignal` internal slot; false otherwise
|
|
@@ -215,7 +225,8 @@ export declare function isQueueRetries(value: unknown): value is number;
|
|
|
215
225
|
export declare function isQueueSignal(value: unknown): value is AbortSignal;
|
|
216
226
|
|
|
217
227
|
/**
|
|
218
|
-
* Determines whether a value is a valid queue timeout
|
|
228
|
+
* Determines whether a value is a valid queue timeout — an integer count of milliseconds
|
|
229
|
+
* inside the native timer range.
|
|
219
230
|
*
|
|
220
231
|
* @param value - Value to inspect
|
|
221
232
|
* @returns True if the value is an integer within the native timer range,
|
|
@@ -230,7 +241,8 @@ export declare function isQueueSignal(value: unknown): value is AbortSignal;
|
|
|
230
241
|
export declare function isQueueTimeout(value: unknown): value is number;
|
|
231
242
|
|
|
232
243
|
/**
|
|
233
|
-
*
|
|
244
|
+
* Determines whether a value is a valid stored queue entry — a record holding a string
|
|
245
|
+
* `id`, an `input`, and a nonnegative safe-integer `attempts`.
|
|
234
246
|
*
|
|
235
247
|
* @remarks
|
|
236
248
|
* The single test for what a {@link StoredEntry} is, shared by the queue's `restore`
|
|
@@ -250,7 +262,8 @@ export declare function isQueueTimeout(value: unknown): value is number;
|
|
|
250
262
|
export declare function isStoredEntry(value: unknown): value is StoredEntry<unknown>;
|
|
251
263
|
|
|
252
264
|
/**
|
|
253
|
-
* Represents an in-memory store
|
|
265
|
+
* Represents an in-memory store of a queue's outstanding entries — a process-lifetime `Map`
|
|
266
|
+
* owning validated, immutable JSON snapshots.
|
|
254
267
|
*
|
|
255
268
|
* @typeParam TInput - The contract shape for each stored input
|
|
256
269
|
*
|
|
@@ -282,7 +295,8 @@ export declare class MemoryQueueStore<TInput extends ContractShape> implements Q
|
|
|
282
295
|
}
|
|
283
296
|
|
|
284
297
|
/**
|
|
285
|
-
* Represents a concurrent, cooperative FIFO job queue
|
|
298
|
+
* Represents a concurrent, cooperative FIFO job queue — a wake-park worker loop with
|
|
299
|
+
* retries, a per-attempt timeout, abort, and optional outstanding-work persistence.
|
|
286
300
|
*
|
|
287
301
|
* @typeParam TInput - The work input each entry carries
|
|
288
302
|
* @typeParam TResult - The value each entry resolves
|
|
@@ -412,7 +426,8 @@ export declare interface QueueEntryOptions {
|
|
|
412
426
|
}
|
|
413
427
|
|
|
414
428
|
/**
|
|
415
|
-
* Represents a failure carrying a machine-readable
|
|
429
|
+
* Represents a queue failure carrying a lowercase machine-readable `code`, optional
|
|
430
|
+
* structured context, and an optional cause.
|
|
416
431
|
*
|
|
417
432
|
* @example
|
|
418
433
|
* ```ts
|
|
@@ -435,6 +450,10 @@ export declare class QueueError extends Error {
|
|
|
435
450
|
/**
|
|
436
451
|
* Represents the structured context carried by a {@link QueueError}.
|
|
437
452
|
*
|
|
453
|
+
* @remarks
|
|
454
|
+
* `option` names the {@link QueueOption} the failure concerns, and `operation` names the store
|
|
455
|
+
* call that failed.
|
|
456
|
+
*
|
|
438
457
|
* @example
|
|
439
458
|
* ```ts
|
|
440
459
|
* const context: QueueErrorContext = { id: 'job-1', operation: 'remove' }
|
|
@@ -463,17 +482,17 @@ export declare interface QueueErrorOptions {
|
|
|
463
482
|
|
|
464
483
|
/**
|
|
465
484
|
* Represents the push observation surface of a {@link QueueInterface} — the lifecycle
|
|
466
|
-
* moments a fire-and-forget observer (logging, metrics, tracing) subscribes to,
|
|
467
|
-
*
|
|
485
|
+
* moments a fire-and-forget observer (logging, metrics, tracing) subscribes to, beside the
|
|
486
|
+
* per-entry `enqueue` promise.
|
|
468
487
|
*
|
|
469
488
|
* @typeParam TResult - The value an entry resolves (the `success` payload), mirroring the
|
|
470
489
|
* {@link QueueInterface}'s own `TResult` — so the map is `QueueEventMap<TResult>`.
|
|
471
490
|
*
|
|
472
491
|
* @remarks
|
|
473
492
|
* Listener isolation is the emitter's: every event is emitted directly and a
|
|
474
|
-
* listener throw is routed to the emitter's
|
|
493
|
+
* listener throw is routed to the emitter's own `error` handler (the `error` option), never
|
|
475
494
|
* onto this domain map and never into the cooperative wake-park / settle-once engine — so a
|
|
476
|
-
* buggy observer can never reorder, throw into, or corrupt the queue. Every emit sits
|
|
495
|
+
* buggy observer can never reorder, throw into, or corrupt the queue. Every emit sits after
|
|
477
496
|
* the relevant wake / park / settle transition, so observation is purely a side-channel: a
|
|
478
497
|
* throwing observer cannot unbalance `active` or strand a parked worker. Subscribe through
|
|
479
498
|
* `queue.emitter.on(...)`.
|
|
@@ -506,7 +525,7 @@ export declare type QueueEventMap<TResult> = {
|
|
|
506
525
|
};
|
|
507
526
|
|
|
508
527
|
/**
|
|
509
|
-
* Runs one queued entry's work;
|
|
528
|
+
* Runs one queued entry's work; a rejection triggers a retry while attempts remain.
|
|
510
529
|
*
|
|
511
530
|
* @typeParam TInput - The work input
|
|
512
531
|
* @typeParam TResult - The resolved work result
|
|
@@ -519,12 +538,13 @@ export declare type QueueEventMap<TResult> = {
|
|
|
519
538
|
export declare type QueueHandler<TInput, TResult> = (input: TInput, context: QueueContext) => Promise<TResult> | TResult;
|
|
520
539
|
|
|
521
540
|
/**
|
|
522
|
-
* Represents a
|
|
541
|
+
* Represents the contract a queue consumer holds — the readings of its live work, the
|
|
542
|
+
* typed observation surface, and the calls that submit, restore, and wind it down.
|
|
523
543
|
*
|
|
524
544
|
* @remarks
|
|
525
545
|
* Exposes a typed {@link emitter} carrying its lifecycle moments
|
|
526
|
-
* ({@link QueueEventMap}) for fire-and-forget observers,
|
|
527
|
-
* Emitting is observation-only — every event fires
|
|
546
|
+
* ({@link QueueEventMap}) for fire-and-forget observers, beside each `enqueue` promise.
|
|
547
|
+
* Emitting is observation-only — every event fires after the relevant wake / park / settle
|
|
528
548
|
* transition, so a buggy observer can never reorder or corrupt the wake-park / settle-once
|
|
529
549
|
* engine: the emitter isolates a listener throw and routes it to its `error` handler (the
|
|
530
550
|
* `error` option), never the engine. Subscribe through `queue.emitter.on(...)`.
|
|
@@ -545,23 +565,23 @@ export declare interface QueueInterface<TInput, TResult> {
|
|
|
545
565
|
readonly paused: boolean;
|
|
546
566
|
/** Reports the halt state: true after `stop` or `abort` has halted the queue; false otherwise. */
|
|
547
567
|
readonly stopped: boolean;
|
|
548
|
-
/** Reserves
|
|
568
|
+
/** Reserves one FIFO entry, submits it, and wakes a worker; the returned promise settles when the entry finally settles. */
|
|
549
569
|
enqueue(input: TInput, options?: QueueEntryOptions): Promise<TResult>;
|
|
550
|
-
/** Re-enqueues outstanding entries
|
|
570
|
+
/** Re-enqueues the store's outstanding entries at their persisted attempt count so they run again; without a store it does nothing. */
|
|
551
571
|
restore(): Promise<void>;
|
|
552
|
-
/** Begins or restarts worker execution. */
|
|
572
|
+
/** Begins or restarts worker execution after a `stop`, spawning loops for accepted demand up to the queue's concurrency; after an abort it does nothing. */
|
|
553
573
|
start(): void;
|
|
554
|
-
/** Rejects non-active work and awaits current-loop
|
|
574
|
+
/** Rejects non-active work and awaits durable removals plus current-loop quiescence, while in-flight entries settle normally. */
|
|
555
575
|
stop(): Promise<void>;
|
|
556
|
-
/** Suspends
|
|
576
|
+
/** Suspends dequeuing until `resume`, parking the workers while in-flight entries keep running. */
|
|
557
577
|
pause(): void;
|
|
558
|
-
/** Continues
|
|
578
|
+
/** Continues a paused queue, waking the parked workers. */
|
|
559
579
|
resume(): void;
|
|
560
|
-
/** Cancels active work, rejects pending work, and awaits cleanup. */
|
|
580
|
+
/** Cancels active work through its signal, rejects pending work immediately, and awaits the owned persistence cleanup. */
|
|
561
581
|
abort(reason?: unknown): Promise<void>;
|
|
562
|
-
/** Rejects non-active work and awaits its durable
|
|
582
|
+
/** Rejects non-active work immediately and awaits its durable removal, leaving active entries untouched. */
|
|
563
583
|
clear(): Promise<void>;
|
|
564
|
-
/**
|
|
584
|
+
/** Blocks admissions, aborts, awaits the cleanup, then destroys the emitter last; idempotent. */
|
|
565
585
|
destroy(): Promise<void>;
|
|
566
586
|
}
|
|
567
587
|
|
|
@@ -609,10 +629,11 @@ export declare interface QueueOptions<TInput, TResult> {
|
|
|
609
629
|
}
|
|
610
630
|
|
|
611
631
|
/**
|
|
612
|
-
* Represents the durable backing for a
|
|
632
|
+
* Represents the durable backing for a queue's outstanding entries — the small keyed
|
|
633
|
+
* surface a restart resumes from.
|
|
613
634
|
*
|
|
614
635
|
* @remarks
|
|
615
|
-
* The store holds
|
|
636
|
+
* The store holds only work that has not yet completed: `save` upserts an entry
|
|
616
637
|
* (by its `id`), `remove` drops a finished one, `load` returns everything
|
|
617
638
|
* outstanding (to restore a queue after a restart), and `clear` empties it. It is
|
|
618
639
|
* a minimal interface over the `@orkestrel/database` layer — a queue's durable
|
|
@@ -627,18 +648,22 @@ export declare interface QueueOptions<TInput, TResult> {
|
|
|
627
648
|
* ```
|
|
628
649
|
*/
|
|
629
650
|
export declare interface QueueStoreInterface<TInput> {
|
|
651
|
+
/** Upserts one entry by its `id`, overwriting an entry already stored under that id. */
|
|
630
652
|
save(entry: StoredEntry<TInput>): Promise<void>;
|
|
653
|
+
/** Drops one finished entry by `id`, doing nothing when the store holds no such id. */
|
|
631
654
|
remove(id: string): Promise<void>;
|
|
655
|
+
/** Returns every outstanding entry — the work a restart resumes. */
|
|
632
656
|
load(): Promise<ReadonlyArray<StoredEntry<TInput>>>;
|
|
657
|
+
/** Empties the store, dropping every outstanding entry. */
|
|
633
658
|
clear(): Promise<void>;
|
|
634
659
|
}
|
|
635
660
|
|
|
636
661
|
/**
|
|
637
|
-
* Reads one named option from a caller-supplied entry options object
|
|
662
|
+
* Reads one named option from a caller-supplied entry options object exactly once,
|
|
663
|
+
* containing a throwing getter as a coded failure.
|
|
638
664
|
*
|
|
639
665
|
* @remarks
|
|
640
|
-
*
|
|
641
|
-
* getter. An absent options object reads nothing and yields `undefined`.
|
|
666
|
+
* An absent options object reads nothing and yields `undefined`.
|
|
642
667
|
*
|
|
643
668
|
* @param options - The caller's own entry options, or `undefined`
|
|
644
669
|
* @param option - The option key to read
|
|
@@ -658,7 +683,7 @@ export declare function readOption(options: QueueEntryOptions | undefined, optio
|
|
|
658
683
|
* Represents a durably persisted, still-outstanding queue entry — re-run after a restart.
|
|
659
684
|
*
|
|
660
685
|
* @remarks
|
|
661
|
-
* The store holds only entries that have
|
|
686
|
+
* The store holds only entries that have not yet completed, so what `load`
|
|
662
687
|
* returns on startup is exactly the work to resume. `id` keys the entry (the
|
|
663
688
|
* store upserts by it); `input` is the handler's work payload (it must be
|
|
664
689
|
* JSON-serializable to survive a JSON / SQLite driver); `attempts` is how many
|
|
@@ -676,7 +701,8 @@ export declare interface StoredEntry<TInput> {
|
|
|
676
701
|
}
|
|
677
702
|
|
|
678
703
|
/**
|
|
679
|
-
* Validates one already-read queue option against its guard
|
|
704
|
+
* Validates one already-read queue option against its guard, and throws the coded invalid
|
|
705
|
+
* failure carrying the option and the refused value when the guard refuses it.
|
|
680
706
|
*
|
|
681
707
|
* @param value - The option value to check
|
|
682
708
|
* @param guard - The total guard the value must satisfy
|