@zudojs/queue 0.0.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +40 -8
- package/dist/deadLetter/deadLetter.core.js +1 -1
- package/dist/inMemoryQueue/inMemoryQueue.core.d.ts +95 -4
- package/dist/inMemoryQueue/inMemoryQueue.core.js +398 -73
- package/dist/inMemoryQueue/inMemoryQueue.processing.d.ts +53 -8
- package/dist/inMemoryQueue/inMemoryQueue.processing.js +83 -36
- package/dist/inMemoryQueue/inMemoryQueue.scheduling.d.ts +17 -1
- package/dist/inMemoryQueue/inMemoryQueue.scheduling.js +43 -11
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -2
- package/dist/job/job.core.js +20 -12
- package/dist/job/job.type.d.ts +3 -2
- package/dist/jobOptions/jobOptions.type.d.ts +12 -3
- package/dist/jobTypes/index.d.ts +1 -1
- package/dist/jobTypes/jobTypes.type.d.ts +9 -0
- package/dist/middleware/middleware.core.d.ts +10 -1
- package/dist/middleware/middleware.core.js +40 -17
- package/dist/processor/index.d.ts +1 -1
- package/dist/processor/index.js +1 -1
- package/dist/processor/processor.core.js +2 -0
- package/dist/processor/processor.type.d.ts +18 -1
- package/dist/processor/processor.type.js +15 -0
- package/dist/queue/index.d.ts +1 -1
- package/dist/queue/queue.core.js +2 -2
- package/dist/queue/queue.type.d.ts +108 -2
- package/dist/queueEmitter/queueEmitter.core.d.ts +22 -2
- package/dist/queueEmitter/queueEmitter.core.js +47 -7
- package/dist/queueManager/queueManager.core.js +24 -5
- package/dist/queueRegistry/queueRegistry.core.js +15 -0
- package/dist/queueRegistry/queueRegistry.type.d.ts +2 -0
- package/dist/retryPolicy/retryPolicy.core.d.ts +42 -3
- package/dist/retryPolicy/retryPolicy.core.js +82 -13
- package/dist/serializer/serializer.core.d.ts +2 -1
- package/dist/worker/worker.core.d.ts +4 -0
- package/dist/worker/worker.core.js +87 -23
- package/dist/worker/worker.type.d.ts +17 -3
- package/package.json +21 -14
- package/dist/.tsbuildinfo +0 -1
- package/dist/deadLetter/deadLetter.core.d.ts.map +0 -1
- package/dist/deadLetter/deadLetter.core.js.map +0 -1
- package/dist/deadLetter/deadLetter.type.d.ts.map +0 -1
- package/dist/deadLetter/deadLetter.type.js.map +0 -1
- package/dist/deadLetter/index.d.ts.map +0 -1
- package/dist/deadLetter/index.js.map +0 -1
- package/dist/inMemoryQueue/inMemoryQueue.core.d.ts.map +0 -1
- package/dist/inMemoryQueue/inMemoryQueue.core.js.map +0 -1
- package/dist/inMemoryQueue/inMemoryQueue.factory.d.ts.map +0 -1
- package/dist/inMemoryQueue/inMemoryQueue.factory.js.map +0 -1
- package/dist/inMemoryQueue/inMemoryQueue.processing.d.ts.map +0 -1
- package/dist/inMemoryQueue/inMemoryQueue.processing.js.map +0 -1
- package/dist/inMemoryQueue/inMemoryQueue.scheduling.d.ts.map +0 -1
- package/dist/inMemoryQueue/inMemoryQueue.scheduling.js.map +0 -1
- package/dist/inMemoryQueue/index.d.ts.map +0 -1
- package/dist/inMemoryQueue/index.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/job/index.d.ts.map +0 -1
- package/dist/job/index.js.map +0 -1
- package/dist/job/job.core.d.ts.map +0 -1
- package/dist/job/job.core.js.map +0 -1
- package/dist/job/job.type.d.ts.map +0 -1
- package/dist/job/job.type.js.map +0 -1
- package/dist/jobContext/index.d.ts.map +0 -1
- package/dist/jobContext/index.js.map +0 -1
- package/dist/jobContext/jobContext.core.d.ts.map +0 -1
- package/dist/jobContext/jobContext.core.js.map +0 -1
- package/dist/jobContext/jobContext.type.d.ts.map +0 -1
- package/dist/jobContext/jobContext.type.js.map +0 -1
- package/dist/jobOptions/index.d.ts.map +0 -1
- package/dist/jobOptions/index.js.map +0 -1
- package/dist/jobOptions/jobOptions.core.d.ts.map +0 -1
- package/dist/jobOptions/jobOptions.core.js.map +0 -1
- package/dist/jobOptions/jobOptions.type.d.ts.map +0 -1
- package/dist/jobOptions/jobOptions.type.js.map +0 -1
- package/dist/jobResult/index.d.ts.map +0 -1
- package/dist/jobResult/index.js.map +0 -1
- package/dist/jobResult/jobResult.core.d.ts.map +0 -1
- package/dist/jobResult/jobResult.core.js.map +0 -1
- package/dist/jobResult/jobResult.type.d.ts.map +0 -1
- package/dist/jobResult/jobResult.type.js.map +0 -1
- package/dist/jobTypes/index.d.ts.map +0 -1
- package/dist/jobTypes/index.js.map +0 -1
- package/dist/jobTypes/jobTypes.type.d.ts.map +0 -1
- package/dist/jobTypes/jobTypes.type.js.map +0 -1
- package/dist/middleware/index.d.ts.map +0 -1
- package/dist/middleware/index.js.map +0 -1
- package/dist/middleware/middleware.core.d.ts.map +0 -1
- package/dist/middleware/middleware.core.js.map +0 -1
- package/dist/middleware/middleware.type.d.ts.map +0 -1
- package/dist/middleware/middleware.type.js.map +0 -1
- package/dist/processor/index.d.ts.map +0 -1
- package/dist/processor/index.js.map +0 -1
- package/dist/processor/processor.core.d.ts.map +0 -1
- package/dist/processor/processor.core.js.map +0 -1
- package/dist/processor/processor.type.d.ts.map +0 -1
- package/dist/processor/processor.type.js.map +0 -1
- package/dist/queue/index.d.ts.map +0 -1
- package/dist/queue/index.js.map +0 -1
- package/dist/queue/queue.core.d.ts.map +0 -1
- package/dist/queue/queue.core.js.map +0 -1
- package/dist/queue/queue.type.d.ts.map +0 -1
- package/dist/queue/queue.type.js.map +0 -1
- package/dist/queueEmitter/index.d.ts.map +0 -1
- package/dist/queueEmitter/index.js.map +0 -1
- package/dist/queueEmitter/queueEmitter.core.d.ts.map +0 -1
- package/dist/queueEmitter/queueEmitter.core.js.map +0 -1
- package/dist/queueEmitter/queueEmitter.type.d.ts.map +0 -1
- package/dist/queueEmitter/queueEmitter.type.js.map +0 -1
- package/dist/queueEvents/index.d.ts +0 -8
- package/dist/queueEvents/index.d.ts.map +0 -1
- package/dist/queueEvents/index.js +0 -7
- package/dist/queueEvents/index.js.map +0 -1
- package/dist/queueEvents/queueEvents.core.d.ts +0 -6
- package/dist/queueEvents/queueEvents.core.d.ts.map +0 -1
- package/dist/queueEvents/queueEvents.core.js +0 -12
- package/dist/queueEvents/queueEvents.core.js.map +0 -1
- package/dist/queueEvents/queueEvents.type.d.ts +0 -97
- package/dist/queueEvents/queueEvents.type.d.ts.map +0 -1
- package/dist/queueEvents/queueEvents.type.js +0 -2
- package/dist/queueEvents/queueEvents.type.js.map +0 -1
- package/dist/queueManager/index.d.ts.map +0 -1
- package/dist/queueManager/index.js.map +0 -1
- package/dist/queueManager/queueManager.core.d.ts.map +0 -1
- package/dist/queueManager/queueManager.core.js.map +0 -1
- package/dist/queueManager/queueManager.type.d.ts.map +0 -1
- package/dist/queueManager/queueManager.type.js.map +0 -1
- package/dist/queueRegistry/index.d.ts.map +0 -1
- package/dist/queueRegistry/index.js.map +0 -1
- package/dist/queueRegistry/queueRegistry.core.d.ts.map +0 -1
- package/dist/queueRegistry/queueRegistry.core.js.map +0 -1
- package/dist/queueRegistry/queueRegistry.type.d.ts.map +0 -1
- package/dist/queueRegistry/queueRegistry.type.js.map +0 -1
- package/dist/retryPolicy/index.d.ts.map +0 -1
- package/dist/retryPolicy/index.js.map +0 -1
- package/dist/retryPolicy/retryPolicy.core.d.ts.map +0 -1
- package/dist/retryPolicy/retryPolicy.core.js.map +0 -1
- package/dist/serializer/index.d.ts.map +0 -1
- package/dist/serializer/index.js.map +0 -1
- package/dist/serializer/serializer.core.d.ts.map +0 -1
- package/dist/serializer/serializer.core.js.map +0 -1
- package/dist/serializer/serializer.type.d.ts.map +0 -1
- package/dist/serializer/serializer.type.js.map +0 -1
- package/dist/worker/index.d.ts.map +0 -1
- package/dist/worker/index.js.map +0 -1
- package/dist/worker/worker.core.d.ts.map +0 -1
- package/dist/worker/worker.core.js.map +0 -1
- package/dist/worker/worker.type.d.ts.map +0 -1
- package/dist/worker/worker.type.js.map +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zudojs Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -10,21 +10,48 @@ npm install @zudojs/queue
|
|
|
10
10
|
|
|
11
11
|
## Quick Start
|
|
12
12
|
|
|
13
|
+
Runs as written — queue names are branded, so build one with
|
|
14
|
+
`createQueueName`:
|
|
15
|
+
|
|
13
16
|
```typescript
|
|
14
|
-
import { createInMemoryQueue } from "@zudojs/queue";
|
|
17
|
+
import { createInMemoryQueue, createQueueName } from "@zudojs/queue";
|
|
18
|
+
|
|
19
|
+
interface Email {
|
|
20
|
+
to: string;
|
|
21
|
+
subject: string;
|
|
22
|
+
}
|
|
15
23
|
|
|
16
|
-
const queue = createInMemoryQueue("emails", {
|
|
24
|
+
const queue = createInMemoryQueue<Email>(createQueueName("emails"), {
|
|
17
25
|
concurrency: 5,
|
|
26
|
+
// Give jobs somewhere to log; without this `context.log()` goes nowhere.
|
|
27
|
+
logger: console,
|
|
18
28
|
});
|
|
19
29
|
|
|
20
|
-
|
|
21
|
-
|
|
30
|
+
// A processor may return a value, a JobResult, or nothing.
|
|
31
|
+
queue.process("send-email", async (job, context) => {
|
|
32
|
+
context.log("sending", { to: job.data.to });
|
|
33
|
+
return { delivered: job.data.to };
|
|
22
34
|
});
|
|
23
35
|
|
|
24
|
-
await queue.add(
|
|
25
|
-
|
|
26
|
-
subject: "Welcome",
|
|
27
|
-
|
|
36
|
+
await queue.add(
|
|
37
|
+
"send-email",
|
|
38
|
+
{ to: "user@example.com", subject: "Welcome" },
|
|
39
|
+
// Backoff accepts either BackoffType.FIXED or the plain string.
|
|
40
|
+
{ attempts: 3, backoff: { type: "exponential", delay: 500, jitter: "full" } },
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
// The queue polls in the background; give it a tick before reading counts.
|
|
44
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
45
|
+
|
|
46
|
+
// The queue polls in the background; give it a tick before reading counts.
|
|
47
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
48
|
+
|
|
49
|
+
// Jobs that exhaust their attempts land here rather than vanishing.
|
|
50
|
+
console.log((await queue.getDeadLetterJobs()).length);
|
|
51
|
+
console.log(await queue.getStats());
|
|
52
|
+
|
|
53
|
+
// Draining shutdown: in-flight jobs finish before the queue tears down.
|
|
54
|
+
await queue.close();
|
|
28
55
|
```
|
|
29
56
|
|
|
30
57
|
## Features
|
|
@@ -36,6 +63,11 @@ await queue.add("send-email", {
|
|
|
36
63
|
- Scheduled and delayed jobs
|
|
37
64
|
- Middleware pipeline for job processing
|
|
38
65
|
- Concurrency control
|
|
66
|
+
- Draining shutdown with a bounded `closeTimeout`, then cooperative abort
|
|
67
|
+
- Stalled-job reclaim (`stalledAfter`, `maxStalledCount`) for consumers that die
|
|
68
|
+
mid-job
|
|
69
|
+
- Bounded retention of settled jobs, so a long-lived queue does not grow without
|
|
70
|
+
limit
|
|
39
71
|
|
|
40
72
|
## Use Cases
|
|
41
73
|
|
|
@@ -3,6 +3,8 @@ import type { Job } from "../job/job.type.js";
|
|
|
3
3
|
import type { Queue, QueueOptions, QueueStats } from "../queue/queue.type.js";
|
|
4
4
|
import type { Processor } from "../processor/processor.type.js";
|
|
5
5
|
import type { JobOptions } from "../jobOptions/jobOptions.type.js";
|
|
6
|
+
import type { QueueMiddleware } from "../middleware/middleware.type.js";
|
|
7
|
+
import type { DeadLetterJob } from "../deadLetter/deadLetter.type.js";
|
|
6
8
|
/**
|
|
7
9
|
* In-memory queue implementation.
|
|
8
10
|
*
|
|
@@ -20,29 +22,118 @@ export declare class InMemoryQueue<TData = unknown> implements Queue<TData> {
|
|
|
20
22
|
private disposed;
|
|
21
23
|
private activeCount;
|
|
22
24
|
private pollTimer;
|
|
23
|
-
private scheduledTimers;
|
|
25
|
+
private readonly scheduledTimers;
|
|
26
|
+
private readonly retryTimers;
|
|
27
|
+
private readonly inFlight;
|
|
28
|
+
private readonly settledOrder;
|
|
29
|
+
/** How many times each job has been reclaimed after stalling. */
|
|
30
|
+
private readonly stalledCounts;
|
|
24
31
|
private readonly deduplicationIndex;
|
|
25
32
|
private readonly deadLetterStore;
|
|
26
|
-
private processedCount;
|
|
27
|
-
private succeededCount;
|
|
28
|
-
private failedCount;
|
|
29
33
|
private readonly emitter;
|
|
34
|
+
private readonly counters;
|
|
30
35
|
private emptySince;
|
|
31
36
|
private backoffMs;
|
|
32
37
|
constructor(name: QueueName, options?: QueueOptions);
|
|
33
38
|
add(jobName: string, data: TData, options?: JobOptions): Promise<Job<TData>>;
|
|
34
39
|
process(name: string, processor: Processor<TData>): void;
|
|
35
40
|
getJob(jobId: JobId): Promise<Job<TData> | null>;
|
|
41
|
+
/**
|
|
42
|
+
* Returns the job that would be processed next without claiming it.
|
|
43
|
+
*
|
|
44
|
+
* This is a read-only peek. Consumers that intend to run the job must
|
|
45
|
+
* use {@link claimNextJob}, which transitions it to `active` so no
|
|
46
|
+
* other consumer can pick up the same job.
|
|
47
|
+
*/
|
|
36
48
|
getNextJob(): Promise<Job<TData> | null>;
|
|
49
|
+
/**
|
|
50
|
+
* Atomically selects the next runnable job and marks it `active`.
|
|
51
|
+
*
|
|
52
|
+
* Claiming is what prevents two consumers — or one consumer polling in
|
|
53
|
+
* a loop — from processing the same job repeatedly.
|
|
54
|
+
*/
|
|
55
|
+
claimNextJob(): Promise<Job<TData> | null>;
|
|
56
|
+
/**
|
|
57
|
+
* Returns a claimed job to the waiting pool.
|
|
58
|
+
*/
|
|
59
|
+
releaseJob(jobId: JobId): Promise<boolean>;
|
|
37
60
|
getProcessor(name: string): Processor<TData> | undefined;
|
|
38
61
|
getStats(): Promise<QueueStats>;
|
|
62
|
+
/**
|
|
63
|
+
* Returns the jobs that exhausted their attempts.
|
|
64
|
+
*/
|
|
65
|
+
getDeadLetterJobs(): Promise<readonly DeadLetterJob<TData>[]>;
|
|
39
66
|
pause(): Promise<void>;
|
|
40
67
|
resume(): Promise<void>;
|
|
41
68
|
isPaused(): boolean;
|
|
69
|
+
isDisposed(): boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Closes the queue, draining in-flight jobs first.
|
|
72
|
+
*
|
|
73
|
+
* Nothing is torn down until running jobs have settled, so a job can
|
|
74
|
+
* never write back into a cleared queue. Jobs that outlast
|
|
75
|
+
* `closeTimeout` have their `AbortSignal` aborted and are then
|
|
76
|
+
* abandoned so shutdown cannot hang indefinitely.
|
|
77
|
+
*/
|
|
42
78
|
close(): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Waits for in-flight jobs to settle, aborting them past the timeout.
|
|
81
|
+
*/
|
|
82
|
+
private drain;
|
|
83
|
+
/**
|
|
84
|
+
* Generates a collision-resistant job id.
|
|
85
|
+
*/
|
|
86
|
+
private createJobId;
|
|
87
|
+
/**
|
|
88
|
+
* Round-trips a payload through the configured serializer.
|
|
89
|
+
*/
|
|
90
|
+
private encodePayload;
|
|
91
|
+
/**
|
|
92
|
+
* Selects the highest-priority job that is due and runnable.
|
|
93
|
+
*
|
|
94
|
+
* Ties on priority are broken by creation time, oldest first. The
|
|
95
|
+
* incumbent is tracked by reference rather than by a sentinel priority,
|
|
96
|
+
* so jobs with negative priorities are selectable like any other.
|
|
97
|
+
*/
|
|
98
|
+
private selectJob;
|
|
43
99
|
private startPolling;
|
|
44
100
|
private scheduleNextTick;
|
|
101
|
+
private scheduleTick;
|
|
45
102
|
private stopPolling;
|
|
103
|
+
/**
|
|
104
|
+
* Runs an already-claimed job through this queue's processing pipeline.
|
|
105
|
+
*
|
|
106
|
+
* The queue owns job state, so every consumer — the internal poller and
|
|
107
|
+
* any external `Worker` — must run jobs through here. A consumer that
|
|
108
|
+
* invokes a processor directly leaves the job stuck in `active` and
|
|
109
|
+
* skips retry, dead-lettering and middleware entirely.
|
|
110
|
+
*/
|
|
111
|
+
runJob(job: Job<TData>, options?: {
|
|
112
|
+
middleware?: readonly QueueMiddleware[];
|
|
113
|
+
signal?: AbortSignal;
|
|
114
|
+
/** Fallback timeout for a job that carries none of its own. */
|
|
115
|
+
timeoutMs?: number;
|
|
116
|
+
}): Promise<void>;
|
|
46
117
|
private processTick;
|
|
118
|
+
/**
|
|
119
|
+
* Returns jobs stuck in `active` to the waiting pool.
|
|
120
|
+
*
|
|
121
|
+
* A consumer can claim a job and then die, or be killed mid-run.
|
|
122
|
+
* Without this the job stays `active` forever and no one picks it up
|
|
123
|
+
* again. A job that stalls repeatedly is dead-lettered rather than
|
|
124
|
+
* cycled indefinitely.
|
|
125
|
+
*/
|
|
126
|
+
private reclaimStalledJobs;
|
|
127
|
+
/**
|
|
128
|
+
* Records a job that reached a terminal state and prunes history.
|
|
129
|
+
*
|
|
130
|
+
* Without this the queue retains every job it has ever run, and every
|
|
131
|
+
* deduplication key it has ever seen, for the life of the process.
|
|
132
|
+
*/
|
|
133
|
+
private recordSettled;
|
|
134
|
+
/**
|
|
135
|
+
* Drops the oldest terminal jobs beyond the retention limit.
|
|
136
|
+
*/
|
|
137
|
+
private pruneSettled;
|
|
47
138
|
}
|
|
48
139
|
//# sourceMappingURL=inMemoryQueue.core.d.ts.map
|