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