@orkestrel/queue 0.0.10 → 0.0.12
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 +3 -3
- package/dist/src/core/index.cjs +189 -166
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +204 -109
- package/dist/src/core/index.d.ts +204 -109
- package/dist/src/core/index.js +188 -168
- package/dist/src/core/index.js.map +1 -1
- package/package.json +21 -18
package/README.md
CHANGED
|
@@ -24,8 +24,8 @@ npm install @orkestrel/queue
|
|
|
24
24
|
|
|
25
25
|
## Requirements
|
|
26
26
|
|
|
27
|
-
- Node.js >=
|
|
28
|
-
- ESM
|
|
27
|
+
- Node.js >= 22.12.0
|
|
28
|
+
- ESM and CommonJS builds from one entry point
|
|
29
29
|
|
|
30
30
|
## Usage
|
|
31
31
|
|
|
@@ -47,7 +47,7 @@ const status = await queue.enqueue('https://example.com')
|
|
|
47
47
|
For the full surface — the `Queue` engine, options, the durable
|
|
48
48
|
`QueueStoreInterface` (`MemoryQueueStore` / `DatabaseQueueStore`), the
|
|
49
49
|
observable `emitter`, and usage patterns — see
|
|
50
|
-
[`guides/
|
|
50
|
+
[`guides/queue.md`](guides/queue.md).
|
|
51
51
|
|
|
52
52
|
## Package
|
|
53
53
|
|
package/dist/src/core/index.cjs
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
let _orkestrel_contract = require("@orkestrel/contract");
|
|
3
|
+
let _orkestrel_timeout = require("@orkestrel/timeout");
|
|
3
4
|
let _orkestrel_abort = require("@orkestrel/abort");
|
|
4
5
|
let _orkestrel_emitter = require("@orkestrel/emitter");
|
|
5
|
-
let _orkestrel_timeout = require("@orkestrel/timeout");
|
|
6
6
|
let _orkestrel_database = require("@orkestrel/database");
|
|
7
7
|
//#region src/core/errors.ts
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* Represents a failure carrying a machine-readable queue category and optional context.
|
|
10
10
|
*
|
|
11
11
|
* @example
|
|
12
12
|
* ```ts
|
|
@@ -18,7 +18,7 @@ var QueueError = class extends Error {
|
|
|
18
18
|
code;
|
|
19
19
|
context;
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
21
|
+
* Creates a queue error.
|
|
22
22
|
*
|
|
23
23
|
* @param message - Human-readable failure description
|
|
24
24
|
* @param options - Machine-readable category, optional context, and optional cause
|
|
@@ -31,10 +31,11 @@ var QueueError = class extends Error {
|
|
|
31
31
|
}
|
|
32
32
|
};
|
|
33
33
|
/**
|
|
34
|
-
*
|
|
34
|
+
* Determines whether an unknown value is a {@link QueueError}.
|
|
35
35
|
*
|
|
36
36
|
* @param value - The value to inspect
|
|
37
|
-
* @returns
|
|
37
|
+
* @returns True if the value is a real `QueueError` instance; false otherwise, including for
|
|
38
|
+
* a hostile value
|
|
38
39
|
*
|
|
39
40
|
* @example
|
|
40
41
|
* ```ts
|
|
@@ -55,10 +56,10 @@ function isQueueError(value) {
|
|
|
55
56
|
//#endregion
|
|
56
57
|
//#region src/core/validators.ts
|
|
57
58
|
/**
|
|
58
|
-
*
|
|
59
|
+
* Determines whether a value is a valid queue concurrency.
|
|
59
60
|
*
|
|
60
61
|
* @param value - Value to inspect
|
|
61
|
-
* @returns
|
|
62
|
+
* @returns True if the value is a positive safe integer; false otherwise
|
|
62
63
|
*
|
|
63
64
|
* @example
|
|
64
65
|
* ```ts
|
|
@@ -70,10 +71,10 @@ function isQueueConcurrency(value) {
|
|
|
70
71
|
return (0, _orkestrel_contract.isInteger)(value) && Number.isSafeInteger(value) && value > 0;
|
|
71
72
|
}
|
|
72
73
|
/**
|
|
73
|
-
*
|
|
74
|
+
* Determines whether a value is a valid queue retry count.
|
|
74
75
|
*
|
|
75
76
|
* @param value - Value to inspect
|
|
76
|
-
* @returns
|
|
77
|
+
* @returns True if the value is a nonnegative safe integer; false otherwise
|
|
77
78
|
*
|
|
78
79
|
* @example
|
|
79
80
|
* ```ts
|
|
@@ -85,10 +86,11 @@ function isQueueRetries(value) {
|
|
|
85
86
|
return (0, _orkestrel_contract.isInteger)(value) && Number.isSafeInteger(value) && value >= 0;
|
|
86
87
|
}
|
|
87
88
|
/**
|
|
88
|
-
*
|
|
89
|
+
* Determines whether a value is a valid queue timeout.
|
|
89
90
|
*
|
|
90
91
|
* @param value - Value to inspect
|
|
91
|
-
* @returns
|
|
92
|
+
* @returns True if the value is an integer within the native timer range,
|
|
93
|
+
* inclusive; false otherwise
|
|
92
94
|
*
|
|
93
95
|
* @example
|
|
94
96
|
* ```ts
|
|
@@ -97,13 +99,13 @@ function isQueueRetries(value) {
|
|
|
97
99
|
* ```
|
|
98
100
|
*/
|
|
99
101
|
function isQueueTimeout(value) {
|
|
100
|
-
return (0, _orkestrel_contract.isFiniteNumber)(value) && (0, _orkestrel_contract.isInteger)(value) && value >= 0 && value <=
|
|
102
|
+
return (0, _orkestrel_contract.isFiniteNumber)(value) && (0, _orkestrel_contract.isInteger)(value) && value >= 0 && value <= _orkestrel_timeout.MAX_TIMEOUT_MS;
|
|
101
103
|
}
|
|
102
104
|
/**
|
|
103
|
-
*
|
|
105
|
+
* Determines whether a value is a native abort signal usable by the queue.
|
|
104
106
|
*
|
|
105
107
|
* @param value - Value to inspect
|
|
106
|
-
* @returns
|
|
108
|
+
* @returns True if the value carries the native `AbortSignal` internal slot; false otherwise
|
|
107
109
|
*
|
|
108
110
|
* @example
|
|
109
111
|
* ```ts
|
|
@@ -121,10 +123,94 @@ function isQueueSignal(value) {
|
|
|
121
123
|
return false;
|
|
122
124
|
}
|
|
123
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Checks whether a value is a valid stored queue entry.
|
|
128
|
+
*
|
|
129
|
+
* @remarks
|
|
130
|
+
* The single test for what a {@link StoredEntry} is, shared by the queue's `restore`
|
|
131
|
+
* boundary and the memory store's `save` boundary. `input` carries the caller's own
|
|
132
|
+
* payload type, so only its presence is checked.
|
|
133
|
+
*
|
|
134
|
+
* @param value - Value to inspect
|
|
135
|
+
* @returns True if the value is a record holding a string `id`, an `input` member, and a
|
|
136
|
+
* nonnegative safe-integer `attempts`; false otherwise
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```ts
|
|
140
|
+
* isStoredEntry({ id: 'job-1', input: 'task', attempts: 0 }) // true
|
|
141
|
+
* isStoredEntry({ id: 'job-1', input: 'task', attempts: -1 }) // false
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
function isStoredEntry(value) {
|
|
145
|
+
try {
|
|
146
|
+
if (!(0, _orkestrel_contract.isRecord)(value)) return false;
|
|
147
|
+
return (0, _orkestrel_contract.isString)(value.id) && "input" in value && isQueueRetries(value.attempts);
|
|
148
|
+
} catch {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
//#endregion
|
|
153
|
+
//#region src/core/helpers.ts
|
|
154
|
+
/**
|
|
155
|
+
* Reads one named option from a caller-supplied entry options object.
|
|
156
|
+
*
|
|
157
|
+
* @remarks
|
|
158
|
+
* The property is read exactly once, inside a boundary that contains a throwing
|
|
159
|
+
* getter. An absent options object reads nothing and yields `undefined`.
|
|
160
|
+
*
|
|
161
|
+
* @param options - The caller's own entry options, or `undefined`
|
|
162
|
+
* @param option - The option key to read
|
|
163
|
+
* @param message - The failure description the thrown error carries
|
|
164
|
+
* @returns The value the property held, or `undefined` when no options object was supplied
|
|
165
|
+
* @throws {QueueError} Thrown when reading the property throws, coded `invalid` with the
|
|
166
|
+
* option in its context.
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```ts
|
|
170
|
+
* readOption({ retries: 2 }, 'retries', 'queue retries could not be read') // 2
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
function readOption(options, option, message) {
|
|
174
|
+
try {
|
|
175
|
+
return options?.[option];
|
|
176
|
+
} catch (error) {
|
|
177
|
+
throw new QueueError(message, {
|
|
178
|
+
code: "invalid",
|
|
179
|
+
cause: error,
|
|
180
|
+
context: { option }
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Validates one already-read queue option against its guard.
|
|
186
|
+
*
|
|
187
|
+
* @param value - The option value to check
|
|
188
|
+
* @param guard - The total guard the value must satisfy
|
|
189
|
+
* @param option - The option key the failure context reports
|
|
190
|
+
* @param message - The failure description the thrown error carries
|
|
191
|
+
* @returns The same value, narrowed by the guard
|
|
192
|
+
* @throws {QueueError} Thrown when the guard refuses the value, coded `invalid` with the
|
|
193
|
+
* option and the refused value in its context.
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* ```ts
|
|
197
|
+
* validateOption(4, isQueueConcurrency, 'concurrency', 'queue concurrency must be positive') // 4
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
function validateOption(value, guard, option, message) {
|
|
201
|
+
if (guard(value)) return value;
|
|
202
|
+
throw new QueueError(message, {
|
|
203
|
+
code: "invalid",
|
|
204
|
+
context: {
|
|
205
|
+
option,
|
|
206
|
+
value
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
}
|
|
124
210
|
//#endregion
|
|
125
211
|
//#region src/core/Queue.ts
|
|
126
212
|
/**
|
|
127
|
-
*
|
|
213
|
+
* Represents a concurrent, cooperative FIFO job queue with optional outstanding-work persistence.
|
|
128
214
|
*
|
|
129
215
|
* @typeParam TInput - The work input each entry carries
|
|
130
216
|
* @typeParam TResult - The value each entry resolves
|
|
@@ -159,42 +245,19 @@ var Queue = class {
|
|
|
159
245
|
#generation = 0;
|
|
160
246
|
#paused = false;
|
|
161
247
|
#stopped = false;
|
|
162
|
-
#aborted = false;
|
|
163
|
-
#destroyed = false;
|
|
164
248
|
#drained = true;
|
|
165
249
|
/**
|
|
166
|
-
*
|
|
250
|
+
* Creates a queue.
|
|
167
251
|
*
|
|
168
252
|
* @param options - Handler, validated execution limits, persistence, and observation hooks
|
|
169
253
|
*/
|
|
170
254
|
constructor(options) {
|
|
171
255
|
const configuredConcurrency = options.concurrency;
|
|
172
|
-
const concurrency = configuredConcurrency === void 0 ? 1 : configuredConcurrency;
|
|
173
|
-
if (!isQueueConcurrency(concurrency)) throw new QueueError("queue concurrency must be a positive safe integer", {
|
|
174
|
-
code: "invalid",
|
|
175
|
-
context: {
|
|
176
|
-
option: "concurrency",
|
|
177
|
-
value: concurrency
|
|
178
|
-
}
|
|
179
|
-
});
|
|
256
|
+
const concurrency = validateOption(configuredConcurrency === void 0 ? 1 : configuredConcurrency, isQueueConcurrency, "concurrency", "queue concurrency must be a positive safe integer");
|
|
180
257
|
const configuredRetries = options.retries;
|
|
181
|
-
const retries = configuredRetries === void 0 ? 0 : configuredRetries;
|
|
182
|
-
if (!isQueueRetries(retries)) throw new QueueError("queue retries must be a nonnegative safe integer", {
|
|
183
|
-
code: "invalid",
|
|
184
|
-
context: {
|
|
185
|
-
option: "retries",
|
|
186
|
-
value: retries
|
|
187
|
-
}
|
|
188
|
-
});
|
|
258
|
+
const retries = validateOption(configuredRetries === void 0 ? 0 : configuredRetries, isQueueRetries, "retries", "queue retries must be a nonnegative safe integer");
|
|
189
259
|
const configuredTimeout = options.timeout;
|
|
190
|
-
const timeout = configuredTimeout === void 0 ? 0 : configuredTimeout;
|
|
191
|
-
if (!isQueueTimeout(timeout)) throw new QueueError("queue timeout must be within the native timer range", {
|
|
192
|
-
code: "invalid",
|
|
193
|
-
context: {
|
|
194
|
-
option: "timeout",
|
|
195
|
-
value: timeout
|
|
196
|
-
}
|
|
197
|
-
});
|
|
260
|
+
const timeout = validateOption(configuredTimeout === void 0 ? 0 : configuredTimeout, isQueueTimeout, "timeout", "queue timeout must be within the native timer range");
|
|
198
261
|
const on = options.on;
|
|
199
262
|
const error = options.error;
|
|
200
263
|
this.#handler = options.handler;
|
|
@@ -220,93 +283,33 @@ var Queue = class {
|
|
|
220
283
|
return this.#paused;
|
|
221
284
|
}
|
|
222
285
|
get stopped() {
|
|
223
|
-
return this.#stopped || this.#
|
|
286
|
+
return this.#stopped || this.#abortPromise !== void 0;
|
|
224
287
|
}
|
|
225
288
|
/**
|
|
226
|
-
*
|
|
289
|
+
* Reserves and submits one FIFO entry.
|
|
227
290
|
*
|
|
228
291
|
* @param input - Handler input
|
|
229
292
|
* @param options - Optional id, retry/timeout overrides, and entry abort signal
|
|
230
293
|
* @returns The entry's settle-once execution promise
|
|
231
|
-
* @throws {QueueError}
|
|
294
|
+
* @throws {QueueError} Thrown synchronously when an option is inaccessible or invalid.
|
|
232
295
|
*/
|
|
233
296
|
enqueue(input, options) {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
if (supplied !== void 0 && !(0, _orkestrel_contract.isString)(supplied)) throw new QueueError("queue entry id must be a string", {
|
|
245
|
-
code: "invalid",
|
|
246
|
-
context: {
|
|
247
|
-
option: "id",
|
|
248
|
-
value: supplied
|
|
249
|
-
}
|
|
250
|
-
});
|
|
251
|
-
let retries;
|
|
252
|
-
try {
|
|
253
|
-
retries = options?.retries;
|
|
254
|
-
} catch (error) {
|
|
255
|
-
throw new QueueError("queue retries could not be read", {
|
|
256
|
-
code: "invalid",
|
|
257
|
-
cause: error,
|
|
258
|
-
context: { option: "retries" }
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
if (retries !== void 0 && !isQueueRetries(retries)) throw new QueueError("queue retries must be a nonnegative safe integer", {
|
|
262
|
-
code: "invalid",
|
|
263
|
-
context: {
|
|
264
|
-
option: "retries",
|
|
265
|
-
value: retries
|
|
266
|
-
}
|
|
267
|
-
});
|
|
268
|
-
let timeout;
|
|
269
|
-
try {
|
|
270
|
-
timeout = options?.timeout;
|
|
271
|
-
} catch (error) {
|
|
272
|
-
throw new QueueError("queue timeout could not be read", {
|
|
273
|
-
code: "invalid",
|
|
274
|
-
cause: error,
|
|
275
|
-
context: { option: "timeout" }
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
if (timeout !== void 0 && !isQueueTimeout(timeout)) throw new QueueError("queue timeout must be within the native timer range", {
|
|
279
|
-
code: "invalid",
|
|
280
|
-
context: {
|
|
281
|
-
option: "timeout",
|
|
282
|
-
value: timeout
|
|
283
|
-
}
|
|
284
|
-
});
|
|
285
|
-
let signal;
|
|
286
|
-
try {
|
|
287
|
-
signal = options?.signal;
|
|
288
|
-
} catch (error) {
|
|
289
|
-
throw new QueueError("queue signal could not be read", {
|
|
290
|
-
code: "invalid",
|
|
291
|
-
cause: error,
|
|
292
|
-
context: { option: "signal" }
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
if (signal !== void 0 && !isQueueSignal(signal)) throw new QueueError("queue signal must be an AbortSignal", {
|
|
296
|
-
code: "invalid",
|
|
297
|
-
context: {
|
|
298
|
-
option: "signal",
|
|
299
|
-
value: signal
|
|
300
|
-
}
|
|
301
|
-
});
|
|
297
|
+
const rawId = readOption(options, "id", "queue entry id could not be read");
|
|
298
|
+
const supplied = rawId === void 0 ? void 0 : validateOption(rawId, _orkestrel_contract.isString, "id", "queue entry id must be a string");
|
|
299
|
+
const rawRetries = readOption(options, "retries", "queue retries could not be read");
|
|
300
|
+
const retries = rawRetries === void 0 ? void 0 : validateOption(rawRetries, isQueueRetries, "retries", "queue retries must be a nonnegative safe integer");
|
|
301
|
+
const rawTimeout = readOption(options, "timeout", "queue timeout could not be read");
|
|
302
|
+
const timeout = rawTimeout === void 0 ? void 0 : validateOption(rawTimeout, isQueueTimeout, "timeout", "queue timeout must be within the native timer range");
|
|
303
|
+
const rawSignal = readOption(options, "signal", "queue signal could not be read");
|
|
304
|
+
const signal = rawSignal === void 0 ? void 0 : validateOption(rawSignal, isQueueSignal, "signal", "queue signal must be an AbortSignal");
|
|
302
305
|
const normalized = options === void 0 ? void 0 : Object.freeze({
|
|
303
306
|
...supplied === void 0 ? {} : { id: supplied },
|
|
304
307
|
...retries === void 0 ? {} : { retries },
|
|
305
308
|
...timeout === void 0 ? {} : { timeout },
|
|
306
309
|
...signal === void 0 ? {} : { signal }
|
|
307
310
|
});
|
|
308
|
-
if (this.#
|
|
309
|
-
if (this.#
|
|
311
|
+
if (this.#destroyPromise !== void 0) return Promise.reject(new QueueError("queue is destroyed", { code: "destroyed" }));
|
|
312
|
+
if (this.#abortPromise !== void 0) return Promise.reject(new QueueError("queue is aborted", { code: "aborted" }));
|
|
310
313
|
if (this.#stopped) return Promise.reject(new QueueError("queue is stopped", { code: "stopped" }));
|
|
311
314
|
const id = supplied ?? crypto.randomUUID();
|
|
312
315
|
if (this.#live.has(id)) return Promise.reject(new QueueError("queue entry id is already live", {
|
|
@@ -318,27 +321,30 @@ var Queue = class {
|
|
|
318
321
|
else this.#schedule(token);
|
|
319
322
|
return token.promise;
|
|
320
323
|
}
|
|
321
|
-
/**
|
|
324
|
+
/**
|
|
325
|
+
* Loads and accepts every outstanding stored entry from the current lifecycle generation.
|
|
326
|
+
*
|
|
327
|
+
* @returns A promise that settles after every loaded entry is reserved and accepted
|
|
328
|
+
* @throws {QueueError} Thrown when the store's load fails or returns a malformed entry.
|
|
329
|
+
*/
|
|
322
330
|
async restore() {
|
|
323
|
-
if (this.#store === void 0 || this.stopped || this.#
|
|
331
|
+
if (this.#store === void 0 || this.stopped || this.#destroyPromise !== void 0) return;
|
|
324
332
|
const generation = this.#generation;
|
|
325
333
|
const entries = [];
|
|
326
334
|
let malformed = false;
|
|
327
335
|
try {
|
|
328
336
|
const loaded = await this.#store.load();
|
|
329
337
|
for (const stored of loaded) {
|
|
330
|
-
const
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
338
|
+
const candidate = {
|
|
339
|
+
id: stored.id,
|
|
340
|
+
input: stored.input,
|
|
341
|
+
attempts: stored.attempts
|
|
342
|
+
};
|
|
343
|
+
if (!isStoredEntry(candidate)) {
|
|
334
344
|
malformed = true;
|
|
335
345
|
break;
|
|
336
346
|
}
|
|
337
|
-
entries.push(Object.freeze(
|
|
338
|
-
id,
|
|
339
|
-
input,
|
|
340
|
-
attempts
|
|
341
|
-
}));
|
|
347
|
+
entries.push(Object.freeze(candidate));
|
|
342
348
|
}
|
|
343
349
|
} catch (error) {
|
|
344
350
|
throw new QueueError("queue store load failed", {
|
|
@@ -351,23 +357,27 @@ var Queue = class {
|
|
|
351
357
|
code: "store",
|
|
352
358
|
context: { operation: "load" }
|
|
353
359
|
});
|
|
354
|
-
if (generation !== this.#generation || this.stopped || this.#
|
|
360
|
+
if (generation !== this.#generation || this.stopped || this.#destroyPromise !== void 0) return;
|
|
355
361
|
for (const stored of entries) {
|
|
356
|
-
if (generation !== this.#generation || this.stopped || this.#
|
|
362
|
+
if (generation !== this.#generation || this.stopped || this.#destroyPromise !== void 0) return;
|
|
357
363
|
if (this.#live.has(stored.id)) continue;
|
|
358
364
|
const token = this.#reserve(stored.id, stored.input, void 0, stored.attempts);
|
|
359
365
|
this.#accept(token);
|
|
360
366
|
token.promise.catch(() => {});
|
|
361
367
|
}
|
|
362
368
|
}
|
|
363
|
-
/**
|
|
369
|
+
/** Begins or restarts worker execution unless the queue is terminal. */
|
|
364
370
|
start() {
|
|
365
|
-
if (this.#
|
|
371
|
+
if (this.#abortPromise !== void 0 || this.#destroyPromise !== void 0) return;
|
|
366
372
|
this.#stopped = false;
|
|
367
373
|
this.#stopPromise = void 0;
|
|
368
374
|
this.#spawn();
|
|
369
375
|
}
|
|
370
|
-
/**
|
|
376
|
+
/**
|
|
377
|
+
* Rejects non-active work and awaits current-loop and durable cleanup quiescence.
|
|
378
|
+
*
|
|
379
|
+
* @returns The stable stop barrier
|
|
380
|
+
*/
|
|
371
381
|
stop() {
|
|
372
382
|
if (this.#destroyPromise !== void 0) return this.#destroyPromise;
|
|
373
383
|
if (this.#abortPromise !== void 0) return this.#abortPromise;
|
|
@@ -388,19 +398,19 @@ var Queue = class {
|
|
|
388
398
|
this.#settleBarrier(barrier, tasks, "queue stop cleanup failed");
|
|
389
399
|
return barrier.promise;
|
|
390
400
|
}
|
|
391
|
-
/**
|
|
401
|
+
/** Suspends new execution while leaving active entries untouched. */
|
|
392
402
|
pause() {
|
|
393
|
-
if (this.#paused || this.stopped || this.#
|
|
403
|
+
if (this.#paused || this.stopped || this.#destroyPromise !== void 0) return;
|
|
394
404
|
this.#paused = true;
|
|
395
405
|
}
|
|
396
|
-
/**
|
|
406
|
+
/** Continues execution after a pause. */
|
|
397
407
|
resume() {
|
|
398
|
-
if (!this.#paused || this.stopped || this.#
|
|
408
|
+
if (!this.#paused || this.stopped || this.#destroyPromise !== void 0) return;
|
|
399
409
|
this.#paused = false;
|
|
400
410
|
this.#wakeAll();
|
|
401
411
|
}
|
|
402
412
|
/**
|
|
403
|
-
*
|
|
413
|
+
* Cancels the queue and awaits owned cleanup.
|
|
404
414
|
*
|
|
405
415
|
* @param reason - Optional cause carried by the coded abort error
|
|
406
416
|
* @returns The stable abort barrier
|
|
@@ -409,7 +419,6 @@ var Queue = class {
|
|
|
409
419
|
if (this.#abortPromise !== void 0) return this.#abortPromise;
|
|
410
420
|
const barrier = Promise.withResolvers();
|
|
411
421
|
this.#abortPromise = barrier.promise;
|
|
412
|
-
this.#aborted = true;
|
|
413
422
|
this.#stopped = true;
|
|
414
423
|
this.#generation += 1;
|
|
415
424
|
const error = new QueueError(reason instanceof Error ? reason.message : "queue is aborted", {
|
|
@@ -430,9 +439,13 @@ var Queue = class {
|
|
|
430
439
|
this.#settleBarrier(barrier, tasks, "queue abort cleanup failed");
|
|
431
440
|
return barrier.promise;
|
|
432
441
|
}
|
|
433
|
-
/**
|
|
442
|
+
/**
|
|
443
|
+
* Rejects non-active work and awaits its durable cleanup.
|
|
444
|
+
*
|
|
445
|
+
* @returns A promise that settles after every affected removal completes
|
|
446
|
+
*/
|
|
434
447
|
clear() {
|
|
435
|
-
if (this.#
|
|
448
|
+
if (this.#destroyPromise !== void 0) return Promise.reject(new QueueError("queue is destroyed", { code: "destroyed" }));
|
|
436
449
|
const existing = [];
|
|
437
450
|
for (const [token, cleanup] of this.#cleanups) if (!this.#claims.has(token)) existing.push(cleanup);
|
|
438
451
|
const cleanup = [...this.#drain(new QueueError("queue is cleared", { code: "cleared" })), ...this.#cleanOrphans(false)];
|
|
@@ -442,12 +455,15 @@ var Queue = class {
|
|
|
442
455
|
...cleanup
|
|
443
456
|
], "queue clear cleanup failed");
|
|
444
457
|
}
|
|
445
|
-
/**
|
|
458
|
+
/**
|
|
459
|
+
* Tears down once, destroying the emitter only after cleanup finishes.
|
|
460
|
+
*
|
|
461
|
+
* @returns The stable destroy barrier
|
|
462
|
+
*/
|
|
446
463
|
destroy() {
|
|
447
464
|
if (this.#destroyPromise !== void 0) return this.#destroyPromise;
|
|
448
465
|
const barrier = Promise.withResolvers();
|
|
449
466
|
this.#destroyPromise = barrier.promise;
|
|
450
|
-
this.#destroyed = true;
|
|
451
467
|
const aborting = this.abort(new QueueError("queue is destroyed", { code: "destroyed" }));
|
|
452
468
|
this.#settleDestroy(barrier, aborting);
|
|
453
469
|
return barrier.promise;
|
|
@@ -505,7 +521,7 @@ var Queue = class {
|
|
|
505
521
|
this.#emitDrain();
|
|
506
522
|
return;
|
|
507
523
|
}
|
|
508
|
-
if (entry.settled || this.#
|
|
524
|
+
if (entry.settled || this.#abortPromise !== void 0 || this.#destroyPromise !== void 0 || this.#stopped) {
|
|
509
525
|
await this.#discard(token);
|
|
510
526
|
return;
|
|
511
527
|
}
|
|
@@ -520,7 +536,7 @@ var Queue = class {
|
|
|
520
536
|
this.#wake();
|
|
521
537
|
}
|
|
522
538
|
#spawn() {
|
|
523
|
-
if (this.#stopped || this.#
|
|
539
|
+
if (this.#stopped || this.#abortPromise !== void 0 || this.#destroyPromise !== void 0) return;
|
|
524
540
|
const demand = Math.min(this.#concurrency, this.#claims.size + this.#pending.length);
|
|
525
541
|
while (this.#workers.size < demand) {
|
|
526
542
|
const generation = this.#generation;
|
|
@@ -548,7 +564,7 @@ var Queue = class {
|
|
|
548
564
|
return new Promise((resolve) => this.#take(generation, resolve));
|
|
549
565
|
}
|
|
550
566
|
#take(generation, resolve) {
|
|
551
|
-
if (generation !== this.#generation || this.#stopped || this.#
|
|
567
|
+
if (generation !== this.#generation || this.#stopped || this.#abortPromise !== void 0 || this.#destroyPromise !== void 0) {
|
|
552
568
|
resolve(void 0);
|
|
553
569
|
return;
|
|
554
570
|
}
|
|
@@ -868,7 +884,7 @@ var Queue = class {
|
|
|
868
884
|
//#endregion
|
|
869
885
|
//#region src/core/stores/DatabaseQueueStore.ts
|
|
870
886
|
/**
|
|
871
|
-
*
|
|
887
|
+
* Represents a {@link QueueStoreInterface} backed by one table of the `@orkestrel/database` layer — a
|
|
872
888
|
* queue's durable state IS a table, so persistence reduces to keyed CRUD over a
|
|
873
889
|
* `TableInterface`.
|
|
874
890
|
*
|
|
@@ -878,7 +894,7 @@ var Queue = class {
|
|
|
878
894
|
* `save` upserts an entry by its primary key (`set`), `remove` drops one (`remove`),
|
|
879
895
|
* `load` returns every outstanding entry (`records` with no criteria), and `clear`
|
|
880
896
|
* empties the table. Reads are narrowed through the table's contract, so `load`
|
|
881
|
-
* returns typed {@link StoredEntry}`<TInput>[]` with no cast
|
|
897
|
+
* returns typed {@link StoredEntry}`<TInput>[]` with no cast — the table
|
|
882
898
|
* is created over the `{ id; input; attempts }` column map, which `Infer`s to exactly
|
|
883
899
|
* that row. The store holds only OUTSTANDING work (completed entries are `remove`d), so
|
|
884
900
|
* `load` on startup yields precisely the entries to resume.
|
|
@@ -899,7 +915,7 @@ var Queue = class {
|
|
|
899
915
|
var DatabaseQueueStore = class {
|
|
900
916
|
#table;
|
|
901
917
|
/**
|
|
902
|
-
*
|
|
918
|
+
* Wraps a table as a queue store.
|
|
903
919
|
*
|
|
904
920
|
* @param table - The {@link TableInterface} holding the outstanding entries — its
|
|
905
921
|
* row is a {@link StoredEntry}`<TInput>` (the `{ id; input; attempts }` shape)
|
|
@@ -907,19 +923,19 @@ var DatabaseQueueStore = class {
|
|
|
907
923
|
constructor(table) {
|
|
908
924
|
this.#table = table;
|
|
909
925
|
}
|
|
910
|
-
/**
|
|
926
|
+
/** Upserts an entry by its `id` (a re-`save` of an existing `id` overwrites it). */
|
|
911
927
|
async save(entry) {
|
|
912
928
|
await this.#table.set(entry);
|
|
913
929
|
}
|
|
914
|
-
/**
|
|
930
|
+
/** Drops a finished entry by `id`; absent is a no-op (no throw). */
|
|
915
931
|
async remove(id) {
|
|
916
932
|
await this.#table.remove(id);
|
|
917
933
|
}
|
|
918
|
-
/**
|
|
934
|
+
/** Returns every outstanding entry — the work to resume after a restart. */
|
|
919
935
|
load() {
|
|
920
936
|
return this.#table.records();
|
|
921
937
|
}
|
|
922
|
-
/**
|
|
938
|
+
/** Empties the store — drops every outstanding entry. */
|
|
923
939
|
async clear() {
|
|
924
940
|
await this.#table.clear();
|
|
925
941
|
}
|
|
@@ -927,7 +943,7 @@ var DatabaseQueueStore = class {
|
|
|
927
943
|
//#endregion
|
|
928
944
|
//#region src/core/stores/MemoryQueueStore.ts
|
|
929
945
|
/**
|
|
930
|
-
*
|
|
946
|
+
* Represents an in-memory store owning validated, immutable JSON snapshots of outstanding entries.
|
|
931
947
|
*
|
|
932
948
|
* @typeParam TInput - The contract shape for each stored input
|
|
933
949
|
*
|
|
@@ -944,23 +960,26 @@ var MemoryQueueStore = class {
|
|
|
944
960
|
#contract;
|
|
945
961
|
#entries = /* @__PURE__ */ new Map();
|
|
946
962
|
/**
|
|
947
|
-
*
|
|
963
|
+
* Creates an in-memory queue store.
|
|
948
964
|
*
|
|
949
965
|
* @param input - Runtime contract for each stored input
|
|
950
966
|
*/
|
|
951
967
|
constructor(input) {
|
|
952
968
|
this.#contract = (0, _orkestrel_contract.createContract)(input);
|
|
953
969
|
}
|
|
954
|
-
/**
|
|
970
|
+
/** Upserts a validated, owned snapshot under the entry's id. */
|
|
955
971
|
save(entry) {
|
|
956
972
|
try {
|
|
957
|
-
const
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
973
|
+
const candidate = {
|
|
974
|
+
id: entry.id,
|
|
975
|
+
input: entry.input,
|
|
976
|
+
attempts: entry.attempts
|
|
977
|
+
};
|
|
978
|
+
if (!isStoredEntry(candidate)) throw new QueueError("queue store entry is invalid", {
|
|
961
979
|
code: "store",
|
|
962
980
|
context: { operation: "save" }
|
|
963
981
|
});
|
|
982
|
+
const { id, input, attempts } = candidate;
|
|
964
983
|
this.#entries.set(id, this.#snapshot(id, input, attempts, "save"));
|
|
965
984
|
return Promise.resolve();
|
|
966
985
|
} catch (error) {
|
|
@@ -971,12 +990,12 @@ var MemoryQueueStore = class {
|
|
|
971
990
|
}));
|
|
972
991
|
}
|
|
973
992
|
}
|
|
974
|
-
/**
|
|
993
|
+
/** Removes an entry; an absent id is a no-op. */
|
|
975
994
|
remove(id) {
|
|
976
995
|
this.#entries.delete(id);
|
|
977
996
|
return Promise.resolve();
|
|
978
997
|
}
|
|
979
|
-
/**
|
|
998
|
+
/** Returns fresh immutable snapshots of every outstanding entry. */
|
|
980
999
|
load() {
|
|
981
1000
|
try {
|
|
982
1001
|
const snapshots = [];
|
|
@@ -995,7 +1014,7 @@ var MemoryQueueStore = class {
|
|
|
995
1014
|
}));
|
|
996
1015
|
}
|
|
997
1016
|
}
|
|
998
|
-
/**
|
|
1017
|
+
/** Removes every outstanding entry. */
|
|
999
1018
|
clear() {
|
|
1000
1019
|
this.#entries.clear();
|
|
1001
1020
|
return Promise.resolve();
|
|
@@ -1019,7 +1038,7 @@ var MemoryQueueStore = class {
|
|
|
1019
1038
|
//#endregion
|
|
1020
1039
|
//#region src/core/factories.ts
|
|
1021
1040
|
/**
|
|
1022
|
-
*
|
|
1041
|
+
* Creates a concurrent, cooperative job queue — a bounded-concurrency engine that
|
|
1023
1042
|
* runs each enqueued input through a handler, with retries and a per-attempt
|
|
1024
1043
|
* timeout / abort, all over the L1 `Abort` / `Timeout` primitives.
|
|
1025
1044
|
*
|
|
@@ -1029,13 +1048,14 @@ var MemoryQueueStore = class {
|
|
|
1029
1048
|
* returns a promise that settles when the entry finally completes, fails (after its
|
|
1030
1049
|
* retries), or is rejected by an abort. A queue-level `abort` never retries; the
|
|
1031
1050
|
* lifecycle (`start` / `stop` / `pause` / `resume` / `abort` / `clear` / `destroy`)
|
|
1032
|
-
* follows
|
|
1051
|
+
* follows the fixed lifecycle vocabulary. The queue is observable: a typed `emitter` surfaces
|
|
1033
1052
|
* `enqueue` / `start` / `retry` / `success` / `failure` / `abort` / `drain`.
|
|
1034
1053
|
*
|
|
1035
1054
|
* @typeParam TInput - The work input each entry carries
|
|
1036
1055
|
* @typeParam TResult - The value the handler resolves for an entry
|
|
1037
|
-
* @param options - The `handler` plus optional `concurrency`
|
|
1038
|
-
*
|
|
1056
|
+
* @param options - The `handler` plus optional `concurrency`, `retries`, and a default
|
|
1057
|
+
* per-attempt `timeout` in milliseconds. Default: `concurrency` `1`, `retries` `0`,
|
|
1058
|
+
* `timeout` `0`.
|
|
1039
1059
|
* @returns A working {@link QueueInterface}
|
|
1040
1060
|
*
|
|
1041
1061
|
* @example
|
|
@@ -1070,7 +1090,7 @@ function createDatabaseQueueStore(input, driver) {
|
|
|
1070
1090
|
}).table("entries"));
|
|
1071
1091
|
}
|
|
1072
1092
|
/**
|
|
1073
|
-
*
|
|
1093
|
+
* Creates an in-memory {@link MemoryQueueStore} — the zero-plumbing DEFAULT queue store
|
|
1074
1094
|
* over a plain `Map` (the twin of {@link DatabaseQueueStore}).
|
|
1075
1095
|
*
|
|
1076
1096
|
* @remarks
|
|
@@ -1110,5 +1130,8 @@ exports.isQueueError = isQueueError;
|
|
|
1110
1130
|
exports.isQueueRetries = isQueueRetries;
|
|
1111
1131
|
exports.isQueueSignal = isQueueSignal;
|
|
1112
1132
|
exports.isQueueTimeout = isQueueTimeout;
|
|
1133
|
+
exports.isStoredEntry = isStoredEntry;
|
|
1134
|
+
exports.readOption = readOption;
|
|
1135
|
+
exports.validateOption = validateOption;
|
|
1113
1136
|
|
|
1114
1137
|
//# sourceMappingURL=index.cjs.map
|