@orkestrel/queue 0.0.1
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/LICENSE +21 -0
- package/README.md +59 -0
- package/dist/src/core/index.cjs +581 -0
- package/dist/src/core/index.cjs.map +1 -0
- package/dist/src/core/index.d.cts +457 -0
- package/dist/src/core/index.d.ts +457 -0
- package/dist/src/core/index.js +575 -0
- package/dist/src/core/index.js.map +1 -0
- package/package.json +87 -0
|
@@ -0,0 +1,457 @@
|
|
|
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 { Infer } from '@orkestrel/contract';
|
|
7
|
+
import { TableInterface } from '@orkestrel/database';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Create a {@link DatabaseQueueStore} over any {@link DriverInterface} — the durable,
|
|
11
|
+
* driver-pluggable backing for a queue's outstanding entries.
|
|
12
|
+
*
|
|
13
|
+
* @remarks
|
|
14
|
+
* Builds a one-table database (`entries`, keyed by `id`) over the supplied driver, with
|
|
15
|
+
* the fixed `{ id; input; attempts }` column map — `input` is the caller's own shape,
|
|
16
|
+
* so the stored entry's payload is validated and typed by it. Reads narrow through that
|
|
17
|
+
* contract, so the store's `load` returns typed `StoredEntry<Infer<TInput>>[]` with no
|
|
18
|
+
* cast. Pass `createMemoryDriver()` for an ephemeral store, a server `createJSONDriver` /
|
|
19
|
+
* `createSQLiteDriver` for a persistent one — the durability is the driver's job, the
|
|
20
|
+
* store engine is shared. The `input` shape must be JSON-serializable to survive a JSON
|
|
21
|
+
* or SQLite driver.
|
|
22
|
+
*
|
|
23
|
+
* @typeParam TInput - The contract shape of each entry's `input` payload
|
|
24
|
+
* @param input - The {@link ContractShape} for the work payload (used directly as the
|
|
25
|
+
* `input` column)
|
|
26
|
+
* @param driver - The storage backend the entries persist to
|
|
27
|
+
* @returns A {@link QueueStoreInterface} over the driver, typed by `input`
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { createDatabaseQueueStore, createMemoryDriver, objectShape, stringShape } from '@src/core'
|
|
32
|
+
*
|
|
33
|
+
* const store = createDatabaseQueueStore(
|
|
34
|
+
* objectShape({ url: stringShape(), label: stringShape() }),
|
|
35
|
+
* createMemoryDriver(),
|
|
36
|
+
* )
|
|
37
|
+
* await store.save({ id: 'job-1', input: { url: 'https://example.com', label: 'home' }, attempts: 0 })
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare function createDatabaseQueueStore<TInput extends ContractShape>(input: TInput, driver: DriverInterface): QueueStoreInterface<Infer<TInput>>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Create an in-memory {@link MemoryQueueStore} — the zero-plumbing DEFAULT queue store
|
|
44
|
+
* over a plain `Map` (the twin of {@link DatabaseQueueStore}).
|
|
45
|
+
*
|
|
46
|
+
* @remarks
|
|
47
|
+
* The convenience for a non-persistent queue store (tests, ephemeral queues): the entries
|
|
48
|
+
* live in a process-lifetime `Map` and are gone when the process exits. UNLIKE
|
|
49
|
+
* {@link createDatabaseQueueStore}, no `databases` table / driver codec is built — a queue
|
|
50
|
+
* entry is already pure JSON, so the memory tier needs no encoding (AGENTS §21). The
|
|
51
|
+
* `input` shape is accepted for SOURCE-COMPATIBILITY with the driver-backed family (every
|
|
52
|
+
* call site passes its payload shape, and the store is still typed by it through `Infer`),
|
|
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`).
|
|
57
|
+
*
|
|
58
|
+
* @typeParam TInput - The contract shape of each entry's `input` payload
|
|
59
|
+
* @param _input - The {@link ContractShape} for the work payload — types the store (via
|
|
60
|
+
* `Infer`); the plain-`Map` backing does not use it at runtime, so it is `_`-prefixed
|
|
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`
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* import { createMemoryQueueStore, stringShape } from '@src/core'
|
|
68
|
+
*
|
|
69
|
+
* const store = createMemoryQueueStore(stringShape())
|
|
70
|
+
* await store.save({ id: 'job-1', input: 'task', attempts: 0 })
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export declare function createMemoryQueueStore<TInput extends ContractShape>(_input: TInput): QueueStoreInterface<Infer<TInput>>;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Create a concurrent, cooperative job queue — a bounded-concurrency engine that
|
|
77
|
+
* runs each enqueued input through a handler, with retries and a per-attempt
|
|
78
|
+
* timeout / abort, all over the L1 `Abort` / `Timeout` primitives.
|
|
79
|
+
*
|
|
80
|
+
* @remarks
|
|
81
|
+
* Worker loops PARK on a wake list when no entry is ready and are woken one-at-a-time
|
|
82
|
+
* by `enqueue` / `resume`, so an idle queue consumes no CPU (no polling). `enqueue`
|
|
83
|
+
* returns a promise that settles when the entry finally completes, fails (after its
|
|
84
|
+
* retries), or is rejected by an abort. A queue-level `abort` never retries; the
|
|
85
|
+
* lifecycle (`start` / `stop` / `pause` / `resume` / `abort` / `clear` / `destroy`)
|
|
86
|
+
* follows AGENTS §10. The queue is observable (§13): a typed `emitter` surfaces
|
|
87
|
+
* `enqueue` / `start` / `retry` / `success` / `failure` / `abort` / `drain`.
|
|
88
|
+
*
|
|
89
|
+
* @typeParam TInput - The work input each entry carries
|
|
90
|
+
* @typeParam TResult - The value the handler resolves for an entry
|
|
91
|
+
* @param options - The `handler` plus optional `concurrency` (default `1`),
|
|
92
|
+
* `retries` (default `0`), and a default per-attempt `timeout` in milliseconds
|
|
93
|
+
* @returns A working {@link QueueInterface}
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* import { createQueue } from '@src/core'
|
|
98
|
+
*
|
|
99
|
+
* const queue = createQueue<string, number>({
|
|
100
|
+
* handler: async (url, { signal }) => (await fetch(url, { signal })).status,
|
|
101
|
+
* concurrency: 4,
|
|
102
|
+
* retries: 2,
|
|
103
|
+
* timeout: 5_000,
|
|
104
|
+
* })
|
|
105
|
+
*
|
|
106
|
+
* const status = await queue.enqueue('https://example.com')
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export declare function createQueue<TInput, TResult>(options: QueueOptions<TInput, TResult>): QueueInterface<TInput, TResult>;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* A {@link QueueStoreInterface} backed by one table of the `databases` layer — a
|
|
113
|
+
* queue's durable state IS a table, so persistence reduces to keyed CRUD over a
|
|
114
|
+
* `TableInterface`.
|
|
115
|
+
*
|
|
116
|
+
* @remarks
|
|
117
|
+
* The store is driver-agnostic: it holds a single {@link TableInterface} whose
|
|
118
|
+
* backend (memory, JSON, SQLite) is chosen by whoever builds it (the factories).
|
|
119
|
+
* `save` upserts an entry by its primary key (`set`), `remove` drops one (`remove`),
|
|
120
|
+
* `load` returns every outstanding entry (`records` with no criteria), and `clear`
|
|
121
|
+
* empties the table. Reads are narrowed through the table's contract, so `load`
|
|
122
|
+
* returns typed {@link StoredEntry}`<TInput>[]` with no cast (AGENTS §1) — the table
|
|
123
|
+
* is created over the `{ id; input; attempts }` column map, which `Infer`s to exactly
|
|
124
|
+
* that row. The store holds only OUTSTANDING work (completed entries are `remove`d), so
|
|
125
|
+
* `load` on startup yields precisely the entries to resume.
|
|
126
|
+
*
|
|
127
|
+
* @typeParam TInput - The work input each {@link StoredEntry} carries
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* import { createMemoryQueueStore, stringShape } from '@src/core'
|
|
132
|
+
*
|
|
133
|
+
* const store = createMemoryQueueStore(stringShape())
|
|
134
|
+
* await store.save({ id: 'job-1', input: 'https://example.com', attempts: 0 })
|
|
135
|
+
* const outstanding = await store.load() // readonly StoredEntry<string>[]
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
export declare class DatabaseQueueStore<TInput> implements QueueStoreInterface<TInput> {
|
|
139
|
+
#private;
|
|
140
|
+
/**
|
|
141
|
+
* Wrap a table as a queue store.
|
|
142
|
+
*
|
|
143
|
+
* @param table - The {@link TableInterface} holding the outstanding entries — its
|
|
144
|
+
* row is a {@link StoredEntry}`<TInput>` (the `{ id; input; attempts }` shape)
|
|
145
|
+
*/
|
|
146
|
+
constructor(table: TableInterface<StoredEntry<TInput>>);
|
|
147
|
+
/** Upsert an entry by its `id` (a re-`save` of an existing `id` overwrites it). */
|
|
148
|
+
save(entry: StoredEntry<TInput>): Promise<void>;
|
|
149
|
+
/** Drop a finished entry by `id`; absent is a no-op (no throw). */
|
|
150
|
+
remove(id: string): Promise<void>;
|
|
151
|
+
/** Every outstanding entry — the work to resume after a restart. */
|
|
152
|
+
load(): Promise<readonly StoredEntry<TInput>[]>;
|
|
153
|
+
/** Empty the store — drop every outstanding entry. */
|
|
154
|
+
clear(): Promise<void>;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The in-memory {@link QueueStoreInterface} — a process-lifetime `Map` of
|
|
159
|
+
* {@link StoredEntry}s keyed by id, the DEFAULT store
|
|
160
|
+
* {@link import('../factories.js').createMemoryQueueStore} builds.
|
|
161
|
+
*
|
|
162
|
+
* @remarks
|
|
163
|
+
* A plain `Map<string, StoredEntry<TInput>>` (AGENTS §21 — the entry is already pure,
|
|
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}).
|
|
184
|
+
*
|
|
185
|
+
* @typeParam TInput - The work input each {@link StoredEntry} carries
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```ts
|
|
189
|
+
* import { createMemoryQueueStore, stringShape } from '@src/core'
|
|
190
|
+
*
|
|
191
|
+
* const store = createMemoryQueueStore(stringShape())
|
|
192
|
+
* await store.save({ id: 'job-1', input: 'https://example.com', attempts: 0 })
|
|
193
|
+
* const outstanding = await store.load() // readonly StoredEntry<string>[]
|
|
194
|
+
* await store.remove('job-1') // a finished entry leaves the store
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
export declare class MemoryQueueStore<TInput> implements QueueStoreInterface<TInput> {
|
|
198
|
+
#private;
|
|
199
|
+
/** Upsert an entry under its OWN `id` (a re-`save` of an existing `id` overwrites it). */
|
|
200
|
+
save(entry: StoredEntry<TInput>): Promise<void>;
|
|
201
|
+
/** Drop a finished entry by `id`; `Map.delete` of an absent id is already a no-op (no throw). */
|
|
202
|
+
remove(id: string): Promise<void>;
|
|
203
|
+
/** Every outstanding entry — the work to resume (the `Map`'s values, insertion order). */
|
|
204
|
+
load(): Promise<readonly StoredEntry<TInput>[]>;
|
|
205
|
+
/** Empty the store — drop every outstanding entry. */
|
|
206
|
+
clear(): Promise<void>;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* A concurrent, cooperative job queue.
|
|
211
|
+
*
|
|
212
|
+
* @remarks
|
|
213
|
+
* - **Cooperative wake-park loop.** Up to `concurrency` worker loops run; each
|
|
214
|
+
* takes the next pending entry, or — when none is ready (empty or paused) —
|
|
215
|
+
* PARKS by awaiting a fresh promise whose resolver is held in a wake list.
|
|
216
|
+
* `enqueue` / `resume` resolve one parked resolver to wake exactly one worker,
|
|
217
|
+
* so idle workers consume no CPU (no busy-poll, no timers, no recursion).
|
|
218
|
+
* - **FIFO + bounded concurrency.** Entries run in enqueue order; at most
|
|
219
|
+
* `concurrency` are in flight at once (default `1` = strictly ordered). A surplus
|
|
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.
|
|
249
|
+
*/
|
|
250
|
+
export declare class Queue<TInput, TResult> implements QueueInterface<TInput, TResult> {
|
|
251
|
+
#private;
|
|
252
|
+
constructor(options: QueueOptions<TInput, TResult>);
|
|
253
|
+
get emitter(): EmitterInterface<QueueEventMap<TResult>>;
|
|
254
|
+
get count(): number;
|
|
255
|
+
get active(): number;
|
|
256
|
+
get paused(): boolean;
|
|
257
|
+
get stopped(): boolean;
|
|
258
|
+
enqueue(input: TInput, options?: QueueEntryOptions): Promise<TResult>;
|
|
259
|
+
restore(): Promise<void>;
|
|
260
|
+
start(): void;
|
|
261
|
+
stop(): void;
|
|
262
|
+
pause(): void;
|
|
263
|
+
resume(): void;
|
|
264
|
+
abort(reason?: unknown): void;
|
|
265
|
+
clear(): void;
|
|
266
|
+
destroy(): void;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* One queued unit of work, plus the resolvers of the promise `enqueue` returned.
|
|
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).
|
|
277
|
+
*
|
|
278
|
+
* @typeParam TInput - The work payload the entry carries
|
|
279
|
+
* @typeParam TResult - The value the entry resolves
|
|
280
|
+
*/
|
|
281
|
+
export declare interface QueueEntry<TInput, TResult> {
|
|
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
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Per-entry options for `enqueue`.
|
|
292
|
+
*
|
|
293
|
+
* @remarks
|
|
294
|
+
* - `id` — a trace label for the entry; defaults to a random UUID.
|
|
295
|
+
* - `retries` — extra attempts after the first on failure (or a per-attempt
|
|
296
|
+
* timeout); overrides the queue default. A queue-level abort never retries.
|
|
297
|
+
* - `timeout` — the per-attempt deadline in milliseconds; overrides the queue
|
|
298
|
+
* default. A non-positive value (or no default) means no deadline.
|
|
299
|
+
* - `signal` — an entry-scoped abort; once it fires the entry rejects (its
|
|
300
|
+
* in-flight attempt's `signal` fires too) and does not retry.
|
|
301
|
+
*/
|
|
302
|
+
export declare interface QueueEntryOptions {
|
|
303
|
+
readonly id?: string;
|
|
304
|
+
readonly retries?: number;
|
|
305
|
+
readonly timeout?: number;
|
|
306
|
+
readonly signal?: AbortSignal;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* The push observation surface of a {@link QueueInterface} (AGENTS §13) — the lifecycle
|
|
311
|
+
* moments a fire-and-forget observer (logging, metrics, tracing) subscribes to, ALONGSIDE
|
|
312
|
+
* the per-entry `enqueue` promise.
|
|
313
|
+
*
|
|
314
|
+
* @typeParam TResult - The value an entry resolves (the `success` payload), mirroring the
|
|
315
|
+
* {@link QueueInterface}'s own `TResult` — so the map is `QueueEventMap<TResult>`.
|
|
316
|
+
*
|
|
317
|
+
* @remarks
|
|
318
|
+
* Listener isolation is the emitter's (AGENTS §13): every event is emitted directly and a
|
|
319
|
+
* listener throw is routed to the emitter's OWN `error` handler (the `error` option), never
|
|
320
|
+
* onto this domain map and never into the cooperative wake-park / settle-once engine — so a
|
|
321
|
+
* buggy observer can never reorder, throw into, or corrupt the queue. Every emit sits AFTER
|
|
322
|
+
* the relevant wake / park / settle transition, so observation is purely a side-channel: a
|
|
323
|
+
* throwing observer cannot unbalance `active` or strand a parked worker. Subscribe via
|
|
324
|
+
* `queue.emitter.on(...)`.
|
|
325
|
+
*
|
|
326
|
+
* Declared as a `type` alias (not `interface extends EventMap`, §4.5 — `EventMap` is a
|
|
327
|
+
* `type` kind): a type-literal satisfies the `EventMap` constraint
|
|
328
|
+
* (`Record<string, readonly unknown[]>`) structurally, whereas an interface lacks the
|
|
329
|
+
* required index signature.
|
|
330
|
+
*/
|
|
331
|
+
export declare type QueueEventMap<TResult> = {
|
|
332
|
+
/** An entry was accepted (and durably persisted, when a store is set) — its id. */
|
|
333
|
+
readonly enqueue: readonly [id: string];
|
|
334
|
+
/** An attempt began running — the entry's id (after it was dequeued, in flight). */
|
|
335
|
+
readonly start: readonly [id: string];
|
|
336
|
+
/** A failed attempt is being retried — the entry's id + the next (1-based) attempt index. */
|
|
337
|
+
readonly retry: readonly [id: string, attempt: number];
|
|
338
|
+
/** An entry settled successfully — its id + the resolved result. */
|
|
339
|
+
readonly success: readonly [id: string, result: TResult];
|
|
340
|
+
/** An entry settled with a terminal failure — its id + the error (always `unknown`). */
|
|
341
|
+
readonly failure: readonly [id: string, error: unknown];
|
|
342
|
+
/** The queue was aborted — the cancel reason. */
|
|
343
|
+
readonly abort: readonly [reason: unknown];
|
|
344
|
+
/** The queue went idle — no pending entries and none in flight (drained). */
|
|
345
|
+
readonly drain: readonly [];
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
/** The per-attempt execution handle a queue handler receives. */
|
|
349
|
+
export declare interface QueueExecution {
|
|
350
|
+
/**
|
|
351
|
+
* The entry's stable id — equal across every attempt and across a crash-replay
|
|
352
|
+
* (`restore` re-runs an entry under its original id). Durable persistence is
|
|
353
|
+
* at-least-once (a crash between handler-success and the store's `remove`, or a
|
|
354
|
+
* failed `remove`, re-runs the entry), so use this id to make a handler idempotent
|
|
355
|
+
* — de-dup against it over the replay window.
|
|
356
|
+
*/
|
|
357
|
+
readonly id: string;
|
|
358
|
+
/** Fires on the per-attempt timeout, a queue-level abort, or the entry's own signal. */
|
|
359
|
+
readonly signal: AbortSignal;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** Runs one queued entry's work; may reject to trigger a retry. */
|
|
363
|
+
export declare type QueueHandler<TInput, TResult> = (input: TInput, execution: QueueExecution) => Promise<TResult> | TResult;
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* A concurrent, cooperative job queue.
|
|
367
|
+
*
|
|
368
|
+
* @remarks
|
|
369
|
+
* Exposes a typed {@link emitter} (AGENTS §13) carrying its lifecycle moments
|
|
370
|
+
* ({@link QueueEventMap}) for fire-and-forget observers, ALONGSIDE each `enqueue` promise.
|
|
371
|
+
* Emitting is observation-only — every event fires AFTER the relevant wake / park / settle
|
|
372
|
+
* transition, so a buggy observer can never reorder or corrupt the wake-park / settle-once
|
|
373
|
+
* engine: the emitter isolates a listener throw and routes it to its `error` handler (the
|
|
374
|
+
* `error` option), never the engine. Subscribe via `queue.emitter.on(...)`.
|
|
375
|
+
*/
|
|
376
|
+
export declare interface QueueInterface<TInput, TResult> {
|
|
377
|
+
readonly emitter: EmitterInterface<QueueEventMap<TResult>>;
|
|
378
|
+
readonly count: number;
|
|
379
|
+
readonly active: number;
|
|
380
|
+
readonly paused: boolean;
|
|
381
|
+
readonly stopped: boolean;
|
|
382
|
+
enqueue(input: TInput, options?: QueueEntryOptions): Promise<TResult>;
|
|
383
|
+
/** Re-enqueue outstanding entries loaded from the store; no-op without a store. */
|
|
384
|
+
restore(): Promise<void>;
|
|
385
|
+
start(): void;
|
|
386
|
+
stop(): void;
|
|
387
|
+
pause(): void;
|
|
388
|
+
resume(): void;
|
|
389
|
+
abort(reason?: unknown): void;
|
|
390
|
+
clear(): void;
|
|
391
|
+
destroy(): void;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Options for `createQueue`.
|
|
396
|
+
*
|
|
397
|
+
* @remarks
|
|
398
|
+
* - `handler` — runs each entry's work; rejecting triggers a retry while attempts
|
|
399
|
+
* remain.
|
|
400
|
+
* - `concurrency` — the maximum number of entries in flight at once; defaults to
|
|
401
|
+
* `1` (ordered, one-at-a-time). Floored at `1`.
|
|
402
|
+
* - `retries` — the default extra attempts per entry on failure; defaults to `0`.
|
|
403
|
+
* - `timeout` — the default per-attempt deadline in milliseconds; defaults to none
|
|
404
|
+
* (a non-positive value means no deadline).
|
|
405
|
+
* - `store` — durable backing; outstanding entries survive a restart; call
|
|
406
|
+
* `restore()` to re-run them.
|
|
407
|
+
* - `on` — the reserved {@link EmitterHooks} key (§8): initial listeners for the queue's
|
|
408
|
+
* {@link QueueEventMap}, wired at construction (e.g. `{ drain: () => log('idle') }`).
|
|
409
|
+
*/
|
|
410
|
+
export declare interface QueueOptions<TInput, TResult> {
|
|
411
|
+
readonly on?: EmitterHooks<QueueEventMap<TResult>>;
|
|
412
|
+
/** The emitter's listener-error handler (AGENTS §13) — a listener throw routes here, not to a domain event. */
|
|
413
|
+
readonly error?: EmitterErrorHandler;
|
|
414
|
+
readonly handler: QueueHandler<TInput, TResult>;
|
|
415
|
+
readonly concurrency?: number;
|
|
416
|
+
readonly retries?: number;
|
|
417
|
+
readonly timeout?: number;
|
|
418
|
+
readonly store?: QueueStoreInterface<TInput>;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Durable backing for a Queue's outstanding entries.
|
|
423
|
+
*
|
|
424
|
+
* @remarks
|
|
425
|
+
* The store holds ONLY work that has not yet completed: `save` upserts an entry
|
|
426
|
+
* (by its `id`), `remove` drops a finished one, `load` returns everything
|
|
427
|
+
* outstanding (to restore a queue after a restart), and `clear` empties it. It is
|
|
428
|
+
* a minimal interface (AGENTS §21) over the `databases` layer — a queue's durable
|
|
429
|
+
* state is just a table — so any `DriverInterface` backend (memory, JSON, SQLite)
|
|
430
|
+
* persists it without the store knowing which.
|
|
431
|
+
*
|
|
432
|
+
* @typeParam TInput - The work input each {@link StoredEntry} carries
|
|
433
|
+
*/
|
|
434
|
+
export declare interface QueueStoreInterface<TInput> {
|
|
435
|
+
save(entry: StoredEntry<TInput>): Promise<void>;
|
|
436
|
+
remove(id: string): Promise<void>;
|
|
437
|
+
load(): Promise<readonly StoredEntry<TInput>[]>;
|
|
438
|
+
clear(): Promise<void>;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* A durably persisted, still-outstanding queue entry — re-run after a restart.
|
|
443
|
+
*
|
|
444
|
+
* @remarks
|
|
445
|
+
* The store holds only entries that have NOT yet completed, so what `load`
|
|
446
|
+
* returns on startup is exactly the work to resume. `id` keys the entry (the
|
|
447
|
+
* store upserts by it); `input` is the handler's work payload (it must be
|
|
448
|
+
* JSON-serializable to survive a JSON / SQLite driver); `attempts` is how many
|
|
449
|
+
* times the entry has been tried so far.
|
|
450
|
+
*/
|
|
451
|
+
export declare interface StoredEntry<TInput> {
|
|
452
|
+
readonly id: string;
|
|
453
|
+
readonly input: TInput;
|
|
454
|
+
readonly attempts: number;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
export { }
|