@orkestrel/queue 0.0.4 → 0.0.6
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/dist/src/core/index.cjs +784 -275
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +277 -149
- package/dist/src/core/index.d.ts +277 -149
- package/dist/src/core/index.js +779 -275
- package/dist/src/core/index.js.map +1 -1
- package/package.json +7 -7
package/dist/src/core/index.d.ts
CHANGED
|
@@ -6,22 +6,6 @@ import { EmitterInterface } from '@orkestrel/emitter';
|
|
|
6
6
|
import { Infer } from '@orkestrel/contract';
|
|
7
7
|
import { TableInterface } from '@orkestrel/database';
|
|
8
8
|
|
|
9
|
-
/**
|
|
10
|
-
* Create the queue failure for an aborted or expired attempt.
|
|
11
|
-
*
|
|
12
|
-
* @param expired - Whether the attempt deadline expired
|
|
13
|
-
* @returns The queue's documented attempt failure
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```ts
|
|
17
|
-
* import { createAttemptError } from '@src/core'
|
|
18
|
-
*
|
|
19
|
-
* createAttemptError(false).message // 'attempt aborted'
|
|
20
|
-
* createAttemptError(true).message // 'attempt timed out'
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
export declare function createAttemptError(expired: boolean): Error;
|
|
24
|
-
|
|
25
9
|
/**
|
|
26
10
|
* Create a {@link DatabaseQueueStore} over any {@link DriverInterface} — the durable,
|
|
27
11
|
* driver-pluggable backing for a queue's outstanding entries.
|
|
@@ -44,7 +28,9 @@ export declare function createAttemptError(expired: boolean): Error;
|
|
|
44
28
|
*
|
|
45
29
|
* @example
|
|
46
30
|
* ```ts
|
|
47
|
-
* import {
|
|
31
|
+
* import { objectShape, stringShape } from '@orkestrel/contract'
|
|
32
|
+
* import { createMemoryDriver } from '@orkestrel/database'
|
|
33
|
+
* import { createDatabaseQueueStore } from '@orkestrel/queue'
|
|
48
34
|
*
|
|
49
35
|
* const store = createDatabaseQueueStore(
|
|
50
36
|
* objectShape({ url: stringShape(), label: stringShape() }),
|
|
@@ -60,33 +46,27 @@ export declare function createDatabaseQueueStore<TInput extends ContractShape>(i
|
|
|
60
46
|
* over a plain `Map` (the twin of {@link DatabaseQueueStore}).
|
|
61
47
|
*
|
|
62
48
|
* @remarks
|
|
63
|
-
* The
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* but a plain-`Map` store does not validate the payload — durability + contract narrowing
|
|
70
|
-
* are {@link DatabaseQueueStore}'s job. For durability across restarts, build the store
|
|
71
|
-
* over a server JSON / SQLite driver via {@link createDatabaseQueueStore} (or the server
|
|
72
|
-
* `createJSONQueueStore`).
|
|
49
|
+
* The entries live in a process-lifetime `Map` and are gone when the process exits. The
|
|
50
|
+
* supplied shape is compiled and enforced for every save. Exact JSON inputs are cloned
|
|
51
|
+
* into deeply frozen owned snapshots, and each load returns fresh snapshots, so neither
|
|
52
|
+
* caller mutation nor a returned object can alias stored state. For durability across
|
|
53
|
+
* restarts, build the store over a persistent driver with
|
|
54
|
+
* {@link createDatabaseQueueStore}.
|
|
73
55
|
*
|
|
74
56
|
* @typeParam TInput - The contract shape of each entry's `input` payload
|
|
75
|
-
* @param
|
|
76
|
-
*
|
|
77
|
-
* (AGENTS unused-arg convention) while staying in the signature for source-compatibility
|
|
78
|
-
* with the driver-backed family
|
|
79
|
-
* @returns A memory-backed {@link QueueStoreInterface}, typed by `_input`
|
|
57
|
+
* @param input - The runtime {@link ContractShape} for the work payload
|
|
58
|
+
* @returns A validated memory-backed {@link QueueStoreInterface}, typed by `input`
|
|
80
59
|
*
|
|
81
60
|
* @example
|
|
82
61
|
* ```ts
|
|
83
|
-
* import {
|
|
62
|
+
* import { stringShape } from '@orkestrel/contract'
|
|
63
|
+
* import { createMemoryQueueStore } from '@orkestrel/queue'
|
|
84
64
|
*
|
|
85
65
|
* const store = createMemoryQueueStore(stringShape())
|
|
86
66
|
* await store.save({ id: 'job-1', input: 'task', attempts: 0 })
|
|
87
67
|
* ```
|
|
88
68
|
*/
|
|
89
|
-
export declare function createMemoryQueueStore<TInput extends ContractShape>(
|
|
69
|
+
export declare function createMemoryQueueStore<TInput extends ContractShape>(input: TInput): QueueStoreInterface<Infer<TInput>>;
|
|
90
70
|
|
|
91
71
|
/**
|
|
92
72
|
* Create a concurrent, cooperative job queue — a bounded-concurrency engine that
|
|
@@ -110,7 +90,7 @@ export declare function createMemoryQueueStore<TInput extends ContractShape>(_in
|
|
|
110
90
|
*
|
|
111
91
|
* @example
|
|
112
92
|
* ```ts
|
|
113
|
-
* import { createQueue } from '@
|
|
93
|
+
* import { createQueue } from '@orkestrel/queue'
|
|
114
94
|
*
|
|
115
95
|
* const queue = createQueue<string, number>({
|
|
116
96
|
* handler: async (url, { signal }) => (await fetch(url, { signal })).status,
|
|
@@ -144,9 +124,11 @@ export declare function createQueue<TInput, TResult>(options: QueueOptions<TInpu
|
|
|
144
124
|
*
|
|
145
125
|
* @example
|
|
146
126
|
* ```ts
|
|
147
|
-
* import {
|
|
127
|
+
* import { stringShape } from '@orkestrel/contract'
|
|
128
|
+
* import { createMemoryDriver } from '@orkestrel/database'
|
|
129
|
+
* import { createDatabaseQueueStore } from '@orkestrel/queue'
|
|
148
130
|
*
|
|
149
|
-
* const store =
|
|
131
|
+
* const store = createDatabaseQueueStore(stringShape(), createMemoryDriver())
|
|
150
132
|
* await store.save({ id: 'job-1', input: 'https://example.com', attempts: 0 })
|
|
151
133
|
* const outstanding = await store.load() // readonly StoredEntry<string>[]
|
|
152
134
|
* ```
|
|
@@ -171,137 +153,176 @@ export declare class DatabaseQueueStore<TInput> implements QueueStoreInterface<T
|
|
|
171
153
|
}
|
|
172
154
|
|
|
173
155
|
/**
|
|
174
|
-
*
|
|
175
|
-
* {@link StoredEntry}s keyed by id, the DEFAULT store
|
|
176
|
-
* {@link import('../factories.js').createMemoryQueueStore} builds.
|
|
156
|
+
* Determine whether a value is a valid queue concurrency.
|
|
177
157
|
*
|
|
178
|
-
* @
|
|
179
|
-
*
|
|
180
|
-
* self-contained JSON, so no `databases` table / driver codec is needed for the memory
|
|
181
|
-
* tier; this is the zero-plumbing twin of {@link DatabaseQueueStore}, which wraps a
|
|
182
|
-
* `TableInterface` for a driver-pluggable durable backend). The store holds ONLY
|
|
183
|
-
* outstanding work, so `load` yields precisely the entries to resume — though a memory
|
|
184
|
-
* store is gone when the process exits, so `restore()` recovers nothing after a real
|
|
185
|
-
* restart. For durability across restarts, build a {@link DatabaseQueueStore} over a
|
|
186
|
-
* server JSON / SQLite driver (the SAME interface, so the queue / worker never knows).
|
|
187
|
-
*
|
|
188
|
-
* - **`save` upserts an entry under its OWN `id`** (a re-`save` of an existing `id`
|
|
189
|
-
* overwrites it — `Map.set`, never a duplicate).
|
|
190
|
-
* - **`remove` drops a finished entry by `id`**; an absent `id` is a no-op (no throw).
|
|
191
|
-
* - **`load` returns every outstanding entry** as a readonly array (the `Map`'s values
|
|
192
|
-
* in insertion order — the work to resume).
|
|
193
|
-
* - **`clear` empties the store** — drop every outstanding entry.
|
|
194
|
-
*
|
|
195
|
-
* Each primitive is async (it returns a resolved `Promise`) to satisfy the
|
|
196
|
-
* {@link QueueStoreInterface} signatures, wrapping the synchronous `Map` op — so the
|
|
197
|
-
* memory and driver-backed stores are interchangeable behind the one interface. The
|
|
198
|
-
* public surface is EXACTLY `save` / `remove` / `load` / `clear` — no extra members (the
|
|
199
|
-
* §22 method bijection with {@link QueueStoreInterface}).
|
|
158
|
+
* @param value - Value to inspect
|
|
159
|
+
* @returns Whether the value is a positive safe integer
|
|
200
160
|
*
|
|
201
|
-
* @
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* isQueueConcurrency(4) // true
|
|
164
|
+
* isQueueConcurrency(0) // false
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
export declare function isQueueConcurrency(value: unknown): value is number;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Determine whether an unknown value is a {@link QueueError}.
|
|
171
|
+
*
|
|
172
|
+
* @param value - The value to inspect
|
|
173
|
+
* @returns `true` only for a `QueueError`; hostile values return `false`
|
|
202
174
|
*
|
|
203
175
|
* @example
|
|
204
176
|
* ```ts
|
|
205
|
-
*
|
|
177
|
+
* try {
|
|
178
|
+
* await queue.clear()
|
|
179
|
+
* } catch (error) {
|
|
180
|
+
* if (isQueueError(error) && error.code === 'cleanup') report(error.context)
|
|
181
|
+
* }
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
export declare function isQueueError(value: unknown): value is QueueError;
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Determine whether a value is a valid queue retry count.
|
|
206
188
|
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
189
|
+
* @param value - Value to inspect
|
|
190
|
+
* @returns Whether the value is a nonnegative safe integer
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```ts
|
|
194
|
+
* isQueueRetries(2) // true
|
|
195
|
+
* isQueueRetries(-1) // false
|
|
211
196
|
* ```
|
|
212
197
|
*/
|
|
213
|
-
export declare
|
|
198
|
+
export declare function isQueueRetries(value: unknown): value is number;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Determine whether a value is a native abort signal usable by the queue.
|
|
202
|
+
*
|
|
203
|
+
* @param value - Value to inspect
|
|
204
|
+
* @returns Whether the value carries the native `AbortSignal` internal slot
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* ```ts
|
|
208
|
+
* isQueueSignal(new AbortController().signal) // true
|
|
209
|
+
* isQueueSignal({}) // false
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
export declare function isQueueSignal(value: unknown): value is AbortSignal;
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Determine whether a value is a valid queue timeout.
|
|
216
|
+
*
|
|
217
|
+
* @param value - Value to inspect
|
|
218
|
+
* @returns Whether the value is within the native timer range, inclusive
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```ts
|
|
222
|
+
* isQueueTimeout(500) // true
|
|
223
|
+
* isQueueTimeout(2_147_483_648) // false
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
export declare function isQueueTimeout(value: unknown): value is number;
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* An in-memory store owning validated, immutable JSON snapshots of outstanding entries.
|
|
230
|
+
*
|
|
231
|
+
* @typeParam TInput - The contract shape for each stored input
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* ```ts
|
|
235
|
+
* import { stringShape } from '@orkestrel/contract'
|
|
236
|
+
* import { MemoryQueueStore } from '@orkestrel/queue'
|
|
237
|
+
*
|
|
238
|
+
* const store = new MemoryQueueStore(stringShape())
|
|
239
|
+
* await store.save({ id: 'job-1', input: 'task', attempts: 0 })
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
export declare class MemoryQueueStore<TInput extends ContractShape> implements QueueStoreInterface<Infer<TInput>> {
|
|
214
243
|
#private;
|
|
215
|
-
/**
|
|
216
|
-
|
|
217
|
-
|
|
244
|
+
/**
|
|
245
|
+
* Create an in-memory queue store.
|
|
246
|
+
*
|
|
247
|
+
* @param input - Runtime contract for each stored input
|
|
248
|
+
*/
|
|
249
|
+
constructor(input: TInput);
|
|
250
|
+
/** Upsert a validated, owned snapshot under the entry's id. */
|
|
251
|
+
save(entry: StoredEntry<Infer<TInput>>): Promise<void>;
|
|
252
|
+
/** Remove an entry; an absent id is a no-op. */
|
|
218
253
|
remove(id: string): Promise<void>;
|
|
219
|
-
/**
|
|
220
|
-
load(): Promise<readonly StoredEntry<TInput
|
|
221
|
-
/**
|
|
254
|
+
/** Return fresh immutable snapshots of every outstanding entry. */
|
|
255
|
+
load(): Promise<readonly StoredEntry<Infer<TInput>>[]>;
|
|
256
|
+
/** Remove every outstanding entry. */
|
|
222
257
|
clear(): Promise<void>;
|
|
223
258
|
}
|
|
224
259
|
|
|
225
260
|
/**
|
|
226
|
-
* A concurrent, cooperative job queue.
|
|
261
|
+
* A concurrent, cooperative FIFO job queue with optional outstanding-work persistence.
|
|
227
262
|
*
|
|
228
|
-
* @
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
* entry waits for a worker to free up.
|
|
237
|
-
* - **Per-attempt deadline + cancellation.** Each attempt's `signal` (handed to the
|
|
238
|
-
* handler) fires on the per-attempt `Timeout` expiring, a queue-level `abort`, or
|
|
239
|
-
* the entry's own `signal`. The handler is RACED against that signal, so an attempt
|
|
240
|
-
* that ignores its `signal` still fails on the deadline. Built on the L1 `Abort` /
|
|
241
|
-
* `Timeout` primitives (a queue / entry abort CLEARS the deadline, never expires it).
|
|
242
|
-
* - **Retries.** A failed attempt (a handler rejection or a per-attempt timeout)
|
|
243
|
-
* retries while attempts remain (`retries` + 1 total). A queue-level abort NEVER
|
|
244
|
-
* retries — it rejects the entry at once; an entry-`signal` abort likewise.
|
|
245
|
-
* - **Lifecycle (§10).** `pause` parks workers; `resume` wakes them; `stop` ends the
|
|
246
|
-
* loops permanently and rejects pending (in-flight settle); `abort` fires the queue
|
|
247
|
-
* signal (cancelling in-flight attempts) and rejects pending; `clear` drops only
|
|
248
|
-
* pending; `destroy` aborts then stops, idempotently.
|
|
249
|
-
* - **Durability (optional `store`).** The store mirrors only OUTSTANDING work: an
|
|
250
|
-
* entry is `save`d on accept (and re-`save`d as its attempt count climbs) and
|
|
251
|
-
* `remove`d the moment it settles (success or terminal failure) or is drained by a
|
|
252
|
-
* lifecycle call. Accepting work durably is NOT best-effort — a failed initial `save`
|
|
253
|
-
* rejects the enqueue; per-attempt persistence IS best-effort (in-memory state is
|
|
254
|
-
* authoritative). A graceful shutdown empties the store; a crash leaves rows that
|
|
255
|
-
* `restore()` re-runs at their persisted attempt count (queue-default retries /
|
|
256
|
-
* timeout, no per-entry signal). At-least-once — handlers should be idempotent.
|
|
257
|
-
* - **Observable (§13).** The owned {@link emitter} ({@link QueueEventMap}) carries the
|
|
258
|
-
* lifecycle moments — `enqueue` / `start` / `retry` / `success` / `failure` / `abort` /
|
|
259
|
-
* `drain` — for fire-and-forget observers. Every event is emitted directly, strictly AFTER
|
|
260
|
-
* the relevant wake / park / settle transition; the emitter isolates a listener throw and
|
|
261
|
-
* routes it to its `error` handler (the `error` option), so a buggy observer can NEVER
|
|
262
|
-
* reorder, throw into, or corrupt the cooperative wake-park / settle-once engine: `active`
|
|
263
|
-
* stays balanced and no parked worker is stranded regardless of what a listener does.
|
|
264
|
-
* Observation is purely a side-channel.
|
|
263
|
+
* @typeParam TInput - The work input each entry carries
|
|
264
|
+
* @typeParam TResult - The value each entry resolves
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```ts
|
|
268
|
+
* const queue = new Queue<number, number>({ handler: (input) => input * 2 })
|
|
269
|
+
* const result = await queue.enqueue(21)
|
|
270
|
+
* ```
|
|
265
271
|
*/
|
|
266
272
|
export declare class Queue<TInput, TResult> implements QueueInterface<TInput, TResult> {
|
|
267
273
|
#private;
|
|
274
|
+
/**
|
|
275
|
+
* Create a queue.
|
|
276
|
+
*
|
|
277
|
+
* @param options - Handler, validated execution limits, persistence, and observation hooks
|
|
278
|
+
*/
|
|
268
279
|
constructor(options: QueueOptions<TInput, TResult>);
|
|
269
280
|
get emitter(): EmitterInterface<QueueEventMap<TResult>>;
|
|
270
281
|
get count(): number;
|
|
271
282
|
get active(): number;
|
|
272
283
|
get paused(): boolean;
|
|
273
284
|
get stopped(): boolean;
|
|
285
|
+
/**
|
|
286
|
+
* Reserve and submit one FIFO entry.
|
|
287
|
+
*
|
|
288
|
+
* @param input - Handler input
|
|
289
|
+
* @param options - Optional id, retry/timeout overrides, and entry abort signal
|
|
290
|
+
* @returns The entry's settle-once execution promise
|
|
291
|
+
* @throws {QueueError} Synchronously when an option is inaccessible or invalid
|
|
292
|
+
*/
|
|
274
293
|
enqueue(input: TInput, options?: QueueEntryOptions): Promise<TResult>;
|
|
294
|
+
/** Load and accept every outstanding stored entry from the current lifecycle generation. */
|
|
275
295
|
restore(): Promise<void>;
|
|
296
|
+
/** Begin or restart worker execution unless the queue is terminal. */
|
|
276
297
|
start(): void;
|
|
277
|
-
|
|
298
|
+
/** Reject non-active work and await current-loop and durable cleanup quiescence. */
|
|
299
|
+
stop(): Promise<void>;
|
|
300
|
+
/** Suspend new execution while leaving active entries untouched. */
|
|
278
301
|
pause(): void;
|
|
302
|
+
/** Continue execution after a pause. */
|
|
279
303
|
resume(): void;
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
304
|
+
/**
|
|
305
|
+
* Cancel the queue and await owned cleanup.
|
|
306
|
+
*
|
|
307
|
+
* @param reason - Optional cause carried by the coded abort error
|
|
308
|
+
* @returns The stable abort barrier
|
|
309
|
+
*/
|
|
310
|
+
abort(reason?: unknown): Promise<void>;
|
|
311
|
+
/** Reject non-active work and await its durable cleanup. */
|
|
312
|
+
clear(): Promise<void>;
|
|
313
|
+
/** Tear down once, destroying the emitter only after cleanup finishes. */
|
|
314
|
+
destroy(): Promise<void>;
|
|
283
315
|
}
|
|
284
316
|
|
|
285
317
|
/**
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
* @remarks
|
|
289
|
-
* `id` keys the entry in the durable store; `attempts` is the STARTING attempt index — `0`
|
|
290
|
-
* for a fresh entry, the persisted count for one resumed by `restore`. Held only inside the
|
|
291
|
-
* {@link QueueInterface} engine (the in-flight pending list); not part of the public call
|
|
292
|
-
* surface, but centralized here per AGENTS §5 (an impl file holds only its class).
|
|
318
|
+
* Machine-readable queue failure categories.
|
|
293
319
|
*
|
|
294
|
-
* @
|
|
295
|
-
*
|
|
320
|
+
* @example
|
|
321
|
+
* ```ts
|
|
322
|
+
* const code: QueueCode = 'cleanup'
|
|
323
|
+
* ```
|
|
296
324
|
*/
|
|
297
|
-
export declare
|
|
298
|
-
readonly id: string;
|
|
299
|
-
readonly input: TInput;
|
|
300
|
-
readonly options: QueueEntryOptions | undefined;
|
|
301
|
-
readonly attempts: number;
|
|
302
|
-
readonly resolve: (value: TResult) => void;
|
|
303
|
-
readonly reject: (error: unknown) => void;
|
|
304
|
-
}
|
|
325
|
+
export declare type QueueCode = 'invalid' | 'duplicate' | 'stopped' | 'aborted' | 'destroyed' | 'cleared' | 'timeout' | 'store' | 'cleanup';
|
|
305
326
|
|
|
306
327
|
/**
|
|
307
328
|
* Per-entry options for `enqueue`.
|
|
@@ -310,10 +331,15 @@ export declare interface QueueEntry<TInput, TResult> {
|
|
|
310
331
|
* - `id` — a trace label for the entry; defaults to a random UUID.
|
|
311
332
|
* - `retries` — extra attempts after the first on failure (or a per-attempt
|
|
312
333
|
* timeout); overrides the queue default. A queue-level abort never retries.
|
|
313
|
-
* - `timeout` — the per-attempt deadline in milliseconds
|
|
314
|
-
*
|
|
334
|
+
* - `timeout` — the nonnegative per-attempt deadline in milliseconds, at most
|
|
335
|
+
* `2_147_483_647`; overrides the queue default. Zero means no deadline.
|
|
315
336
|
* - `signal` — an entry-scoped abort; once it fires the entry rejects (its
|
|
316
337
|
* in-flight attempt's `signal` fires too) and does not retry.
|
|
338
|
+
*
|
|
339
|
+
* @example
|
|
340
|
+
* ```ts
|
|
341
|
+
* const options: QueueEntryOptions = { id: 'job-1', retries: 2, timeout: 500 }
|
|
342
|
+
* ```
|
|
317
343
|
*/
|
|
318
344
|
export declare interface QueueEntryOptions {
|
|
319
345
|
readonly id?: string;
|
|
@@ -322,6 +348,56 @@ export declare interface QueueEntryOptions {
|
|
|
322
348
|
readonly signal?: AbortSignal;
|
|
323
349
|
}
|
|
324
350
|
|
|
351
|
+
/**
|
|
352
|
+
* Error carrying a machine-readable queue category and optional context.
|
|
353
|
+
*
|
|
354
|
+
* @example
|
|
355
|
+
* ```ts
|
|
356
|
+
* new QueueError('duplicate id', { code: 'duplicate', context: { id: 'job-1' } })
|
|
357
|
+
* ```
|
|
358
|
+
*/
|
|
359
|
+
export declare class QueueError extends Error {
|
|
360
|
+
readonly name = "QueueError";
|
|
361
|
+
readonly code: QueueCode;
|
|
362
|
+
readonly context: QueueErrorContext | undefined;
|
|
363
|
+
/**
|
|
364
|
+
* Create a queue error.
|
|
365
|
+
*
|
|
366
|
+
* @param message - Human-readable failure description
|
|
367
|
+
* @param options - Machine-readable category, optional context, and optional cause
|
|
368
|
+
*/
|
|
369
|
+
constructor(message: string, options: QueueErrorOptions);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Structured context carried by a {@link QueueError}.
|
|
374
|
+
*
|
|
375
|
+
* @example
|
|
376
|
+
* ```ts
|
|
377
|
+
* const context: QueueErrorContext = { id: 'job-1', operation: 'remove' }
|
|
378
|
+
* ```
|
|
379
|
+
*/
|
|
380
|
+
export declare interface QueueErrorContext {
|
|
381
|
+
readonly id?: string;
|
|
382
|
+
readonly option?: 'id' | 'concurrency' | 'retries' | 'timeout' | 'signal';
|
|
383
|
+
readonly operation?: 'save' | 'remove' | 'load' | 'clear';
|
|
384
|
+
readonly value?: unknown;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Construction options for a {@link QueueError}.
|
|
389
|
+
*
|
|
390
|
+
* @example
|
|
391
|
+
* ```ts
|
|
392
|
+
* const options: QueueErrorOptions = { code: 'invalid', context: { option: 'id' } }
|
|
393
|
+
* ```
|
|
394
|
+
*/
|
|
395
|
+
export declare interface QueueErrorOptions {
|
|
396
|
+
readonly code: QueueCode;
|
|
397
|
+
readonly context?: QueueErrorContext;
|
|
398
|
+
readonly cause?: unknown;
|
|
399
|
+
}
|
|
400
|
+
|
|
325
401
|
/**
|
|
326
402
|
* The push observation surface of a {@link QueueInterface} (AGENTS §13) — the lifecycle
|
|
327
403
|
* moments a fire-and-forget observer (logging, metrics, tracing) subscribes to, ALONGSIDE
|
|
@@ -343,25 +419,38 @@ export declare interface QueueEntryOptions {
|
|
|
343
419
|
* `type` kind): a type-literal satisfies the `EventMap` constraint
|
|
344
420
|
* (`Record<string, readonly unknown[]>`) structurally, whereas an interface lacks the
|
|
345
421
|
* required index signature.
|
|
422
|
+
*
|
|
423
|
+
* @example
|
|
424
|
+
* ```ts
|
|
425
|
+
* const hooks: EmitterHooks<QueueEventMap<number>> = { success: (_id, result) => report(result) }
|
|
426
|
+
* ```
|
|
346
427
|
*/
|
|
347
428
|
export declare type QueueEventMap<TResult> = {
|
|
348
429
|
/** An entry was accepted (and durably persisted, when a store is set) — its id. */
|
|
349
430
|
readonly enqueue: readonly [id: string];
|
|
350
431
|
/** An attempt began running — the entry's id (after it was dequeued, in flight). */
|
|
351
432
|
readonly start: readonly [id: string];
|
|
352
|
-
/** A failed attempt is being retried — the entry
|
|
433
|
+
/** A failed attempt is being retried — the entry id + completed-attempt count. */
|
|
353
434
|
readonly retry: readonly [id: string, attempt: number];
|
|
354
435
|
/** An entry settled successfully — its id + the resolved result. */
|
|
355
436
|
readonly success: readonly [id: string, result: TResult];
|
|
356
437
|
/** An entry settled with a terminal failure — its id + the error (always `unknown`). */
|
|
357
438
|
readonly failure: readonly [id: string, error: unknown];
|
|
358
|
-
/** The queue was aborted —
|
|
439
|
+
/** The queue was aborted — its coded abort error. */
|
|
359
440
|
readonly abort: readonly [reason: unknown];
|
|
360
|
-
/** The queue
|
|
441
|
+
/** The queue transitioned to no reserved live ids (drained). */
|
|
361
442
|
readonly drain: readonly [];
|
|
362
443
|
};
|
|
363
444
|
|
|
364
|
-
/**
|
|
445
|
+
/**
|
|
446
|
+
* The per-attempt execution handle a queue handler receives.
|
|
447
|
+
*
|
|
448
|
+
* @example
|
|
449
|
+
* ```ts
|
|
450
|
+
* const handler: QueueHandler<string, string> = (input, { id, signal }) =>
|
|
451
|
+
* signal.aborted ? id : input
|
|
452
|
+
* ```
|
|
453
|
+
*/
|
|
365
454
|
export declare interface QueueExecution {
|
|
366
455
|
/**
|
|
367
456
|
* The entry's stable id — equal across every attempt and across a crash-replay
|
|
@@ -375,7 +464,17 @@ export declare interface QueueExecution {
|
|
|
375
464
|
readonly signal: AbortSignal;
|
|
376
465
|
}
|
|
377
466
|
|
|
378
|
-
/**
|
|
467
|
+
/**
|
|
468
|
+
* Runs one queued entry's work; may reject to trigger a retry.
|
|
469
|
+
*
|
|
470
|
+
* @typeParam TInput - The work input
|
|
471
|
+
* @typeParam TResult - The resolved work result
|
|
472
|
+
*
|
|
473
|
+
* @example
|
|
474
|
+
* ```ts
|
|
475
|
+
* const handler: QueueHandler<number, number> = (input) => input * 2
|
|
476
|
+
* ```
|
|
477
|
+
*/
|
|
379
478
|
export declare type QueueHandler<TInput, TResult> = (input: TInput, execution: QueueExecution) => Promise<TResult> | TResult;
|
|
380
479
|
|
|
381
480
|
/**
|
|
@@ -388,6 +487,11 @@ export declare type QueueHandler<TInput, TResult> = (input: TInput, execution: Q
|
|
|
388
487
|
* transition, so a buggy observer can never reorder or corrupt the wake-park / settle-once
|
|
389
488
|
* engine: the emitter isolates a listener throw and routes it to its `error` handler (the
|
|
390
489
|
* `error` option), never the engine. Subscribe via `queue.emitter.on(...)`.
|
|
490
|
+
*
|
|
491
|
+
* @example
|
|
492
|
+
* ```ts
|
|
493
|
+
* const queue: QueueInterface<number, number> = createQueue({ handler: (input) => input })
|
|
494
|
+
* ```
|
|
391
495
|
*/
|
|
392
496
|
export declare interface QueueInterface<TInput, TResult> {
|
|
393
497
|
readonly emitter: EmitterInterface<QueueEventMap<TResult>>;
|
|
@@ -395,16 +499,24 @@ export declare interface QueueInterface<TInput, TResult> {
|
|
|
395
499
|
readonly active: number;
|
|
396
500
|
readonly paused: boolean;
|
|
397
501
|
readonly stopped: boolean;
|
|
502
|
+
/** Reserve and submit one FIFO entry. */
|
|
398
503
|
enqueue(input: TInput, options?: QueueEntryOptions): Promise<TResult>;
|
|
399
504
|
/** Re-enqueue outstanding entries loaded from the store; no-op without a store. */
|
|
400
505
|
restore(): Promise<void>;
|
|
506
|
+
/** Begin or restart worker execution. */
|
|
401
507
|
start(): void;
|
|
402
|
-
|
|
508
|
+
/** Reject non-active work and await current-loop/durable quiescence. */
|
|
509
|
+
stop(): Promise<void>;
|
|
510
|
+
/** Suspend new execution resumably. */
|
|
403
511
|
pause(): void;
|
|
512
|
+
/** Continue execution after a pause. */
|
|
404
513
|
resume(): void;
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
514
|
+
/** Cancel active work, reject pending work, and await cleanup. */
|
|
515
|
+
abort(reason?: unknown): Promise<void>;
|
|
516
|
+
/** Reject non-active work and await its durable cleanup. */
|
|
517
|
+
clear(): Promise<void>;
|
|
518
|
+
/** Tear down idempotently and destroy observation last. */
|
|
519
|
+
destroy(): Promise<void>;
|
|
408
520
|
}
|
|
409
521
|
|
|
410
522
|
/**
|
|
@@ -414,14 +526,20 @@ export declare interface QueueInterface<TInput, TResult> {
|
|
|
414
526
|
* - `handler` — runs each entry's work; rejecting triggers a retry while attempts
|
|
415
527
|
* remain.
|
|
416
528
|
* - `concurrency` — the maximum number of entries in flight at once; defaults to
|
|
417
|
-
* `1`
|
|
418
|
-
* - `retries` — the default extra attempts per entry on failure; defaults to `0
|
|
419
|
-
*
|
|
420
|
-
*
|
|
529
|
+
* `1` and must be a positive safe integer.
|
|
530
|
+
* - `retries` — the default extra attempts per entry on failure; defaults to `0`
|
|
531
|
+
* and must be a nonnegative safe integer.
|
|
532
|
+
* - `timeout` — the default per-attempt deadline in milliseconds; defaults to zero
|
|
533
|
+
* and must be between zero and `2_147_483_647`, inclusive.
|
|
421
534
|
* - `store` — durable backing; outstanding entries survive a restart; call
|
|
422
535
|
* `restore()` to re-run them.
|
|
423
536
|
* - `on` — the reserved {@link EmitterHooks} key (§8): initial listeners for the queue's
|
|
424
537
|
* {@link QueueEventMap}, wired at construction (e.g. `{ drain: () => log('idle') }`).
|
|
538
|
+
*
|
|
539
|
+
* @example
|
|
540
|
+
* ```ts
|
|
541
|
+
* const options: QueueOptions<number, number> = { handler: (input) => input, concurrency: 2 }
|
|
542
|
+
* ```
|
|
425
543
|
*/
|
|
426
544
|
export declare interface QueueOptions<TInput, TResult> {
|
|
427
545
|
readonly on?: EmitterHooks<QueueEventMap<TResult>>;
|
|
@@ -446,6 +564,11 @@ export declare interface QueueOptions<TInput, TResult> {
|
|
|
446
564
|
* persists it without the store knowing which.
|
|
447
565
|
*
|
|
448
566
|
* @typeParam TInput - The work input each {@link StoredEntry} carries
|
|
567
|
+
*
|
|
568
|
+
* @example
|
|
569
|
+
* ```ts
|
|
570
|
+
* const store: QueueStoreInterface<string> = createMemoryQueueStore(stringShape())
|
|
571
|
+
* ```
|
|
449
572
|
*/
|
|
450
573
|
export declare interface QueueStoreInterface<TInput> {
|
|
451
574
|
save(entry: StoredEntry<TInput>): Promise<void>;
|
|
@@ -463,6 +586,11 @@ export declare interface QueueStoreInterface<TInput> {
|
|
|
463
586
|
* store upserts by it); `input` is the handler's work payload (it must be
|
|
464
587
|
* JSON-serializable to survive a JSON / SQLite driver); `attempts` is how many
|
|
465
588
|
* times the entry has been tried so far.
|
|
589
|
+
*
|
|
590
|
+
* @example
|
|
591
|
+
* ```ts
|
|
592
|
+
* const entry: StoredEntry<string> = { id: 'job-1', input: 'task', attempts: 0 }
|
|
593
|
+
* ```
|
|
466
594
|
*/
|
|
467
595
|
export declare interface StoredEntry<TInput> {
|
|
468
596
|
readonly id: string;
|