@orkestrel/queue 0.0.3 → 0.0.5
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 +810 -285
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +277 -133
- package/dist/src/core/index.d.ts +277 -133
- package/dist/src/core/index.js +805 -286
- package/dist/src/core/index.js.map +1 -1
- package/package.json +22 -19
|
@@ -28,7 +28,9 @@ import { TableInterface } from '@orkestrel/database';
|
|
|
28
28
|
*
|
|
29
29
|
* @example
|
|
30
30
|
* ```ts
|
|
31
|
-
* import {
|
|
31
|
+
* import { objectShape, stringShape } from '@orkestrel/contract'
|
|
32
|
+
* import { createMemoryDriver } from '@orkestrel/database'
|
|
33
|
+
* import { createDatabaseQueueStore } from '@orkestrel/queue'
|
|
32
34
|
*
|
|
33
35
|
* const store = createDatabaseQueueStore(
|
|
34
36
|
* objectShape({ url: stringShape(), label: stringShape() }),
|
|
@@ -44,33 +46,27 @@ export declare function createDatabaseQueueStore<TInput extends ContractShape>(i
|
|
|
44
46
|
* over a plain `Map` (the twin of {@link DatabaseQueueStore}).
|
|
45
47
|
*
|
|
46
48
|
* @remarks
|
|
47
|
-
* The
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* but a plain-`Map` store does not validate the payload — durability + contract narrowing
|
|
54
|
-
* are {@link DatabaseQueueStore}'s job. For durability across restarts, build the store
|
|
55
|
-
* over a server JSON / SQLite driver via {@link createDatabaseQueueStore} (or the server
|
|
56
|
-
* `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}.
|
|
57
55
|
*
|
|
58
56
|
* @typeParam TInput - The contract shape of each entry's `input` payload
|
|
59
|
-
* @param
|
|
60
|
-
*
|
|
61
|
-
* (AGENTS unused-arg convention) while staying in the signature for source-compatibility
|
|
62
|
-
* with the driver-backed family
|
|
63
|
-
* @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`
|
|
64
59
|
*
|
|
65
60
|
* @example
|
|
66
61
|
* ```ts
|
|
67
|
-
* import {
|
|
62
|
+
* import { stringShape } from '@orkestrel/contract'
|
|
63
|
+
* import { createMemoryQueueStore } from '@orkestrel/queue'
|
|
68
64
|
*
|
|
69
65
|
* const store = createMemoryQueueStore(stringShape())
|
|
70
66
|
* await store.save({ id: 'job-1', input: 'task', attempts: 0 })
|
|
71
67
|
* ```
|
|
72
68
|
*/
|
|
73
|
-
export declare function createMemoryQueueStore<TInput extends ContractShape>(
|
|
69
|
+
export declare function createMemoryQueueStore<TInput extends ContractShape>(input: TInput): QueueStoreInterface<Infer<TInput>>;
|
|
74
70
|
|
|
75
71
|
/**
|
|
76
72
|
* Create a concurrent, cooperative job queue — a bounded-concurrency engine that
|
|
@@ -94,7 +90,7 @@ export declare function createMemoryQueueStore<TInput extends ContractShape>(_in
|
|
|
94
90
|
*
|
|
95
91
|
* @example
|
|
96
92
|
* ```ts
|
|
97
|
-
* import { createQueue } from '@
|
|
93
|
+
* import { createQueue } from '@orkestrel/queue'
|
|
98
94
|
*
|
|
99
95
|
* const queue = createQueue<string, number>({
|
|
100
96
|
* handler: async (url, { signal }) => (await fetch(url, { signal })).status,
|
|
@@ -128,9 +124,11 @@ export declare function createQueue<TInput, TResult>(options: QueueOptions<TInpu
|
|
|
128
124
|
*
|
|
129
125
|
* @example
|
|
130
126
|
* ```ts
|
|
131
|
-
* import {
|
|
127
|
+
* import { stringShape } from '@orkestrel/contract'
|
|
128
|
+
* import { createMemoryDriver } from '@orkestrel/database'
|
|
129
|
+
* import { createDatabaseQueueStore } from '@orkestrel/queue'
|
|
132
130
|
*
|
|
133
|
-
* const store =
|
|
131
|
+
* const store = createDatabaseQueueStore(stringShape(), createMemoryDriver())
|
|
134
132
|
* await store.save({ id: 'job-1', input: 'https://example.com', attempts: 0 })
|
|
135
133
|
* const outstanding = await store.load() // readonly StoredEntry<string>[]
|
|
136
134
|
* ```
|
|
@@ -155,137 +153,176 @@ export declare class DatabaseQueueStore<TInput> implements QueueStoreInterface<T
|
|
|
155
153
|
}
|
|
156
154
|
|
|
157
155
|
/**
|
|
158
|
-
*
|
|
159
|
-
* {@link StoredEntry}s keyed by id, the DEFAULT store
|
|
160
|
-
* {@link import('../factories.js').createMemoryQueueStore} builds.
|
|
156
|
+
* Determine whether a value is a valid queue concurrency.
|
|
161
157
|
*
|
|
162
|
-
* @
|
|
163
|
-
*
|
|
164
|
-
* self-contained JSON, so no `databases` table / driver codec is needed for the memory
|
|
165
|
-
* tier; this is the zero-plumbing twin of {@link DatabaseQueueStore}, which wraps a
|
|
166
|
-
* `TableInterface` for a driver-pluggable durable backend). The store holds ONLY
|
|
167
|
-
* outstanding work, so `load` yields precisely the entries to resume — though a memory
|
|
168
|
-
* store is gone when the process exits, so `restore()` recovers nothing after a real
|
|
169
|
-
* restart. For durability across restarts, build a {@link DatabaseQueueStore} over a
|
|
170
|
-
* server JSON / SQLite driver (the SAME interface, so the queue / worker never knows).
|
|
171
|
-
*
|
|
172
|
-
* - **`save` upserts an entry under its OWN `id`** (a re-`save` of an existing `id`
|
|
173
|
-
* overwrites it — `Map.set`, never a duplicate).
|
|
174
|
-
* - **`remove` drops a finished entry by `id`**; an absent `id` is a no-op (no throw).
|
|
175
|
-
* - **`load` returns every outstanding entry** as a readonly array (the `Map`'s values
|
|
176
|
-
* in insertion order — the work to resume).
|
|
177
|
-
* - **`clear` empties the store** — drop every outstanding entry.
|
|
178
|
-
*
|
|
179
|
-
* Each primitive is async (it returns a resolved `Promise`) to satisfy the
|
|
180
|
-
* {@link QueueStoreInterface} signatures, wrapping the synchronous `Map` op — so the
|
|
181
|
-
* memory and driver-backed stores are interchangeable behind the one interface. The
|
|
182
|
-
* public surface is EXACTLY `save` / `remove` / `load` / `clear` — no extra members (the
|
|
183
|
-
* §22 method bijection with {@link QueueStoreInterface}).
|
|
158
|
+
* @param value - Value to inspect
|
|
159
|
+
* @returns Whether the value is a positive safe integer
|
|
184
160
|
*
|
|
185
|
-
* @
|
|
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`
|
|
186
174
|
*
|
|
187
175
|
* @example
|
|
188
176
|
* ```ts
|
|
189
|
-
*
|
|
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.
|
|
190
188
|
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
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
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
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
|
|
195
224
|
* ```
|
|
196
225
|
*/
|
|
197
|
-
export declare
|
|
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>> {
|
|
198
243
|
#private;
|
|
199
|
-
/**
|
|
200
|
-
|
|
201
|
-
|
|
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. */
|
|
202
253
|
remove(id: string): Promise<void>;
|
|
203
|
-
/**
|
|
204
|
-
load(): Promise<readonly StoredEntry<TInput
|
|
205
|
-
/**
|
|
254
|
+
/** Return fresh immutable snapshots of every outstanding entry. */
|
|
255
|
+
load(): Promise<readonly StoredEntry<Infer<TInput>>[]>;
|
|
256
|
+
/** Remove every outstanding entry. */
|
|
206
257
|
clear(): Promise<void>;
|
|
207
258
|
}
|
|
208
259
|
|
|
209
260
|
/**
|
|
210
|
-
* A concurrent, cooperative job queue.
|
|
261
|
+
* A concurrent, cooperative FIFO job queue with optional outstanding-work persistence.
|
|
211
262
|
*
|
|
212
|
-
* @
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
* entry waits for a worker to free up.
|
|
221
|
-
* - **Per-attempt deadline + cancellation.** Each attempt's `signal` (handed to the
|
|
222
|
-
* handler) fires on the per-attempt `Timeout` expiring, a queue-level `abort`, or
|
|
223
|
-
* the entry's own `signal`. The handler is RACED against that signal, so an attempt
|
|
224
|
-
* that ignores its `signal` still fails on the deadline. Built on the L1 `Abort` /
|
|
225
|
-
* `Timeout` primitives (a queue / entry abort CLEARS the deadline, never expires it).
|
|
226
|
-
* - **Retries.** A failed attempt (a handler rejection or a per-attempt timeout)
|
|
227
|
-
* retries while attempts remain (`retries` + 1 total). A queue-level abort NEVER
|
|
228
|
-
* retries — it rejects the entry at once; an entry-`signal` abort likewise.
|
|
229
|
-
* - **Lifecycle (§10).** `pause` parks workers; `resume` wakes them; `stop` ends the
|
|
230
|
-
* loops permanently and rejects pending (in-flight settle); `abort` fires the queue
|
|
231
|
-
* signal (cancelling in-flight attempts) and rejects pending; `clear` drops only
|
|
232
|
-
* pending; `destroy` aborts then stops, idempotently.
|
|
233
|
-
* - **Durability (optional `store`).** The store mirrors only OUTSTANDING work: an
|
|
234
|
-
* entry is `save`d on accept (and re-`save`d as its attempt count climbs) and
|
|
235
|
-
* `remove`d the moment it settles (success or terminal failure) or is drained by a
|
|
236
|
-
* lifecycle call. Accepting work durably is NOT best-effort — a failed initial `save`
|
|
237
|
-
* rejects the enqueue; per-attempt persistence IS best-effort (in-memory state is
|
|
238
|
-
* authoritative). A graceful shutdown empties the store; a crash leaves rows that
|
|
239
|
-
* `restore()` re-runs at their persisted attempt count (queue-default retries /
|
|
240
|
-
* timeout, no per-entry signal). At-least-once — handlers should be idempotent.
|
|
241
|
-
* - **Observable (§13).** The owned {@link emitter} ({@link QueueEventMap}) carries the
|
|
242
|
-
* lifecycle moments — `enqueue` / `start` / `retry` / `success` / `failure` / `abort` /
|
|
243
|
-
* `drain` — for fire-and-forget observers. Every event is emitted directly, strictly AFTER
|
|
244
|
-
* the relevant wake / park / settle transition; the emitter isolates a listener throw and
|
|
245
|
-
* routes it to its `error` handler (the `error` option), so a buggy observer can NEVER
|
|
246
|
-
* reorder, throw into, or corrupt the cooperative wake-park / settle-once engine: `active`
|
|
247
|
-
* stays balanced and no parked worker is stranded regardless of what a listener does.
|
|
248
|
-
* 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
|
+
* ```
|
|
249
271
|
*/
|
|
250
272
|
export declare class Queue<TInput, TResult> implements QueueInterface<TInput, TResult> {
|
|
251
273
|
#private;
|
|
274
|
+
/**
|
|
275
|
+
* Create a queue.
|
|
276
|
+
*
|
|
277
|
+
* @param options - Handler, validated execution limits, persistence, and observation hooks
|
|
278
|
+
*/
|
|
252
279
|
constructor(options: QueueOptions<TInput, TResult>);
|
|
253
280
|
get emitter(): EmitterInterface<QueueEventMap<TResult>>;
|
|
254
281
|
get count(): number;
|
|
255
282
|
get active(): number;
|
|
256
283
|
get paused(): boolean;
|
|
257
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
|
+
*/
|
|
258
293
|
enqueue(input: TInput, options?: QueueEntryOptions): Promise<TResult>;
|
|
294
|
+
/** Load and accept every outstanding stored entry from the current lifecycle generation. */
|
|
259
295
|
restore(): Promise<void>;
|
|
296
|
+
/** Begin or restart worker execution unless the queue is terminal. */
|
|
260
297
|
start(): void;
|
|
261
|
-
|
|
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. */
|
|
262
301
|
pause(): void;
|
|
302
|
+
/** Continue execution after a pause. */
|
|
263
303
|
resume(): void;
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
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>;
|
|
267
315
|
}
|
|
268
316
|
|
|
269
317
|
/**
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
* @remarks
|
|
273
|
-
* `id` keys the entry in the durable store; `attempts` is the STARTING attempt index — `0`
|
|
274
|
-
* for a fresh entry, the persisted count for one resumed by `restore`. Held only inside the
|
|
275
|
-
* {@link QueueInterface} engine (the in-flight pending list); not part of the public call
|
|
276
|
-
* surface, but centralized here per AGENTS §5 (an impl file holds only its class).
|
|
318
|
+
* Machine-readable queue failure categories.
|
|
277
319
|
*
|
|
278
|
-
* @
|
|
279
|
-
*
|
|
320
|
+
* @example
|
|
321
|
+
* ```ts
|
|
322
|
+
* const code: QueueCode = 'cleanup'
|
|
323
|
+
* ```
|
|
280
324
|
*/
|
|
281
|
-
export declare
|
|
282
|
-
readonly id: string;
|
|
283
|
-
readonly input: TInput;
|
|
284
|
-
readonly options: QueueEntryOptions | undefined;
|
|
285
|
-
readonly attempts: number;
|
|
286
|
-
readonly resolve: (value: TResult) => void;
|
|
287
|
-
readonly reject: (error: unknown) => void;
|
|
288
|
-
}
|
|
325
|
+
export declare type QueueCode = 'invalid' | 'duplicate' | 'stopped' | 'aborted' | 'destroyed' | 'cleared' | 'timeout' | 'store' | 'cleanup';
|
|
289
326
|
|
|
290
327
|
/**
|
|
291
328
|
* Per-entry options for `enqueue`.
|
|
@@ -294,10 +331,15 @@ export declare interface QueueEntry<TInput, TResult> {
|
|
|
294
331
|
* - `id` — a trace label for the entry; defaults to a random UUID.
|
|
295
332
|
* - `retries` — extra attempts after the first on failure (or a per-attempt
|
|
296
333
|
* timeout); overrides the queue default. A queue-level abort never retries.
|
|
297
|
-
* - `timeout` — the per-attempt deadline in milliseconds
|
|
298
|
-
*
|
|
334
|
+
* - `timeout` — the nonnegative per-attempt deadline in milliseconds, at most
|
|
335
|
+
* `2_147_483_647`; overrides the queue default. Zero means no deadline.
|
|
299
336
|
* - `signal` — an entry-scoped abort; once it fires the entry rejects (its
|
|
300
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
|
+
* ```
|
|
301
343
|
*/
|
|
302
344
|
export declare interface QueueEntryOptions {
|
|
303
345
|
readonly id?: string;
|
|
@@ -306,6 +348,56 @@ export declare interface QueueEntryOptions {
|
|
|
306
348
|
readonly signal?: AbortSignal;
|
|
307
349
|
}
|
|
308
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
|
+
|
|
309
401
|
/**
|
|
310
402
|
* The push observation surface of a {@link QueueInterface} (AGENTS §13) — the lifecycle
|
|
311
403
|
* moments a fire-and-forget observer (logging, metrics, tracing) subscribes to, ALONGSIDE
|
|
@@ -327,25 +419,38 @@ export declare interface QueueEntryOptions {
|
|
|
327
419
|
* `type` kind): a type-literal satisfies the `EventMap` constraint
|
|
328
420
|
* (`Record<string, readonly unknown[]>`) structurally, whereas an interface lacks the
|
|
329
421
|
* required index signature.
|
|
422
|
+
*
|
|
423
|
+
* @example
|
|
424
|
+
* ```ts
|
|
425
|
+
* const hooks: EmitterHooks<QueueEventMap<number>> = { success: (_id, result) => report(result) }
|
|
426
|
+
* ```
|
|
330
427
|
*/
|
|
331
428
|
export declare type QueueEventMap<TResult> = {
|
|
332
429
|
/** An entry was accepted (and durably persisted, when a store is set) — its id. */
|
|
333
430
|
readonly enqueue: readonly [id: string];
|
|
334
431
|
/** An attempt began running — the entry's id (after it was dequeued, in flight). */
|
|
335
432
|
readonly start: readonly [id: string];
|
|
336
|
-
/** A failed attempt is being retried — the entry
|
|
433
|
+
/** A failed attempt is being retried — the entry id + completed-attempt count. */
|
|
337
434
|
readonly retry: readonly [id: string, attempt: number];
|
|
338
435
|
/** An entry settled successfully — its id + the resolved result. */
|
|
339
436
|
readonly success: readonly [id: string, result: TResult];
|
|
340
437
|
/** An entry settled with a terminal failure — its id + the error (always `unknown`). */
|
|
341
438
|
readonly failure: readonly [id: string, error: unknown];
|
|
342
|
-
/** The queue was aborted —
|
|
439
|
+
/** The queue was aborted — its coded abort error. */
|
|
343
440
|
readonly abort: readonly [reason: unknown];
|
|
344
|
-
/** The queue
|
|
441
|
+
/** The queue transitioned to no reserved live ids (drained). */
|
|
345
442
|
readonly drain: readonly [];
|
|
346
443
|
};
|
|
347
444
|
|
|
348
|
-
/**
|
|
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
|
+
*/
|
|
349
454
|
export declare interface QueueExecution {
|
|
350
455
|
/**
|
|
351
456
|
* The entry's stable id — equal across every attempt and across a crash-replay
|
|
@@ -359,7 +464,17 @@ export declare interface QueueExecution {
|
|
|
359
464
|
readonly signal: AbortSignal;
|
|
360
465
|
}
|
|
361
466
|
|
|
362
|
-
/**
|
|
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
|
+
*/
|
|
363
478
|
export declare type QueueHandler<TInput, TResult> = (input: TInput, execution: QueueExecution) => Promise<TResult> | TResult;
|
|
364
479
|
|
|
365
480
|
/**
|
|
@@ -372,6 +487,11 @@ export declare type QueueHandler<TInput, TResult> = (input: TInput, execution: Q
|
|
|
372
487
|
* transition, so a buggy observer can never reorder or corrupt the wake-park / settle-once
|
|
373
488
|
* engine: the emitter isolates a listener throw and routes it to its `error` handler (the
|
|
374
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
|
+
* ```
|
|
375
495
|
*/
|
|
376
496
|
export declare interface QueueInterface<TInput, TResult> {
|
|
377
497
|
readonly emitter: EmitterInterface<QueueEventMap<TResult>>;
|
|
@@ -379,16 +499,24 @@ export declare interface QueueInterface<TInput, TResult> {
|
|
|
379
499
|
readonly active: number;
|
|
380
500
|
readonly paused: boolean;
|
|
381
501
|
readonly stopped: boolean;
|
|
502
|
+
/** Reserve and submit one FIFO entry. */
|
|
382
503
|
enqueue(input: TInput, options?: QueueEntryOptions): Promise<TResult>;
|
|
383
504
|
/** Re-enqueue outstanding entries loaded from the store; no-op without a store. */
|
|
384
505
|
restore(): Promise<void>;
|
|
506
|
+
/** Begin or restart worker execution. */
|
|
385
507
|
start(): void;
|
|
386
|
-
|
|
508
|
+
/** Reject non-active work and await current-loop/durable quiescence. */
|
|
509
|
+
stop(): Promise<void>;
|
|
510
|
+
/** Suspend new execution resumably. */
|
|
387
511
|
pause(): void;
|
|
512
|
+
/** Continue execution after a pause. */
|
|
388
513
|
resume(): void;
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
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>;
|
|
392
520
|
}
|
|
393
521
|
|
|
394
522
|
/**
|
|
@@ -398,14 +526,20 @@ export declare interface QueueInterface<TInput, TResult> {
|
|
|
398
526
|
* - `handler` — runs each entry's work; rejecting triggers a retry while attempts
|
|
399
527
|
* remain.
|
|
400
528
|
* - `concurrency` — the maximum number of entries in flight at once; defaults to
|
|
401
|
-
* `1`
|
|
402
|
-
* - `retries` — the default extra attempts per entry on failure; defaults to `0
|
|
403
|
-
*
|
|
404
|
-
*
|
|
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.
|
|
405
534
|
* - `store` — durable backing; outstanding entries survive a restart; call
|
|
406
535
|
* `restore()` to re-run them.
|
|
407
536
|
* - `on` — the reserved {@link EmitterHooks} key (§8): initial listeners for the queue's
|
|
408
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
|
+
* ```
|
|
409
543
|
*/
|
|
410
544
|
export declare interface QueueOptions<TInput, TResult> {
|
|
411
545
|
readonly on?: EmitterHooks<QueueEventMap<TResult>>;
|
|
@@ -430,6 +564,11 @@ export declare interface QueueOptions<TInput, TResult> {
|
|
|
430
564
|
* persists it without the store knowing which.
|
|
431
565
|
*
|
|
432
566
|
* @typeParam TInput - The work input each {@link StoredEntry} carries
|
|
567
|
+
*
|
|
568
|
+
* @example
|
|
569
|
+
* ```ts
|
|
570
|
+
* const store: QueueStoreInterface<string> = createMemoryQueueStore(stringShape())
|
|
571
|
+
* ```
|
|
433
572
|
*/
|
|
434
573
|
export declare interface QueueStoreInterface<TInput> {
|
|
435
574
|
save(entry: StoredEntry<TInput>): Promise<void>;
|
|
@@ -447,6 +586,11 @@ export declare interface QueueStoreInterface<TInput> {
|
|
|
447
586
|
* store upserts by it); `input` is the handler's work payload (it must be
|
|
448
587
|
* JSON-serializable to survive a JSON / SQLite driver); `attempts` is how many
|
|
449
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
|
+
* ```
|
|
450
594
|
*/
|
|
451
595
|
export declare interface StoredEntry<TInput> {
|
|
452
596
|
readonly id: string;
|