@orkestrel/queue 0.0.11 → 0.0.13
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 +12 -18
- package/dist/src/core/index.cjs +212 -174
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +255 -134
- package/dist/src/core/index.d.ts +255 -134
- package/dist/src/core/index.js +211 -176
- package/dist/src/core/index.js.map +1 -1
- package/package.json +17 -19
package/dist/src/core/index.d.ts
CHANGED
|
@@ -1,14 +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 {
|
|
7
|
-
import {
|
|
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';
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
|
-
*
|
|
11
|
-
* driver-pluggable backing for a queue's outstanding entries
|
|
11
|
+
* Creates a {@link DatabaseQueueStore} over any {@link DriverInterface} — the durable,
|
|
12
|
+
* driver-pluggable backing for a queue's outstanding entries, across a memory, JSON, or
|
|
13
|
+
* SQLite driver.
|
|
12
14
|
*
|
|
13
15
|
* @remarks
|
|
14
16
|
* Builds a one-table database (`entries`, keyed by `id`) over the supplied driver, with
|
|
@@ -42,8 +44,9 @@ import { TableInterface } from '@orkestrel/database';
|
|
|
42
44
|
export declare function createDatabaseQueueStore<TInput extends ContractShape>(input: TInput, driver: DriverInterface): QueueStoreInterface<Infer<TInput>>;
|
|
43
45
|
|
|
44
46
|
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
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}.
|
|
47
50
|
*
|
|
48
51
|
* @remarks
|
|
49
52
|
* The entries live in a process-lifetime `Map` and are gone when the process exits. The
|
|
@@ -69,45 +72,52 @@ export declare function createDatabaseQueueStore<TInput extends ContractShape>(i
|
|
|
69
72
|
export declare function createMemoryQueueStore<TInput extends ContractShape>(input: TInput): QueueStoreInterface<Infer<TInput>>;
|
|
70
73
|
|
|
71
74
|
/**
|
|
72
|
-
*
|
|
75
|
+
* Creates a concurrent, cooperative job queue — a bounded-concurrency engine that
|
|
73
76
|
* runs each enqueued input through a handler, with retries and a per-attempt
|
|
74
77
|
* timeout / abort, all over the L1 `Abort` / `Timeout` primitives.
|
|
75
78
|
*
|
|
76
79
|
* @remarks
|
|
77
|
-
* Worker loops
|
|
80
|
+
* Worker loops park on a wake list when no entry is ready and are woken one-at-a-time
|
|
78
81
|
* by `enqueue` / `resume`, so an idle queue consumes no CPU (no polling). `enqueue`
|
|
79
82
|
* returns a promise that settles when the entry finally completes, fails (after its
|
|
80
83
|
* retries), or is rejected by an abort. A queue-level `abort` never retries; the
|
|
81
84
|
* lifecycle (`start` / `stop` / `pause` / `resume` / `abort` / `clear` / `destroy`)
|
|
82
|
-
* follows
|
|
85
|
+
* follows the fixed lifecycle vocabulary. The queue is observable: a typed `emitter` surfaces
|
|
83
86
|
* `enqueue` / `start` / `retry` / `success` / `failure` / `abort` / `drain`.
|
|
84
87
|
*
|
|
85
88
|
* @typeParam TInput - The work input each entry carries
|
|
86
89
|
* @typeParam TResult - The value the handler resolves for an entry
|
|
87
|
-
* @param options - The `handler` plus optional `concurrency`
|
|
88
|
-
*
|
|
90
|
+
* @param options - The `handler` plus optional `concurrency`, `retries`, and a default
|
|
91
|
+
* per-attempt `timeout` in milliseconds. Default: `concurrency` `1`, `retries` `0`,
|
|
92
|
+
* `timeout` `0`.
|
|
89
93
|
* @returns A working {@link QueueInterface}
|
|
90
94
|
*
|
|
91
|
-
* @example
|
|
95
|
+
* @example Create a queue
|
|
92
96
|
* ```ts
|
|
93
97
|
* import { createQueue } from '@orkestrel/queue'
|
|
94
98
|
*
|
|
95
|
-
*
|
|
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>({
|
|
96
106
|
* handler: async (url, { signal }) => (await fetch(url, { signal })).status,
|
|
97
107
|
* concurrency: 4,
|
|
98
108
|
* retries: 2,
|
|
99
109
|
* timeout: 5_000,
|
|
100
110
|
* })
|
|
101
111
|
*
|
|
102
|
-
* const status = await
|
|
112
|
+
* const status = await fetches.enqueue('https://example.com')
|
|
103
113
|
* ```
|
|
104
114
|
*/
|
|
105
115
|
export declare function createQueue<TInput, TResult>(options: QueueOptions<TInput, TResult>): QueueInterface<TInput, TResult>;
|
|
106
116
|
|
|
107
117
|
/**
|
|
108
|
-
*
|
|
109
|
-
* queue's durable state
|
|
110
|
-
* `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`.
|
|
111
121
|
*
|
|
112
122
|
* @remarks
|
|
113
123
|
* The store is driver-agnostic: it holds a single {@link TableInterface} whose
|
|
@@ -115,9 +125,9 @@ export declare function createQueue<TInput, TResult>(options: QueueOptions<TInpu
|
|
|
115
125
|
* `save` upserts an entry by its primary key (`set`), `remove` drops one (`remove`),
|
|
116
126
|
* `load` returns every outstanding entry (`records` with no criteria), and `clear`
|
|
117
127
|
* empties the table. Reads are narrowed through the table's contract, so `load`
|
|
118
|
-
* returns typed {@link StoredEntry}`<TInput>[]` with no cast
|
|
128
|
+
* returns typed {@link StoredEntry}`<TInput>[]` with no cast — the table
|
|
119
129
|
* is created over the `{ id; input; attempts }` column map, which `Infer`s to exactly
|
|
120
|
-
* that row. The store holds only
|
|
130
|
+
* that row. The store holds only outstanding work (completed entries are `remove`d), so
|
|
121
131
|
* `load` on startup yields precisely the entries to resume.
|
|
122
132
|
*
|
|
123
133
|
* @typeParam TInput - The work input each {@link StoredEntry} carries
|
|
@@ -136,27 +146,27 @@ export declare function createQueue<TInput, TResult>(options: QueueOptions<TInpu
|
|
|
136
146
|
export declare class DatabaseQueueStore<TInput> implements QueueStoreInterface<TInput> {
|
|
137
147
|
#private;
|
|
138
148
|
/**
|
|
139
|
-
*
|
|
149
|
+
* Wraps a table as a queue store.
|
|
140
150
|
*
|
|
141
151
|
* @param table - The {@link TableInterface} holding the outstanding entries — its
|
|
142
152
|
* row is a {@link StoredEntry}`<TInput>` (the `{ id; input; attempts }` shape)
|
|
143
153
|
*/
|
|
144
154
|
constructor(table: TableInterface<StoredEntry<TInput>>);
|
|
145
|
-
/**
|
|
155
|
+
/** Upserts an entry by its `id` (a re-`save` of an existing `id` overwrites it). */
|
|
146
156
|
save(entry: StoredEntry<TInput>): Promise<void>;
|
|
147
|
-
/**
|
|
157
|
+
/** Drops a finished entry by `id`; absent is a no-op (no throw). */
|
|
148
158
|
remove(id: string): Promise<void>;
|
|
149
|
-
/**
|
|
159
|
+
/** Returns every outstanding entry — the work to resume after a restart. */
|
|
150
160
|
load(): Promise<ReadonlyArray<StoredEntry<TInput>>>;
|
|
151
|
-
/**
|
|
161
|
+
/** Empties the store — drops every outstanding entry. */
|
|
152
162
|
clear(): Promise<void>;
|
|
153
163
|
}
|
|
154
164
|
|
|
155
165
|
/**
|
|
156
|
-
*
|
|
166
|
+
* Determines whether a value is a valid queue concurrency — a positive safe integer.
|
|
157
167
|
*
|
|
158
168
|
* @param value - Value to inspect
|
|
159
|
-
* @returns
|
|
169
|
+
* @returns True if the value is a positive safe integer; false otherwise
|
|
160
170
|
*
|
|
161
171
|
* @example
|
|
162
172
|
* ```ts
|
|
@@ -167,10 +177,12 @@ export declare class DatabaseQueueStore<TInput> implements QueueStoreInterface<T
|
|
|
167
177
|
export declare function isQueueConcurrency(value: unknown): value is number;
|
|
168
178
|
|
|
169
179
|
/**
|
|
170
|
-
*
|
|
180
|
+
* Determines whether an unknown value is a {@link QueueError}, staying total for a hostile
|
|
181
|
+
* value.
|
|
171
182
|
*
|
|
172
183
|
* @param value - The value to inspect
|
|
173
|
-
* @returns
|
|
184
|
+
* @returns True if the value is a real `QueueError` instance; false otherwise, including for
|
|
185
|
+
* a hostile value
|
|
174
186
|
*
|
|
175
187
|
* @example
|
|
176
188
|
* ```ts
|
|
@@ -184,10 +196,10 @@ export declare function isQueueConcurrency(value: unknown): value is number;
|
|
|
184
196
|
export declare function isQueueError(value: unknown): value is QueueError;
|
|
185
197
|
|
|
186
198
|
/**
|
|
187
|
-
*
|
|
199
|
+
* Determines whether a value is a valid queue retry count — a nonnegative safe integer.
|
|
188
200
|
*
|
|
189
201
|
* @param value - Value to inspect
|
|
190
|
-
* @returns
|
|
202
|
+
* @returns True if the value is a nonnegative safe integer; false otherwise
|
|
191
203
|
*
|
|
192
204
|
* @example
|
|
193
205
|
* ```ts
|
|
@@ -198,10 +210,11 @@ export declare function isQueueError(value: unknown): value is QueueError;
|
|
|
198
210
|
export declare function isQueueRetries(value: unknown): value is number;
|
|
199
211
|
|
|
200
212
|
/**
|
|
201
|
-
*
|
|
213
|
+
* Determines whether a value is a native abort signal usable by the queue, testing the
|
|
214
|
+
* native brand rather than the shape.
|
|
202
215
|
*
|
|
203
216
|
* @param value - Value to inspect
|
|
204
|
-
* @returns
|
|
217
|
+
* @returns True if the value carries the native `AbortSignal` internal slot; false otherwise
|
|
205
218
|
*
|
|
206
219
|
* @example
|
|
207
220
|
* ```ts
|
|
@@ -212,10 +225,12 @@ export declare function isQueueRetries(value: unknown): value is number;
|
|
|
212
225
|
export declare function isQueueSignal(value: unknown): value is AbortSignal;
|
|
213
226
|
|
|
214
227
|
/**
|
|
215
|
-
*
|
|
228
|
+
* Determines whether a value is a valid queue timeout — an integer count of milliseconds
|
|
229
|
+
* inside the native timer range.
|
|
216
230
|
*
|
|
217
231
|
* @param value - Value to inspect
|
|
218
|
-
* @returns
|
|
232
|
+
* @returns True if the value is an integer within the native timer range,
|
|
233
|
+
* inclusive; false otherwise
|
|
219
234
|
*
|
|
220
235
|
* @example
|
|
221
236
|
* ```ts
|
|
@@ -226,7 +241,29 @@ export declare function isQueueSignal(value: unknown): value is AbortSignal;
|
|
|
226
241
|
export declare function isQueueTimeout(value: unknown): value is number;
|
|
227
242
|
|
|
228
243
|
/**
|
|
229
|
-
*
|
|
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`.
|
|
246
|
+
*
|
|
247
|
+
* @remarks
|
|
248
|
+
* The single test for what a {@link StoredEntry} is, shared by the queue's `restore`
|
|
249
|
+
* boundary and the memory store's `save` boundary. `input` carries the caller's own
|
|
250
|
+
* payload type, so only its presence is checked.
|
|
251
|
+
*
|
|
252
|
+
* @param value - Value to inspect
|
|
253
|
+
* @returns True if the value is a record holding a string `id`, an `input` member, and a
|
|
254
|
+
* nonnegative safe-integer `attempts`; false otherwise
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```ts
|
|
258
|
+
* isStoredEntry({ id: 'job-1', input: 'task', attempts: 0 }) // true
|
|
259
|
+
* isStoredEntry({ id: 'job-1', input: 'task', attempts: -1 }) // false
|
|
260
|
+
* ```
|
|
261
|
+
*/
|
|
262
|
+
export declare function isStoredEntry(value: unknown): value is StoredEntry<unknown>;
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Represents an in-memory store of a queue's outstanding entries — a process-lifetime `Map`
|
|
266
|
+
* owning validated, immutable JSON snapshots.
|
|
230
267
|
*
|
|
231
268
|
* @typeParam TInput - The contract shape for each stored input
|
|
232
269
|
*
|
|
@@ -242,23 +279,24 @@ export declare function isQueueTimeout(value: unknown): value is number;
|
|
|
242
279
|
export declare class MemoryQueueStore<TInput extends ContractShape> implements QueueStoreInterface<Infer<TInput>> {
|
|
243
280
|
#private;
|
|
244
281
|
/**
|
|
245
|
-
*
|
|
282
|
+
* Creates an in-memory queue store.
|
|
246
283
|
*
|
|
247
284
|
* @param input - Runtime contract for each stored input
|
|
248
285
|
*/
|
|
249
286
|
constructor(input: TInput);
|
|
250
|
-
/**
|
|
287
|
+
/** Upserts a validated, owned snapshot under the entry's id. */
|
|
251
288
|
save(entry: StoredEntry<Infer<TInput>>): Promise<void>;
|
|
252
|
-
/**
|
|
289
|
+
/** Removes an entry; an absent id is a no-op. */
|
|
253
290
|
remove(id: string): Promise<void>;
|
|
254
|
-
/**
|
|
291
|
+
/** Returns fresh immutable snapshots of every outstanding entry. */
|
|
255
292
|
load(): Promise<ReadonlyArray<StoredEntry<Infer<TInput>>>>;
|
|
256
|
-
/**
|
|
293
|
+
/** Removes every outstanding entry. */
|
|
257
294
|
clear(): Promise<void>;
|
|
258
295
|
}
|
|
259
296
|
|
|
260
297
|
/**
|
|
261
|
-
*
|
|
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.
|
|
262
300
|
*
|
|
263
301
|
* @typeParam TInput - The work input each entry carries
|
|
264
302
|
* @typeParam TResult - The value each entry resolves
|
|
@@ -272,7 +310,7 @@ export declare class MemoryQueueStore<TInput extends ContractShape> implements Q
|
|
|
272
310
|
export declare class Queue<TInput, TResult> implements QueueInterface<TInput, TResult> {
|
|
273
311
|
#private;
|
|
274
312
|
/**
|
|
275
|
-
*
|
|
313
|
+
* Creates a queue.
|
|
276
314
|
*
|
|
277
315
|
* @param options - Handler, validated execution limits, persistence, and observation hooks
|
|
278
316
|
*/
|
|
@@ -283,39 +321,56 @@ export declare class Queue<TInput, TResult> implements QueueInterface<TInput, TR
|
|
|
283
321
|
get paused(): boolean;
|
|
284
322
|
get stopped(): boolean;
|
|
285
323
|
/**
|
|
286
|
-
*
|
|
324
|
+
* Reserves and submits one FIFO entry.
|
|
287
325
|
*
|
|
288
326
|
* @param input - Handler input
|
|
289
327
|
* @param options - Optional id, retry/timeout overrides, and entry abort signal
|
|
290
328
|
* @returns The entry's settle-once execution promise
|
|
291
|
-
* @throws {QueueError}
|
|
329
|
+
* @throws {QueueError} Thrown synchronously when an option is inaccessible or invalid.
|
|
292
330
|
*/
|
|
293
331
|
enqueue(input: TInput, options?: QueueEntryOptions): Promise<TResult>;
|
|
294
|
-
/**
|
|
332
|
+
/**
|
|
333
|
+
* Loads and accepts every outstanding stored entry from the current lifecycle generation.
|
|
334
|
+
*
|
|
335
|
+
* @returns A promise that settles after every loaded entry is reserved and accepted
|
|
336
|
+
* @throws {QueueError} Thrown when the store's load fails or returns a malformed entry.
|
|
337
|
+
*/
|
|
295
338
|
restore(): Promise<void>;
|
|
296
|
-
/**
|
|
339
|
+
/** Begins or restarts worker execution unless the queue is terminal. */
|
|
297
340
|
start(): void;
|
|
298
|
-
/**
|
|
341
|
+
/**
|
|
342
|
+
* Rejects non-active work and awaits current-loop and durable cleanup quiescence.
|
|
343
|
+
*
|
|
344
|
+
* @returns The stable stop barrier
|
|
345
|
+
*/
|
|
299
346
|
stop(): Promise<void>;
|
|
300
|
-
/**
|
|
347
|
+
/** Suspends new execution while leaving active entries untouched. */
|
|
301
348
|
pause(): void;
|
|
302
|
-
/**
|
|
349
|
+
/** Continues execution after a pause. */
|
|
303
350
|
resume(): void;
|
|
304
351
|
/**
|
|
305
|
-
*
|
|
352
|
+
* Cancels the queue and awaits owned cleanup.
|
|
306
353
|
*
|
|
307
354
|
* @param reason - Optional cause carried by the coded abort error
|
|
308
355
|
* @returns The stable abort barrier
|
|
309
356
|
*/
|
|
310
357
|
abort(reason?: unknown): Promise<void>;
|
|
311
|
-
/**
|
|
358
|
+
/**
|
|
359
|
+
* Rejects non-active work and awaits its durable cleanup.
|
|
360
|
+
*
|
|
361
|
+
* @returns A promise that settles after every affected removal completes
|
|
362
|
+
*/
|
|
312
363
|
clear(): Promise<void>;
|
|
313
|
-
/**
|
|
364
|
+
/**
|
|
365
|
+
* Tears down once, destroying the emitter only after cleanup finishes.
|
|
366
|
+
*
|
|
367
|
+
* @returns The stable destroy barrier
|
|
368
|
+
*/
|
|
314
369
|
destroy(): Promise<void>;
|
|
315
370
|
}
|
|
316
371
|
|
|
317
372
|
/**
|
|
318
|
-
*
|
|
373
|
+
* Represents the machine-readable queue failure categories.
|
|
319
374
|
*
|
|
320
375
|
* @example
|
|
321
376
|
* ```ts
|
|
@@ -325,10 +380,32 @@ export declare class Queue<TInput, TResult> implements QueueInterface<TInput, TR
|
|
|
325
380
|
export declare type QueueCode = 'invalid' | 'duplicate' | 'stopped' | 'aborted' | 'destroyed' | 'cleared' | 'timeout' | 'store' | 'cleanup';
|
|
326
381
|
|
|
327
382
|
/**
|
|
328
|
-
*
|
|
383
|
+
* Represents the per-attempt context a queue handler receives.
|
|
384
|
+
*
|
|
385
|
+
* @example
|
|
386
|
+
* ```ts
|
|
387
|
+
* const handler: QueueHandler<string, string> = (input, { id, signal }) =>
|
|
388
|
+
* signal.aborted ? id : input
|
|
389
|
+
* ```
|
|
390
|
+
*/
|
|
391
|
+
export declare interface QueueContext {
|
|
392
|
+
/**
|
|
393
|
+
* Holds the entry's stable id — equal across every attempt and across a crash-replay
|
|
394
|
+
* (`restore` re-runs an entry under its original id). Durable persistence is
|
|
395
|
+
* at-least-once (a crash between handler-success and the store's `remove`, or a
|
|
396
|
+
* failed `remove`, re-runs the entry), so use this id to make a handler idempotent
|
|
397
|
+
* — de-dup against it over the replay window.
|
|
398
|
+
*/
|
|
399
|
+
readonly id: string;
|
|
400
|
+
/** Fires on the per-attempt timeout, a queue-level abort, or the entry's own signal. */
|
|
401
|
+
readonly signal: AbortSignal;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Represents the per-entry options for `enqueue`.
|
|
329
406
|
*
|
|
330
407
|
* @remarks
|
|
331
|
-
* - `id` — a trace label for the entry
|
|
408
|
+
* - `id` — a trace label for the entry. Default: a random UUID.
|
|
332
409
|
* - `retries` — extra attempts after the first on failure (or a per-attempt
|
|
333
410
|
* timeout); overrides the queue default. A queue-level abort never retries.
|
|
334
411
|
* - `timeout` — the nonnegative integer per-attempt deadline in milliseconds, at most
|
|
@@ -349,7 +426,8 @@ export declare interface QueueEntryOptions {
|
|
|
349
426
|
}
|
|
350
427
|
|
|
351
428
|
/**
|
|
352
|
-
*
|
|
429
|
+
* Represents a queue failure carrying a lowercase machine-readable `code`, optional
|
|
430
|
+
* structured context, and an optional cause.
|
|
353
431
|
*
|
|
354
432
|
* @example
|
|
355
433
|
* ```ts
|
|
@@ -361,7 +439,7 @@ export declare class QueueError extends Error {
|
|
|
361
439
|
readonly code: QueueCode;
|
|
362
440
|
readonly context: QueueErrorContext | undefined;
|
|
363
441
|
/**
|
|
364
|
-
*
|
|
442
|
+
* Creates a queue error.
|
|
365
443
|
*
|
|
366
444
|
* @param message - Human-readable failure description
|
|
367
445
|
* @param options - Machine-readable category, optional context, and optional cause
|
|
@@ -370,7 +448,11 @@ export declare class QueueError extends Error {
|
|
|
370
448
|
}
|
|
371
449
|
|
|
372
450
|
/**
|
|
373
|
-
*
|
|
451
|
+
* Represents the structured context carried by a {@link QueueError}.
|
|
452
|
+
*
|
|
453
|
+
* @remarks
|
|
454
|
+
* `option` names the {@link QueueOption} the failure concerns, and `operation` names the store
|
|
455
|
+
* call that failed.
|
|
374
456
|
*
|
|
375
457
|
* @example
|
|
376
458
|
* ```ts
|
|
@@ -379,13 +461,13 @@ export declare class QueueError extends Error {
|
|
|
379
461
|
*/
|
|
380
462
|
export declare interface QueueErrorContext {
|
|
381
463
|
readonly id?: string;
|
|
382
|
-
readonly option?:
|
|
464
|
+
readonly option?: QueueOption;
|
|
383
465
|
readonly operation?: 'save' | 'remove' | 'load' | 'clear';
|
|
384
466
|
readonly value?: unknown;
|
|
385
467
|
}
|
|
386
468
|
|
|
387
469
|
/**
|
|
388
|
-
*
|
|
470
|
+
* Represents the construction options for a {@link QueueError}.
|
|
389
471
|
*
|
|
390
472
|
* @example
|
|
391
473
|
* ```ts
|
|
@@ -399,23 +481,23 @@ export declare interface QueueErrorOptions {
|
|
|
399
481
|
}
|
|
400
482
|
|
|
401
483
|
/**
|
|
402
|
-
*
|
|
403
|
-
* moments a fire-and-forget observer (logging, metrics, tracing) subscribes to,
|
|
404
|
-
*
|
|
484
|
+
* Represents the push observation surface of a {@link QueueInterface} — the lifecycle
|
|
485
|
+
* moments a fire-and-forget observer (logging, metrics, tracing) subscribes to, beside the
|
|
486
|
+
* per-entry `enqueue` promise.
|
|
405
487
|
*
|
|
406
488
|
* @typeParam TResult - The value an entry resolves (the `success` payload), mirroring the
|
|
407
489
|
* {@link QueueInterface}'s own `TResult` — so the map is `QueueEventMap<TResult>`.
|
|
408
490
|
*
|
|
409
491
|
* @remarks
|
|
410
|
-
* Listener isolation is the emitter's
|
|
411
|
-
* listener throw is routed to the emitter's
|
|
492
|
+
* Listener isolation is the emitter's: every event is emitted directly and a
|
|
493
|
+
* listener throw is routed to the emitter's own `error` handler (the `error` option), never
|
|
412
494
|
* onto this domain map and never into the cooperative wake-park / settle-once engine — so a
|
|
413
|
-
* 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
|
|
414
496
|
* the relevant wake / park / settle transition, so observation is purely a side-channel: a
|
|
415
|
-
* throwing observer cannot unbalance `active` or strand a parked worker. Subscribe
|
|
497
|
+
* throwing observer cannot unbalance `active` or strand a parked worker. Subscribe through
|
|
416
498
|
* `queue.emitter.on(...)`.
|
|
417
499
|
*
|
|
418
|
-
* Declared as a `type` alias (not `interface extends EventMap
|
|
500
|
+
* Declared as a `type` alias (not `interface extends EventMap` — `EventMap` is a
|
|
419
501
|
* `type` kind): a type-literal satisfies the `EventMap` constraint
|
|
420
502
|
* (`Record<string, readonly unknown[]>`) structurally, whereas an interface lacks the
|
|
421
503
|
* required index signature.
|
|
@@ -426,46 +508,24 @@ export declare interface QueueErrorOptions {
|
|
|
426
508
|
* ```
|
|
427
509
|
*/
|
|
428
510
|
export declare type QueueEventMap<TResult> = {
|
|
429
|
-
/**
|
|
511
|
+
/** Signals that an entry was accepted (and durably persisted, when a store is set) — its id. */
|
|
430
512
|
readonly enqueue: readonly [id: string];
|
|
431
|
-
/**
|
|
513
|
+
/** Signals that an attempt began running — the entry's id (after it was dequeued, in flight). */
|
|
432
514
|
readonly start: readonly [id: string];
|
|
433
|
-
/**
|
|
515
|
+
/** Signals that a failed attempt is being retried — the entry id + completed-attempt count. */
|
|
434
516
|
readonly retry: readonly [id: string, attempt: number];
|
|
435
|
-
/**
|
|
517
|
+
/** Signals that an entry settled successfully — its id + the resolved result. */
|
|
436
518
|
readonly success: readonly [id: string, result: TResult];
|
|
437
|
-
/**
|
|
519
|
+
/** Signals that an entry settled with a terminal failure — its id + the error (always `unknown`). */
|
|
438
520
|
readonly failure: readonly [id: string, error: unknown];
|
|
439
|
-
/**
|
|
521
|
+
/** Signals that the queue was aborted — its coded abort error. */
|
|
440
522
|
readonly abort: readonly [reason: unknown];
|
|
441
|
-
/**
|
|
523
|
+
/** Signals that the queue transitioned to no reserved live ids (drained). */
|
|
442
524
|
readonly drain: readonly [];
|
|
443
525
|
};
|
|
444
526
|
|
|
445
527
|
/**
|
|
446
|
-
*
|
|
447
|
-
*
|
|
448
|
-
* @example
|
|
449
|
-
* ```ts
|
|
450
|
-
* const handler: QueueHandler<string, string> = (input, { id, signal }) =>
|
|
451
|
-
* signal.aborted ? id : input
|
|
452
|
-
* ```
|
|
453
|
-
*/
|
|
454
|
-
export declare interface QueueExecution {
|
|
455
|
-
/**
|
|
456
|
-
* The entry's stable id — equal across every attempt and across a crash-replay
|
|
457
|
-
* (`restore` re-runs an entry under its original id). Durable persistence is
|
|
458
|
-
* at-least-once (a crash between handler-success and the store's `remove`, or a
|
|
459
|
-
* failed `remove`, re-runs the entry), so use this id to make a handler idempotent
|
|
460
|
-
* — de-dup against it over the replay window.
|
|
461
|
-
*/
|
|
462
|
-
readonly id: string;
|
|
463
|
-
/** Fires on the per-attempt timeout, a queue-level abort, or the entry's own signal. */
|
|
464
|
-
readonly signal: AbortSignal;
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
/**
|
|
468
|
-
* Runs one queued entry's work; may reject to trigger a retry.
|
|
528
|
+
* Runs one queued entry's work; a rejection triggers a retry while attempts remain.
|
|
469
529
|
*
|
|
470
530
|
* @typeParam TInput - The work input
|
|
471
531
|
* @typeParam TResult - The resolved work result
|
|
@@ -475,18 +535,19 @@ export declare interface QueueExecution {
|
|
|
475
535
|
* const handler: QueueHandler<number, number> = (input) => input * 2
|
|
476
536
|
* ```
|
|
477
537
|
*/
|
|
478
|
-
export declare type QueueHandler<TInput, TResult> = (input: TInput,
|
|
538
|
+
export declare type QueueHandler<TInput, TResult> = (input: TInput, context: QueueContext) => Promise<TResult> | TResult;
|
|
479
539
|
|
|
480
540
|
/**
|
|
481
|
-
*
|
|
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.
|
|
482
543
|
*
|
|
483
544
|
* @remarks
|
|
484
|
-
* Exposes a typed {@link emitter}
|
|
485
|
-
* ({@link QueueEventMap}) for fire-and-forget observers,
|
|
486
|
-
* Emitting is observation-only — every event fires
|
|
545
|
+
* Exposes a typed {@link emitter} carrying its lifecycle moments
|
|
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
|
|
487
548
|
* transition, so a buggy observer can never reorder or corrupt the wake-park / settle-once
|
|
488
549
|
* engine: the emitter isolates a listener throw and routes it to its `error` handler (the
|
|
489
|
-
* `error` option), never the engine. Subscribe
|
|
550
|
+
* `error` option), never the engine. Subscribe through `queue.emitter.on(...)`.
|
|
490
551
|
*
|
|
491
552
|
* @example
|
|
492
553
|
* ```ts
|
|
@@ -494,47 +555,62 @@ export declare type QueueHandler<TInput, TResult> = (input: TInput, execution: Q
|
|
|
494
555
|
* ```
|
|
495
556
|
*/
|
|
496
557
|
export declare interface QueueInterface<TInput, TResult> {
|
|
558
|
+
/** Holds the typed push observation surface carrying this queue's {@link QueueEventMap} moments. */
|
|
497
559
|
readonly emitter: EmitterInterface<QueueEventMap<TResult>>;
|
|
560
|
+
/** Counts the reserved live entries — admitting, pending, claimed, or awaiting cleanup. */
|
|
498
561
|
readonly count: number;
|
|
562
|
+
/** Counts the claimed entries in flight, never above the queue's concurrency. */
|
|
499
563
|
readonly active: number;
|
|
564
|
+
/** Reports the pause state: true while `pause` has suspended dequeuing; false otherwise. */
|
|
500
565
|
readonly paused: boolean;
|
|
566
|
+
/** Reports the halt state: true after `stop` or `abort` has halted the queue; false otherwise. */
|
|
501
567
|
readonly stopped: boolean;
|
|
502
|
-
/**
|
|
568
|
+
/** Reserves one FIFO entry, submits it, and wakes a worker; the returned promise settles when the entry finally settles. */
|
|
503
569
|
enqueue(input: TInput, options?: QueueEntryOptions): Promise<TResult>;
|
|
504
|
-
/** Re-
|
|
570
|
+
/** Re-enqueues the store's outstanding entries at their persisted attempt count so they run again; without a store it does nothing. */
|
|
505
571
|
restore(): Promise<void>;
|
|
506
|
-
/**
|
|
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. */
|
|
507
573
|
start(): void;
|
|
508
|
-
/**
|
|
574
|
+
/** Rejects non-active work and awaits durable removals plus current-loop quiescence, while in-flight entries settle normally. */
|
|
509
575
|
stop(): Promise<void>;
|
|
510
|
-
/**
|
|
576
|
+
/** Suspends dequeuing until `resume`, parking the workers while in-flight entries keep running. */
|
|
511
577
|
pause(): void;
|
|
512
|
-
/**
|
|
578
|
+
/** Continues a paused queue, waking the parked workers. */
|
|
513
579
|
resume(): void;
|
|
514
|
-
/**
|
|
580
|
+
/** Cancels active work through its signal, rejects pending work immediately, and awaits the owned persistence cleanup. */
|
|
515
581
|
abort(reason?: unknown): Promise<void>;
|
|
516
|
-
/**
|
|
582
|
+
/** Rejects non-active work immediately and awaits its durable removal, leaving active entries untouched. */
|
|
517
583
|
clear(): Promise<void>;
|
|
518
|
-
/**
|
|
584
|
+
/** Blocks admissions, aborts, awaits the cleanup, then destroys the emitter last; idempotent. */
|
|
519
585
|
destroy(): Promise<void>;
|
|
520
586
|
}
|
|
521
587
|
|
|
522
588
|
/**
|
|
523
|
-
*
|
|
589
|
+
* Represents the construction and per-entry option keys a queue validates.
|
|
590
|
+
*
|
|
591
|
+
* @example
|
|
592
|
+
* ```ts
|
|
593
|
+
* const option: QueueOption = 'concurrency'
|
|
594
|
+
* ```
|
|
595
|
+
*/
|
|
596
|
+
export declare type QueueOption = 'id' | 'concurrency' | 'retries' | 'timeout' | 'signal';
|
|
597
|
+
|
|
598
|
+
/**
|
|
599
|
+
* Represents the options for `createQueue`.
|
|
524
600
|
*
|
|
525
601
|
* @remarks
|
|
526
602
|
* - `handler` — runs each entry's work; rejecting triggers a retry while attempts
|
|
527
603
|
* remain.
|
|
528
|
-
* - `concurrency` — the maximum number of entries in flight at once
|
|
529
|
-
*
|
|
530
|
-
* - `retries` — the default extra attempts per entry on failure
|
|
531
|
-
*
|
|
532
|
-
* - `timeout` — the default per-attempt deadline in integer milliseconds
|
|
533
|
-
*
|
|
604
|
+
* - `concurrency` — the maximum number of entries in flight at once. Default: `1`.
|
|
605
|
+
* Must be a positive safe integer.
|
|
606
|
+
* - `retries` — the default extra attempts per entry on failure. Default: `0`.
|
|
607
|
+
* Must be a nonnegative safe integer.
|
|
608
|
+
* - `timeout` — the default per-attempt deadline in integer milliseconds. Default: `0`,
|
|
609
|
+
* which disables the deadline. Must be between `0` and `2_147_483_647`, inclusive.
|
|
534
610
|
* - `store` — durable backing; outstanding entries survive a restart; call
|
|
535
611
|
* `restore()` to re-run them.
|
|
536
|
-
* - `on` — the reserved {@link EmitterHooks} key
|
|
537
|
-
* {@link QueueEventMap}, wired at construction
|
|
612
|
+
* - `on` — the reserved {@link EmitterHooks} key: initial listeners for the queue's
|
|
613
|
+
* {@link QueueEventMap}, wired at construction, for example `{ drain: () => log('idle') }`.
|
|
538
614
|
*
|
|
539
615
|
* @example
|
|
540
616
|
* ```ts
|
|
@@ -543,7 +619,7 @@ export declare interface QueueInterface<TInput, TResult> {
|
|
|
543
619
|
*/
|
|
544
620
|
export declare interface QueueOptions<TInput, TResult> {
|
|
545
621
|
readonly on?: EmitterHooks<QueueEventMap<TResult>>;
|
|
546
|
-
/**
|
|
622
|
+
/** Holds the emitter's listener-error handler — a listener throw routes here, not to a domain event. */
|
|
547
623
|
readonly error?: EmitterErrorHandler;
|
|
548
624
|
readonly handler: QueueHandler<TInput, TResult>;
|
|
549
625
|
readonly concurrency?: number;
|
|
@@ -553,14 +629,15 @@ export declare interface QueueOptions<TInput, TResult> {
|
|
|
553
629
|
}
|
|
554
630
|
|
|
555
631
|
/**
|
|
556
|
-
*
|
|
632
|
+
* Represents the durable backing for a queue's outstanding entries — the small keyed
|
|
633
|
+
* surface a restart resumes from.
|
|
557
634
|
*
|
|
558
635
|
* @remarks
|
|
559
|
-
* The store holds
|
|
636
|
+
* The store holds only work that has not yet completed: `save` upserts an entry
|
|
560
637
|
* (by its `id`), `remove` drops a finished one, `load` returns everything
|
|
561
638
|
* outstanding (to restore a queue after a restart), and `clear` empties it. It is
|
|
562
|
-
* a minimal interface
|
|
563
|
-
* state is
|
|
639
|
+
* a minimal interface over the `@orkestrel/database` layer — a queue's durable
|
|
640
|
+
* state is a table — so any `DriverInterface` backend (memory, JSON, SQLite)
|
|
564
641
|
* persists it without the store knowing which.
|
|
565
642
|
*
|
|
566
643
|
* @typeParam TInput - The work input each {@link StoredEntry} carries
|
|
@@ -571,17 +648,42 @@ export declare interface QueueOptions<TInput, TResult> {
|
|
|
571
648
|
* ```
|
|
572
649
|
*/
|
|
573
650
|
export declare interface QueueStoreInterface<TInput> {
|
|
651
|
+
/** Upserts one entry by its `id`, overwriting an entry already stored under that id. */
|
|
574
652
|
save(entry: StoredEntry<TInput>): Promise<void>;
|
|
653
|
+
/** Drops one finished entry by `id`, doing nothing when the store holds no such id. */
|
|
575
654
|
remove(id: string): Promise<void>;
|
|
655
|
+
/** Returns every outstanding entry — the work a restart resumes. */
|
|
576
656
|
load(): Promise<ReadonlyArray<StoredEntry<TInput>>>;
|
|
657
|
+
/** Empties the store, dropping every outstanding entry. */
|
|
577
658
|
clear(): Promise<void>;
|
|
578
659
|
}
|
|
579
660
|
|
|
580
661
|
/**
|
|
581
|
-
*
|
|
662
|
+
* Reads one named option from a caller-supplied entry options object exactly once,
|
|
663
|
+
* containing a throwing getter as a coded failure.
|
|
664
|
+
*
|
|
665
|
+
* @remarks
|
|
666
|
+
* An absent options object reads nothing and yields `undefined`.
|
|
667
|
+
*
|
|
668
|
+
* @param options - The caller's own entry options, or `undefined`
|
|
669
|
+
* @param option - The option key to read
|
|
670
|
+
* @param message - The failure description the thrown error carries
|
|
671
|
+
* @returns The value the property held, or `undefined` when no options object was supplied
|
|
672
|
+
* @throws {QueueError} Thrown when reading the property throws, coded `invalid` with the
|
|
673
|
+
* option in its context.
|
|
674
|
+
*
|
|
675
|
+
* @example
|
|
676
|
+
* ```ts
|
|
677
|
+
* readOption({ retries: 2 }, 'retries', 'queue retries could not be read') // 2
|
|
678
|
+
* ```
|
|
679
|
+
*/
|
|
680
|
+
export declare function readOption(options: QueueEntryOptions | undefined, option: keyof QueueEntryOptions, message: string): unknown;
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* Represents a durably persisted, still-outstanding queue entry — re-run after a restart.
|
|
582
684
|
*
|
|
583
685
|
* @remarks
|
|
584
|
-
* The store holds only entries that have
|
|
686
|
+
* The store holds only entries that have not yet completed, so what `load`
|
|
585
687
|
* returns on startup is exactly the work to resume. `id` keys the entry (the
|
|
586
688
|
* store upserts by it); `input` is the handler's work payload (it must be
|
|
587
689
|
* JSON-serializable to survive a JSON / SQLite driver); `attempts` is how many
|
|
@@ -598,4 +700,23 @@ export declare interface StoredEntry<TInput> {
|
|
|
598
700
|
readonly attempts: number;
|
|
599
701
|
}
|
|
600
702
|
|
|
703
|
+
/**
|
|
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.
|
|
706
|
+
*
|
|
707
|
+
* @param value - The option value to check
|
|
708
|
+
* @param guard - The total guard the value must satisfy
|
|
709
|
+
* @param option - The option key the failure context reports
|
|
710
|
+
* @param message - The failure description the thrown error carries
|
|
711
|
+
* @returns The same value, narrowed by the guard
|
|
712
|
+
* @throws {QueueError} Thrown when the guard refuses the value, coded `invalid` with the
|
|
713
|
+
* option and the refused value in its context.
|
|
714
|
+
*
|
|
715
|
+
* @example
|
|
716
|
+
* ```ts
|
|
717
|
+
* validateOption(4, isQueueConcurrency, 'concurrency', 'queue concurrency must be positive') // 4
|
|
718
|
+
* ```
|
|
719
|
+
*/
|
|
720
|
+
export declare function validateOption<TValue>(value: unknown, guard: Guard<TValue>, option: QueueOption, message: string): TValue;
|
|
721
|
+
|
|
601
722
|
export { }
|