@orkestrel/queue 0.0.11 → 0.0.13
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 +12 -18
- package/dist/src/core/index.cjs +212 -174
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +255 -134
- package/dist/src/core/index.d.ts +255 -134
- package/dist/src/core/index.js +211 -176
- package/dist/src/core/index.js.map +1 -1
- package/package.json +17 -19
package/dist/src/core/index.cjs
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
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 queue failure carrying a lowercase machine-readable `code`, optional
|
|
10
|
+
* structured context, and an optional cause.
|
|
10
11
|
*
|
|
11
12
|
* @example
|
|
12
13
|
* ```ts
|
|
@@ -18,7 +19,7 @@ var QueueError = class extends Error {
|
|
|
18
19
|
code;
|
|
19
20
|
context;
|
|
20
21
|
/**
|
|
21
|
-
*
|
|
22
|
+
* Creates a queue error.
|
|
22
23
|
*
|
|
23
24
|
* @param message - Human-readable failure description
|
|
24
25
|
* @param options - Machine-readable category, optional context, and optional cause
|
|
@@ -31,10 +32,12 @@ var QueueError = class extends Error {
|
|
|
31
32
|
}
|
|
32
33
|
};
|
|
33
34
|
/**
|
|
34
|
-
*
|
|
35
|
+
* Determines whether an unknown value is a {@link QueueError}, staying total for a hostile
|
|
36
|
+
* value.
|
|
35
37
|
*
|
|
36
38
|
* @param value - The value to inspect
|
|
37
|
-
* @returns
|
|
39
|
+
* @returns True if the value is a real `QueueError` instance; false otherwise, including for
|
|
40
|
+
* a hostile value
|
|
38
41
|
*
|
|
39
42
|
* @example
|
|
40
43
|
* ```ts
|
|
@@ -55,10 +58,10 @@ function isQueueError(value) {
|
|
|
55
58
|
//#endregion
|
|
56
59
|
//#region src/core/validators.ts
|
|
57
60
|
/**
|
|
58
|
-
*
|
|
61
|
+
* Determines whether a value is a valid queue concurrency — a positive safe integer.
|
|
59
62
|
*
|
|
60
63
|
* @param value - Value to inspect
|
|
61
|
-
* @returns
|
|
64
|
+
* @returns True if the value is a positive safe integer; false otherwise
|
|
62
65
|
*
|
|
63
66
|
* @example
|
|
64
67
|
* ```ts
|
|
@@ -70,10 +73,10 @@ function isQueueConcurrency(value) {
|
|
|
70
73
|
return (0, _orkestrel_contract.isInteger)(value) && Number.isSafeInteger(value) && value > 0;
|
|
71
74
|
}
|
|
72
75
|
/**
|
|
73
|
-
*
|
|
76
|
+
* Determines whether a value is a valid queue retry count — a nonnegative safe integer.
|
|
74
77
|
*
|
|
75
78
|
* @param value - Value to inspect
|
|
76
|
-
* @returns
|
|
79
|
+
* @returns True if the value is a nonnegative safe integer; false otherwise
|
|
77
80
|
*
|
|
78
81
|
* @example
|
|
79
82
|
* ```ts
|
|
@@ -85,10 +88,12 @@ function isQueueRetries(value) {
|
|
|
85
88
|
return (0, _orkestrel_contract.isInteger)(value) && Number.isSafeInteger(value) && value >= 0;
|
|
86
89
|
}
|
|
87
90
|
/**
|
|
88
|
-
*
|
|
91
|
+
* Determines whether a value is a valid queue timeout — an integer count of milliseconds
|
|
92
|
+
* inside the native timer range.
|
|
89
93
|
*
|
|
90
94
|
* @param value - Value to inspect
|
|
91
|
-
* @returns
|
|
95
|
+
* @returns True if the value is an integer within the native timer range,
|
|
96
|
+
* inclusive; false otherwise
|
|
92
97
|
*
|
|
93
98
|
* @example
|
|
94
99
|
* ```ts
|
|
@@ -97,13 +102,14 @@ function isQueueRetries(value) {
|
|
|
97
102
|
* ```
|
|
98
103
|
*/
|
|
99
104
|
function isQueueTimeout(value) {
|
|
100
|
-
return (0, _orkestrel_contract.isFiniteNumber)(value) && (0, _orkestrel_contract.isInteger)(value) && value >= 0 && value <=
|
|
105
|
+
return (0, _orkestrel_contract.isFiniteNumber)(value) && (0, _orkestrel_contract.isInteger)(value) && value >= 0 && value <= _orkestrel_timeout.MAX_TIMEOUT_MS;
|
|
101
106
|
}
|
|
102
107
|
/**
|
|
103
|
-
*
|
|
108
|
+
* Determines whether a value is a native abort signal usable by the queue, testing the
|
|
109
|
+
* native brand rather than the shape.
|
|
104
110
|
*
|
|
105
111
|
* @param value - Value to inspect
|
|
106
|
-
* @returns
|
|
112
|
+
* @returns True if the value carries the native `AbortSignal` internal slot; false otherwise
|
|
107
113
|
*
|
|
108
114
|
* @example
|
|
109
115
|
* ```ts
|
|
@@ -121,10 +127,97 @@ function isQueueSignal(value) {
|
|
|
121
127
|
return false;
|
|
122
128
|
}
|
|
123
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Determines whether a value is a valid stored queue entry — a record holding a string
|
|
132
|
+
* `id`, an `input`, and a nonnegative safe-integer `attempts`.
|
|
133
|
+
*
|
|
134
|
+
* @remarks
|
|
135
|
+
* The single test for what a {@link StoredEntry} is, shared by the queue's `restore`
|
|
136
|
+
* boundary and the memory store's `save` boundary. `input` carries the caller's own
|
|
137
|
+
* payload type, so only its presence is checked.
|
|
138
|
+
*
|
|
139
|
+
* @param value - Value to inspect
|
|
140
|
+
* @returns True if the value is a record holding a string `id`, an `input` member, and a
|
|
141
|
+
* nonnegative safe-integer `attempts`; false otherwise
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```ts
|
|
145
|
+
* isStoredEntry({ id: 'job-1', input: 'task', attempts: 0 }) // true
|
|
146
|
+
* isStoredEntry({ id: 'job-1', input: 'task', attempts: -1 }) // false
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
function isStoredEntry(value) {
|
|
150
|
+
try {
|
|
151
|
+
if (!(0, _orkestrel_contract.isRecord)(value)) return false;
|
|
152
|
+
return (0, _orkestrel_contract.isString)(value.id) && "input" in value && isQueueRetries(value.attempts);
|
|
153
|
+
} catch {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
//#endregion
|
|
158
|
+
//#region src/core/helpers.ts
|
|
159
|
+
/**
|
|
160
|
+
* Reads one named option from a caller-supplied entry options object exactly once,
|
|
161
|
+
* containing a throwing getter as a coded failure.
|
|
162
|
+
*
|
|
163
|
+
* @remarks
|
|
164
|
+
* An absent options object reads nothing and yields `undefined`.
|
|
165
|
+
*
|
|
166
|
+
* @param options - The caller's own entry options, or `undefined`
|
|
167
|
+
* @param option - The option key to read
|
|
168
|
+
* @param message - The failure description the thrown error carries
|
|
169
|
+
* @returns The value the property held, or `undefined` when no options object was supplied
|
|
170
|
+
* @throws {QueueError} Thrown when reading the property throws, coded `invalid` with the
|
|
171
|
+
* option in its context.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* readOption({ retries: 2 }, 'retries', 'queue retries could not be read') // 2
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
function readOption(options, option, message) {
|
|
179
|
+
try {
|
|
180
|
+
return options?.[option];
|
|
181
|
+
} catch (error) {
|
|
182
|
+
throw new QueueError(message, {
|
|
183
|
+
code: "invalid",
|
|
184
|
+
cause: error,
|
|
185
|
+
context: { option }
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Validates one already-read queue option against its guard, and throws the coded invalid
|
|
191
|
+
* failure carrying the option and the refused value when the guard refuses it.
|
|
192
|
+
*
|
|
193
|
+
* @param value - The option value to check
|
|
194
|
+
* @param guard - The total guard the value must satisfy
|
|
195
|
+
* @param option - The option key the failure context reports
|
|
196
|
+
* @param message - The failure description the thrown error carries
|
|
197
|
+
* @returns The same value, narrowed by the guard
|
|
198
|
+
* @throws {QueueError} Thrown when the guard refuses the value, coded `invalid` with the
|
|
199
|
+
* option and the refused value in its context.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```ts
|
|
203
|
+
* validateOption(4, isQueueConcurrency, 'concurrency', 'queue concurrency must be positive') // 4
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
function validateOption(value, guard, option, message) {
|
|
207
|
+
if (guard(value)) return value;
|
|
208
|
+
throw new QueueError(message, {
|
|
209
|
+
code: "invalid",
|
|
210
|
+
context: {
|
|
211
|
+
option,
|
|
212
|
+
value
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
}
|
|
124
216
|
//#endregion
|
|
125
217
|
//#region src/core/Queue.ts
|
|
126
218
|
/**
|
|
127
|
-
*
|
|
219
|
+
* Represents a concurrent, cooperative FIFO job queue — a wake-park worker loop with
|
|
220
|
+
* retries, a per-attempt timeout, abort, and optional outstanding-work persistence.
|
|
128
221
|
*
|
|
129
222
|
* @typeParam TInput - The work input each entry carries
|
|
130
223
|
* @typeParam TResult - The value each entry resolves
|
|
@@ -159,42 +252,19 @@ var Queue = class {
|
|
|
159
252
|
#generation = 0;
|
|
160
253
|
#paused = false;
|
|
161
254
|
#stopped = false;
|
|
162
|
-
#aborted = false;
|
|
163
|
-
#destroyed = false;
|
|
164
255
|
#drained = true;
|
|
165
256
|
/**
|
|
166
|
-
*
|
|
257
|
+
* Creates a queue.
|
|
167
258
|
*
|
|
168
259
|
* @param options - Handler, validated execution limits, persistence, and observation hooks
|
|
169
260
|
*/
|
|
170
261
|
constructor(options) {
|
|
171
262
|
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
|
-
});
|
|
263
|
+
const concurrency = validateOption(configuredConcurrency === void 0 ? 1 : configuredConcurrency, isQueueConcurrency, "concurrency", "queue concurrency must be a positive safe integer");
|
|
180
264
|
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
|
-
});
|
|
265
|
+
const retries = validateOption(configuredRetries === void 0 ? 0 : configuredRetries, isQueueRetries, "retries", "queue retries must be a nonnegative safe integer");
|
|
189
266
|
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
|
-
});
|
|
267
|
+
const timeout = validateOption(configuredTimeout === void 0 ? 0 : configuredTimeout, isQueueTimeout, "timeout", "queue timeout must be within the native timer range");
|
|
198
268
|
const on = options.on;
|
|
199
269
|
const error = options.error;
|
|
200
270
|
this.#handler = options.handler;
|
|
@@ -220,93 +290,33 @@ var Queue = class {
|
|
|
220
290
|
return this.#paused;
|
|
221
291
|
}
|
|
222
292
|
get stopped() {
|
|
223
|
-
return this.#stopped || this.#
|
|
293
|
+
return this.#stopped || this.#abortPromise !== void 0;
|
|
224
294
|
}
|
|
225
295
|
/**
|
|
226
|
-
*
|
|
296
|
+
* Reserves and submits one FIFO entry.
|
|
227
297
|
*
|
|
228
298
|
* @param input - Handler input
|
|
229
299
|
* @param options - Optional id, retry/timeout overrides, and entry abort signal
|
|
230
300
|
* @returns The entry's settle-once execution promise
|
|
231
|
-
* @throws {QueueError}
|
|
301
|
+
* @throws {QueueError} Thrown synchronously when an option is inaccessible or invalid.
|
|
232
302
|
*/
|
|
233
303
|
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
|
-
});
|
|
304
|
+
const rawId = readOption(options, "id", "queue entry id could not be read");
|
|
305
|
+
const supplied = rawId === void 0 ? void 0 : validateOption(rawId, _orkestrel_contract.isString, "id", "queue entry id must be a string");
|
|
306
|
+
const rawRetries = readOption(options, "retries", "queue retries could not be read");
|
|
307
|
+
const retries = rawRetries === void 0 ? void 0 : validateOption(rawRetries, isQueueRetries, "retries", "queue retries must be a nonnegative safe integer");
|
|
308
|
+
const rawTimeout = readOption(options, "timeout", "queue timeout could not be read");
|
|
309
|
+
const timeout = rawTimeout === void 0 ? void 0 : validateOption(rawTimeout, isQueueTimeout, "timeout", "queue timeout must be within the native timer range");
|
|
310
|
+
const rawSignal = readOption(options, "signal", "queue signal could not be read");
|
|
311
|
+
const signal = rawSignal === void 0 ? void 0 : validateOption(rawSignal, isQueueSignal, "signal", "queue signal must be an AbortSignal");
|
|
302
312
|
const normalized = options === void 0 ? void 0 : Object.freeze({
|
|
303
313
|
...supplied === void 0 ? {} : { id: supplied },
|
|
304
314
|
...retries === void 0 ? {} : { retries },
|
|
305
315
|
...timeout === void 0 ? {} : { timeout },
|
|
306
316
|
...signal === void 0 ? {} : { signal }
|
|
307
317
|
});
|
|
308
|
-
if (this.#
|
|
309
|
-
if (this.#
|
|
318
|
+
if (this.#destroyPromise !== void 0) return Promise.reject(new QueueError("queue is destroyed", { code: "destroyed" }));
|
|
319
|
+
if (this.#abortPromise !== void 0) return Promise.reject(new QueueError("queue is aborted", { code: "aborted" }));
|
|
310
320
|
if (this.#stopped) return Promise.reject(new QueueError("queue is stopped", { code: "stopped" }));
|
|
311
321
|
const id = supplied ?? crypto.randomUUID();
|
|
312
322
|
if (this.#live.has(id)) return Promise.reject(new QueueError("queue entry id is already live", {
|
|
@@ -318,27 +328,30 @@ var Queue = class {
|
|
|
318
328
|
else this.#schedule(token);
|
|
319
329
|
return token.promise;
|
|
320
330
|
}
|
|
321
|
-
/**
|
|
331
|
+
/**
|
|
332
|
+
* Loads and accepts every outstanding stored entry from the current lifecycle generation.
|
|
333
|
+
*
|
|
334
|
+
* @returns A promise that settles after every loaded entry is reserved and accepted
|
|
335
|
+
* @throws {QueueError} Thrown when the store's load fails or returns a malformed entry.
|
|
336
|
+
*/
|
|
322
337
|
async restore() {
|
|
323
|
-
if (this.#store === void 0 || this.stopped || this.#
|
|
338
|
+
if (this.#store === void 0 || this.stopped || this.#destroyPromise !== void 0) return;
|
|
324
339
|
const generation = this.#generation;
|
|
325
340
|
const entries = [];
|
|
326
341
|
let malformed = false;
|
|
327
342
|
try {
|
|
328
343
|
const loaded = await this.#store.load();
|
|
329
344
|
for (const stored of loaded) {
|
|
330
|
-
const
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
345
|
+
const candidate = {
|
|
346
|
+
id: stored.id,
|
|
347
|
+
input: stored.input,
|
|
348
|
+
attempts: stored.attempts
|
|
349
|
+
};
|
|
350
|
+
if (!isStoredEntry(candidate)) {
|
|
334
351
|
malformed = true;
|
|
335
352
|
break;
|
|
336
353
|
}
|
|
337
|
-
entries.push(Object.freeze(
|
|
338
|
-
id,
|
|
339
|
-
input,
|
|
340
|
-
attempts
|
|
341
|
-
}));
|
|
354
|
+
entries.push(Object.freeze(candidate));
|
|
342
355
|
}
|
|
343
356
|
} catch (error) {
|
|
344
357
|
throw new QueueError("queue store load failed", {
|
|
@@ -351,23 +364,27 @@ var Queue = class {
|
|
|
351
364
|
code: "store",
|
|
352
365
|
context: { operation: "load" }
|
|
353
366
|
});
|
|
354
|
-
if (generation !== this.#generation || this.stopped || this.#
|
|
367
|
+
if (generation !== this.#generation || this.stopped || this.#destroyPromise !== void 0) return;
|
|
355
368
|
for (const stored of entries) {
|
|
356
|
-
if (generation !== this.#generation || this.stopped || this.#
|
|
369
|
+
if (generation !== this.#generation || this.stopped || this.#destroyPromise !== void 0) return;
|
|
357
370
|
if (this.#live.has(stored.id)) continue;
|
|
358
371
|
const token = this.#reserve(stored.id, stored.input, void 0, stored.attempts);
|
|
359
372
|
this.#accept(token);
|
|
360
373
|
token.promise.catch(() => {});
|
|
361
374
|
}
|
|
362
375
|
}
|
|
363
|
-
/**
|
|
376
|
+
/** Begins or restarts worker execution unless the queue is terminal. */
|
|
364
377
|
start() {
|
|
365
|
-
if (this.#
|
|
378
|
+
if (this.#abortPromise !== void 0 || this.#destroyPromise !== void 0) return;
|
|
366
379
|
this.#stopped = false;
|
|
367
380
|
this.#stopPromise = void 0;
|
|
368
381
|
this.#spawn();
|
|
369
382
|
}
|
|
370
|
-
/**
|
|
383
|
+
/**
|
|
384
|
+
* Rejects non-active work and awaits current-loop and durable cleanup quiescence.
|
|
385
|
+
*
|
|
386
|
+
* @returns The stable stop barrier
|
|
387
|
+
*/
|
|
371
388
|
stop() {
|
|
372
389
|
if (this.#destroyPromise !== void 0) return this.#destroyPromise;
|
|
373
390
|
if (this.#abortPromise !== void 0) return this.#abortPromise;
|
|
@@ -388,19 +405,19 @@ var Queue = class {
|
|
|
388
405
|
this.#settleBarrier(barrier, tasks, "queue stop cleanup failed");
|
|
389
406
|
return barrier.promise;
|
|
390
407
|
}
|
|
391
|
-
/**
|
|
408
|
+
/** Suspends new execution while leaving active entries untouched. */
|
|
392
409
|
pause() {
|
|
393
|
-
if (this.#paused || this.stopped || this.#
|
|
410
|
+
if (this.#paused || this.stopped || this.#destroyPromise !== void 0) return;
|
|
394
411
|
this.#paused = true;
|
|
395
412
|
}
|
|
396
|
-
/**
|
|
413
|
+
/** Continues execution after a pause. */
|
|
397
414
|
resume() {
|
|
398
|
-
if (!this.#paused || this.stopped || this.#
|
|
415
|
+
if (!this.#paused || this.stopped || this.#destroyPromise !== void 0) return;
|
|
399
416
|
this.#paused = false;
|
|
400
417
|
this.#wakeAll();
|
|
401
418
|
}
|
|
402
419
|
/**
|
|
403
|
-
*
|
|
420
|
+
* Cancels the queue and awaits owned cleanup.
|
|
404
421
|
*
|
|
405
422
|
* @param reason - Optional cause carried by the coded abort error
|
|
406
423
|
* @returns The stable abort barrier
|
|
@@ -409,7 +426,6 @@ var Queue = class {
|
|
|
409
426
|
if (this.#abortPromise !== void 0) return this.#abortPromise;
|
|
410
427
|
const barrier = Promise.withResolvers();
|
|
411
428
|
this.#abortPromise = barrier.promise;
|
|
412
|
-
this.#aborted = true;
|
|
413
429
|
this.#stopped = true;
|
|
414
430
|
this.#generation += 1;
|
|
415
431
|
const error = new QueueError(reason instanceof Error ? reason.message : "queue is aborted", {
|
|
@@ -430,9 +446,13 @@ var Queue = class {
|
|
|
430
446
|
this.#settleBarrier(barrier, tasks, "queue abort cleanup failed");
|
|
431
447
|
return barrier.promise;
|
|
432
448
|
}
|
|
433
|
-
/**
|
|
449
|
+
/**
|
|
450
|
+
* Rejects non-active work and awaits its durable cleanup.
|
|
451
|
+
*
|
|
452
|
+
* @returns A promise that settles after every affected removal completes
|
|
453
|
+
*/
|
|
434
454
|
clear() {
|
|
435
|
-
if (this.#
|
|
455
|
+
if (this.#destroyPromise !== void 0) return Promise.reject(new QueueError("queue is destroyed", { code: "destroyed" }));
|
|
436
456
|
const existing = [];
|
|
437
457
|
for (const [token, cleanup] of this.#cleanups) if (!this.#claims.has(token)) existing.push(cleanup);
|
|
438
458
|
const cleanup = [...this.#drain(new QueueError("queue is cleared", { code: "cleared" })), ...this.#cleanOrphans(false)];
|
|
@@ -442,12 +462,15 @@ var Queue = class {
|
|
|
442
462
|
...cleanup
|
|
443
463
|
], "queue clear cleanup failed");
|
|
444
464
|
}
|
|
445
|
-
/**
|
|
465
|
+
/**
|
|
466
|
+
* Tears down once, destroying the emitter only after cleanup finishes.
|
|
467
|
+
*
|
|
468
|
+
* @returns The stable destroy barrier
|
|
469
|
+
*/
|
|
446
470
|
destroy() {
|
|
447
471
|
if (this.#destroyPromise !== void 0) return this.#destroyPromise;
|
|
448
472
|
const barrier = Promise.withResolvers();
|
|
449
473
|
this.#destroyPromise = barrier.promise;
|
|
450
|
-
this.#destroyed = true;
|
|
451
474
|
const aborting = this.abort(new QueueError("queue is destroyed", { code: "destroyed" }));
|
|
452
475
|
this.#settleDestroy(barrier, aborting);
|
|
453
476
|
return barrier.promise;
|
|
@@ -505,7 +528,7 @@ var Queue = class {
|
|
|
505
528
|
this.#emitDrain();
|
|
506
529
|
return;
|
|
507
530
|
}
|
|
508
|
-
if (entry.settled || this.#
|
|
531
|
+
if (entry.settled || this.#abortPromise !== void 0 || this.#destroyPromise !== void 0 || this.#stopped) {
|
|
509
532
|
await this.#discard(token);
|
|
510
533
|
return;
|
|
511
534
|
}
|
|
@@ -520,7 +543,7 @@ var Queue = class {
|
|
|
520
543
|
this.#wake();
|
|
521
544
|
}
|
|
522
545
|
#spawn() {
|
|
523
|
-
if (this.#stopped || this.#
|
|
546
|
+
if (this.#stopped || this.#abortPromise !== void 0 || this.#destroyPromise !== void 0) return;
|
|
524
547
|
const demand = Math.min(this.#concurrency, this.#claims.size + this.#pending.length);
|
|
525
548
|
while (this.#workers.size < demand) {
|
|
526
549
|
const generation = this.#generation;
|
|
@@ -548,7 +571,7 @@ var Queue = class {
|
|
|
548
571
|
return new Promise((resolve) => this.#take(generation, resolve));
|
|
549
572
|
}
|
|
550
573
|
#take(generation, resolve) {
|
|
551
|
-
if (generation !== this.#generation || this.#stopped || this.#
|
|
574
|
+
if (generation !== this.#generation || this.#stopped || this.#abortPromise !== void 0 || this.#destroyPromise !== void 0) {
|
|
552
575
|
resolve(void 0);
|
|
553
576
|
return;
|
|
554
577
|
}
|
|
@@ -868,9 +891,9 @@ var Queue = class {
|
|
|
868
891
|
//#endregion
|
|
869
892
|
//#region src/core/stores/DatabaseQueueStore.ts
|
|
870
893
|
/**
|
|
871
|
-
*
|
|
872
|
-
* queue's durable state
|
|
873
|
-
* `TableInterface`.
|
|
894
|
+
* Represents the opt-in durable store for a queue's outstanding entries, backed by one table
|
|
895
|
+
* of the `@orkestrel/database` layer — a queue's durable state is a table, so persistence
|
|
896
|
+
* reduces to keyed CRUD over a `TableInterface`.
|
|
874
897
|
*
|
|
875
898
|
* @remarks
|
|
876
899
|
* The store is driver-agnostic: it holds a single {@link TableInterface} whose
|
|
@@ -878,9 +901,9 @@ var Queue = class {
|
|
|
878
901
|
* `save` upserts an entry by its primary key (`set`), `remove` drops one (`remove`),
|
|
879
902
|
* `load` returns every outstanding entry (`records` with no criteria), and `clear`
|
|
880
903
|
* empties the table. Reads are narrowed through the table's contract, so `load`
|
|
881
|
-
* returns typed {@link StoredEntry}`<TInput>[]` with no cast
|
|
904
|
+
* returns typed {@link StoredEntry}`<TInput>[]` with no cast — the table
|
|
882
905
|
* is created over the `{ id; input; attempts }` column map, which `Infer`s to exactly
|
|
883
|
-
* that row. The store holds only
|
|
906
|
+
* that row. The store holds only outstanding work (completed entries are `remove`d), so
|
|
884
907
|
* `load` on startup yields precisely the entries to resume.
|
|
885
908
|
*
|
|
886
909
|
* @typeParam TInput - The work input each {@link StoredEntry} carries
|
|
@@ -899,7 +922,7 @@ var Queue = class {
|
|
|
899
922
|
var DatabaseQueueStore = class {
|
|
900
923
|
#table;
|
|
901
924
|
/**
|
|
902
|
-
*
|
|
925
|
+
* Wraps a table as a queue store.
|
|
903
926
|
*
|
|
904
927
|
* @param table - The {@link TableInterface} holding the outstanding entries — its
|
|
905
928
|
* row is a {@link StoredEntry}`<TInput>` (the `{ id; input; attempts }` shape)
|
|
@@ -907,19 +930,19 @@ var DatabaseQueueStore = class {
|
|
|
907
930
|
constructor(table) {
|
|
908
931
|
this.#table = table;
|
|
909
932
|
}
|
|
910
|
-
/**
|
|
933
|
+
/** Upserts an entry by its `id` (a re-`save` of an existing `id` overwrites it). */
|
|
911
934
|
async save(entry) {
|
|
912
935
|
await this.#table.set(entry);
|
|
913
936
|
}
|
|
914
|
-
/**
|
|
937
|
+
/** Drops a finished entry by `id`; absent is a no-op (no throw). */
|
|
915
938
|
async remove(id) {
|
|
916
939
|
await this.#table.remove(id);
|
|
917
940
|
}
|
|
918
|
-
/**
|
|
941
|
+
/** Returns every outstanding entry — the work to resume after a restart. */
|
|
919
942
|
load() {
|
|
920
943
|
return this.#table.records();
|
|
921
944
|
}
|
|
922
|
-
/**
|
|
945
|
+
/** Empties the store — drops every outstanding entry. */
|
|
923
946
|
async clear() {
|
|
924
947
|
await this.#table.clear();
|
|
925
948
|
}
|
|
@@ -927,7 +950,8 @@ var DatabaseQueueStore = class {
|
|
|
927
950
|
//#endregion
|
|
928
951
|
//#region src/core/stores/MemoryQueueStore.ts
|
|
929
952
|
/**
|
|
930
|
-
*
|
|
953
|
+
* Represents an in-memory store of a queue's outstanding entries — a process-lifetime `Map`
|
|
954
|
+
* owning validated, immutable JSON snapshots.
|
|
931
955
|
*
|
|
932
956
|
* @typeParam TInput - The contract shape for each stored input
|
|
933
957
|
*
|
|
@@ -944,23 +968,26 @@ var MemoryQueueStore = class {
|
|
|
944
968
|
#contract;
|
|
945
969
|
#entries = /* @__PURE__ */ new Map();
|
|
946
970
|
/**
|
|
947
|
-
*
|
|
971
|
+
* Creates an in-memory queue store.
|
|
948
972
|
*
|
|
949
973
|
* @param input - Runtime contract for each stored input
|
|
950
974
|
*/
|
|
951
975
|
constructor(input) {
|
|
952
976
|
this.#contract = (0, _orkestrel_contract.createContract)(input);
|
|
953
977
|
}
|
|
954
|
-
/**
|
|
978
|
+
/** Upserts a validated, owned snapshot under the entry's id. */
|
|
955
979
|
save(entry) {
|
|
956
980
|
try {
|
|
957
|
-
const
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
981
|
+
const candidate = {
|
|
982
|
+
id: entry.id,
|
|
983
|
+
input: entry.input,
|
|
984
|
+
attempts: entry.attempts
|
|
985
|
+
};
|
|
986
|
+
if (!isStoredEntry(candidate)) throw new QueueError("queue store entry is invalid", {
|
|
961
987
|
code: "store",
|
|
962
988
|
context: { operation: "save" }
|
|
963
989
|
});
|
|
990
|
+
const { id, input, attempts } = candidate;
|
|
964
991
|
this.#entries.set(id, this.#snapshot(id, input, attempts, "save"));
|
|
965
992
|
return Promise.resolve();
|
|
966
993
|
} catch (error) {
|
|
@@ -971,12 +998,12 @@ var MemoryQueueStore = class {
|
|
|
971
998
|
}));
|
|
972
999
|
}
|
|
973
1000
|
}
|
|
974
|
-
/**
|
|
1001
|
+
/** Removes an entry; an absent id is a no-op. */
|
|
975
1002
|
remove(id) {
|
|
976
1003
|
this.#entries.delete(id);
|
|
977
1004
|
return Promise.resolve();
|
|
978
1005
|
}
|
|
979
|
-
/**
|
|
1006
|
+
/** Returns fresh immutable snapshots of every outstanding entry. */
|
|
980
1007
|
load() {
|
|
981
1008
|
try {
|
|
982
1009
|
const snapshots = [];
|
|
@@ -995,7 +1022,7 @@ var MemoryQueueStore = class {
|
|
|
995
1022
|
}));
|
|
996
1023
|
}
|
|
997
1024
|
}
|
|
998
|
-
/**
|
|
1025
|
+
/** Removes every outstanding entry. */
|
|
999
1026
|
clear() {
|
|
1000
1027
|
this.#entries.clear();
|
|
1001
1028
|
return Promise.resolve();
|
|
@@ -1019,37 +1046,44 @@ var MemoryQueueStore = class {
|
|
|
1019
1046
|
//#endregion
|
|
1020
1047
|
//#region src/core/factories.ts
|
|
1021
1048
|
/**
|
|
1022
|
-
*
|
|
1049
|
+
* Creates a concurrent, cooperative job queue — a bounded-concurrency engine that
|
|
1023
1050
|
* runs each enqueued input through a handler, with retries and a per-attempt
|
|
1024
1051
|
* timeout / abort, all over the L1 `Abort` / `Timeout` primitives.
|
|
1025
1052
|
*
|
|
1026
1053
|
* @remarks
|
|
1027
|
-
* Worker loops
|
|
1054
|
+
* Worker loops park on a wake list when no entry is ready and are woken one-at-a-time
|
|
1028
1055
|
* by `enqueue` / `resume`, so an idle queue consumes no CPU (no polling). `enqueue`
|
|
1029
1056
|
* returns a promise that settles when the entry finally completes, fails (after its
|
|
1030
1057
|
* retries), or is rejected by an abort. A queue-level `abort` never retries; the
|
|
1031
1058
|
* lifecycle (`start` / `stop` / `pause` / `resume` / `abort` / `clear` / `destroy`)
|
|
1032
|
-
* follows
|
|
1059
|
+
* follows the fixed lifecycle vocabulary. The queue is observable: a typed `emitter` surfaces
|
|
1033
1060
|
* `enqueue` / `start` / `retry` / `success` / `failure` / `abort` / `drain`.
|
|
1034
1061
|
*
|
|
1035
1062
|
* @typeParam TInput - The work input each entry carries
|
|
1036
1063
|
* @typeParam TResult - The value the handler resolves for an entry
|
|
1037
|
-
* @param options - The `handler` plus optional `concurrency`
|
|
1038
|
-
*
|
|
1064
|
+
* @param options - The `handler` plus optional `concurrency`, `retries`, and a default
|
|
1065
|
+
* per-attempt `timeout` in milliseconds. Default: `concurrency` `1`, `retries` `0`,
|
|
1066
|
+
* `timeout` `0`.
|
|
1039
1067
|
* @returns A working {@link QueueInterface}
|
|
1040
1068
|
*
|
|
1041
|
-
* @example
|
|
1069
|
+
* @example Create a queue
|
|
1042
1070
|
* ```ts
|
|
1043
1071
|
* import { createQueue } from '@orkestrel/queue'
|
|
1044
1072
|
*
|
|
1045
|
-
*
|
|
1073
|
+
* // Ordered (concurrency defaults to 1): each entry runs to completion before the next.
|
|
1074
|
+
* const queue = createQueue<Job, Output>({ handler: (job) => run(job) })
|
|
1075
|
+
*
|
|
1076
|
+
* const output = await queue.enqueue(job)
|
|
1077
|
+
*
|
|
1078
|
+
* // Bounded, retried, and time-boxed: four in flight, two extra attempts, 5s per attempt.
|
|
1079
|
+
* const fetches = createQueue<string, number>({
|
|
1046
1080
|
* handler: async (url, { signal }) => (await fetch(url, { signal })).status,
|
|
1047
1081
|
* concurrency: 4,
|
|
1048
1082
|
* retries: 2,
|
|
1049
1083
|
* timeout: 5_000,
|
|
1050
1084
|
* })
|
|
1051
1085
|
*
|
|
1052
|
-
* const status = await
|
|
1086
|
+
* const status = await fetches.enqueue('https://example.com')
|
|
1053
1087
|
* ```
|
|
1054
1088
|
*/
|
|
1055
1089
|
function createQueue(options) {
|
|
@@ -1070,8 +1104,9 @@ function createDatabaseQueueStore(input, driver) {
|
|
|
1070
1104
|
}).table("entries"));
|
|
1071
1105
|
}
|
|
1072
1106
|
/**
|
|
1073
|
-
*
|
|
1074
|
-
*
|
|
1107
|
+
* Creates an in-memory {@link MemoryQueueStore} over a plain `Map` — the zero-plumbing
|
|
1108
|
+
* {@link QueueStoreInterface} a queue takes when its outstanding entries need not outlive
|
|
1109
|
+
* the process, and the twin of {@link DatabaseQueueStore}.
|
|
1075
1110
|
*
|
|
1076
1111
|
* @remarks
|
|
1077
1112
|
* The entries live in a process-lifetime `Map` and are gone when the process exits. The
|
|
@@ -1110,5 +1145,8 @@ exports.isQueueError = isQueueError;
|
|
|
1110
1145
|
exports.isQueueRetries = isQueueRetries;
|
|
1111
1146
|
exports.isQueueSignal = isQueueSignal;
|
|
1112
1147
|
exports.isQueueTimeout = isQueueTimeout;
|
|
1148
|
+
exports.isStoredEntry = isStoredEntry;
|
|
1149
|
+
exports.readOption = readOption;
|
|
1150
|
+
exports.validateOption = validateOption;
|
|
1113
1151
|
|
|
1114
1152
|
//# sourceMappingURL=index.cjs.map
|