@nwire/bullmq 0.10.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 +57 -0
- package/dist/queue-bullmq.d.ts +42 -0
- package/dist/queue-bullmq.js +85 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alex Gefter / 200apps Ltd.
|
|
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
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# @nwire/bullmq
|
|
2
|
+
|
|
3
|
+
> [BullMQ](https://docs.bullmq.io)-backed `QueueTransport` — Redis-durable background jobs.
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
Implements the `@nwire/queue` `QueueTransport` over BullMQ. One BullMQ Queue per queue name; one Worker per `subscribe()`. `enqueue` with `delayMs` translates to BullMQ's `delay` option; `messageId` becomes BullMQ's `jobId` for idempotency. Failed jobs flow into BullMQ's failed-jobs board after the runtime's retry+DLQ logic exhausts.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pnpm add @nwire/bullmq @nwire/queue bullmq
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick start
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { BullMQQueueTransport } from "@nwire/bullmq";
|
|
19
|
+
import { createQueueWorker } from "@nwire/queue";
|
|
20
|
+
import { app } from "./app";
|
|
21
|
+
|
|
22
|
+
const transport = new BullMQQueueTransport({
|
|
23
|
+
connection: { host: "localhost", port: 6379 },
|
|
24
|
+
prefix: "learnflow",
|
|
25
|
+
defaultConcurrency: 5,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const worker = createQueueWorker(app, transport, {
|
|
29
|
+
subscribe: [
|
|
30
|
+
{ queue: "emails", action: "sendWelcomeEmail" },
|
|
31
|
+
{ queue: "reports", action: "generateMonthlyReport", retry: { attempts: 3 } },
|
|
32
|
+
],
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
await worker.start();
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## API surface
|
|
39
|
+
|
|
40
|
+
- `BullMQQueueTransport({ connection, prefix?, defaultConcurrency? })` — implements `QueueTransport`.
|
|
41
|
+
|
|
42
|
+
## When to use
|
|
43
|
+
|
|
44
|
+
Any production queue workload — same handler shape as `InMemoryQueueTransport`, swapped by config.
|
|
45
|
+
|
|
46
|
+
## Within nwire-app
|
|
47
|
+
|
|
48
|
+
For developers using this package as part of the Nwire stack — register it via `app.use(...)` or it auto-wires when you compose `createApp({ modules })`.
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { createApp } from "@nwire/forge";
|
|
52
|
+
|
|
53
|
+
const app = createApp({
|
|
54
|
+
/* ...config... */
|
|
55
|
+
});
|
|
56
|
+
// Adapter/plugin wiring happens here when applicable.
|
|
57
|
+
```
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@nwire/bullmq` — BullMQ-backed QueueTransport for production.
|
|
3
|
+
*
|
|
4
|
+
* import { BullMQQueueTransport } from '@nwire/bullmq'
|
|
5
|
+
*
|
|
6
|
+
* const transport = new BullMQQueueTransport({
|
|
7
|
+
* connection: { host: 'localhost', port: 6379 }
|
|
8
|
+
* })
|
|
9
|
+
* const worker = createQueueWorker(app, transport, { subscribe: [...] })
|
|
10
|
+
* await worker.start()
|
|
11
|
+
*
|
|
12
|
+
* One BullMQ Queue per `queue` name; one BullMQ Worker per `subscribe()` call.
|
|
13
|
+
* `enqueue` with `delayMs` translates to BullMQ's `delay` option. Idempotency
|
|
14
|
+
* via `messageId` becomes BullMQ's `jobId`.
|
|
15
|
+
*
|
|
16
|
+
* Failed jobs flow into BullMQ's failed-jobs board automatically; the
|
|
17
|
+
* runtime's retry+DLQ logic still runs first (per-action retry policy) — by
|
|
18
|
+
* the time a job lands on BullMQ's failure list, the framework has already
|
|
19
|
+
* exhausted retries and dead-lettered.
|
|
20
|
+
*/
|
|
21
|
+
import { type ConnectionOptions } from "bullmq";
|
|
22
|
+
import type { QueueTransport, QueueMessage, QueueConsumer } from "@nwire/queue";
|
|
23
|
+
export interface BullMQQueueOptions {
|
|
24
|
+
readonly connection: ConnectionOptions;
|
|
25
|
+
/** Prefix every BullMQ key (multi-tenant isolation at the Redis level). Default: `'nwire'`. */
|
|
26
|
+
readonly prefix?: string;
|
|
27
|
+
/** Default worker concurrency. Default: 5. */
|
|
28
|
+
readonly defaultConcurrency?: number;
|
|
29
|
+
}
|
|
30
|
+
export declare class BullMQQueueTransport implements QueueTransport {
|
|
31
|
+
private readonly queues;
|
|
32
|
+
private readonly workers;
|
|
33
|
+
private readonly connection;
|
|
34
|
+
private readonly prefix;
|
|
35
|
+
private readonly defaultConcurrency;
|
|
36
|
+
private stopped;
|
|
37
|
+
constructor(options: BullMQQueueOptions);
|
|
38
|
+
private getOrCreateQueue;
|
|
39
|
+
enqueue<TInput>(queue: string, message: Omit<QueueMessage<TInput>, "queue">): Promise<void>;
|
|
40
|
+
subscribe<TInput>(queue: string, consumer: QueueConsumer<TInput>): void;
|
|
41
|
+
stop(): Promise<void>;
|
|
42
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@nwire/bullmq` — BullMQ-backed QueueTransport for production.
|
|
3
|
+
*
|
|
4
|
+
* import { BullMQQueueTransport } from '@nwire/bullmq'
|
|
5
|
+
*
|
|
6
|
+
* const transport = new BullMQQueueTransport({
|
|
7
|
+
* connection: { host: 'localhost', port: 6379 }
|
|
8
|
+
* })
|
|
9
|
+
* const worker = createQueueWorker(app, transport, { subscribe: [...] })
|
|
10
|
+
* await worker.start()
|
|
11
|
+
*
|
|
12
|
+
* One BullMQ Queue per `queue` name; one BullMQ Worker per `subscribe()` call.
|
|
13
|
+
* `enqueue` with `delayMs` translates to BullMQ's `delay` option. Idempotency
|
|
14
|
+
* via `messageId` becomes BullMQ's `jobId`.
|
|
15
|
+
*
|
|
16
|
+
* Failed jobs flow into BullMQ's failed-jobs board automatically; the
|
|
17
|
+
* runtime's retry+DLQ logic still runs first (per-action retry policy) — by
|
|
18
|
+
* the time a job lands on BullMQ's failure list, the framework has already
|
|
19
|
+
* exhausted retries and dead-lettered.
|
|
20
|
+
*/
|
|
21
|
+
import { Queue, Worker } from "bullmq";
|
|
22
|
+
export class BullMQQueueTransport {
|
|
23
|
+
queues = new Map();
|
|
24
|
+
workers = new Map();
|
|
25
|
+
connection;
|
|
26
|
+
prefix;
|
|
27
|
+
defaultConcurrency;
|
|
28
|
+
stopped = false;
|
|
29
|
+
constructor(options) {
|
|
30
|
+
this.connection = options.connection;
|
|
31
|
+
this.prefix = options.prefix ?? "nwire";
|
|
32
|
+
this.defaultConcurrency = options.defaultConcurrency ?? 5;
|
|
33
|
+
}
|
|
34
|
+
getOrCreateQueue(name) {
|
|
35
|
+
let q = this.queues.get(name);
|
|
36
|
+
if (!q) {
|
|
37
|
+
q = new Queue(name, { connection: this.connection, prefix: this.prefix });
|
|
38
|
+
this.queues.set(name, q);
|
|
39
|
+
}
|
|
40
|
+
return q;
|
|
41
|
+
}
|
|
42
|
+
async enqueue(queue, message) {
|
|
43
|
+
if (this.stopped) {
|
|
44
|
+
throw new Error("BullMQQueueTransport: enqueue after stop");
|
|
45
|
+
}
|
|
46
|
+
const q = this.getOrCreateQueue(queue);
|
|
47
|
+
await q.add(queue, { input: message.input, envelope: message.envelope }, {
|
|
48
|
+
jobId: message.messageId,
|
|
49
|
+
delay: message.delayMs && message.delayMs > 0 ? message.delayMs : undefined,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
subscribe(queue, consumer) {
|
|
53
|
+
if (this.stopped) {
|
|
54
|
+
throw new Error("BullMQQueueTransport: subscribe after stop");
|
|
55
|
+
}
|
|
56
|
+
const worker = new Worker(queue, async (job) => {
|
|
57
|
+
const payload = job.data;
|
|
58
|
+
await consumer({
|
|
59
|
+
queue,
|
|
60
|
+
input: payload.input,
|
|
61
|
+
envelope: payload.envelope,
|
|
62
|
+
messageId: job.id,
|
|
63
|
+
});
|
|
64
|
+
}, {
|
|
65
|
+
connection: this.connection,
|
|
66
|
+
prefix: this.prefix,
|
|
67
|
+
concurrency: this.defaultConcurrency,
|
|
68
|
+
});
|
|
69
|
+
const list = this.workers.get(queue) ?? [];
|
|
70
|
+
list.push(worker);
|
|
71
|
+
this.workers.set(queue, list);
|
|
72
|
+
}
|
|
73
|
+
async stop() {
|
|
74
|
+
this.stopped = true;
|
|
75
|
+
for (const list of this.workers.values()) {
|
|
76
|
+
for (const w of list)
|
|
77
|
+
await w.close();
|
|
78
|
+
}
|
|
79
|
+
for (const q of this.queues.values()) {
|
|
80
|
+
await q.close();
|
|
81
|
+
}
|
|
82
|
+
this.workers.clear();
|
|
83
|
+
this.queues.clear();
|
|
84
|
+
}
|
|
85
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nwire/bullmq",
|
|
3
|
+
"version": "0.10.0",
|
|
4
|
+
"description": "Nwire — BullMQ-backed QueueTransport for production. Redis-backed durable queues + delayed jobs (timer scheduler) + failed-jobs board.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"adapter",
|
|
7
|
+
"bullmq",
|
|
8
|
+
"nwire",
|
|
9
|
+
"queue",
|
|
10
|
+
"redis"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"README.md",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
],
|
|
18
|
+
"type": "module",
|
|
19
|
+
"main": "./dist/queue-bullmq.js",
|
|
20
|
+
"types": "./dist/queue-bullmq.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": "./dist/queue-bullmq.js",
|
|
24
|
+
"types": "./dist/queue-bullmq.d.ts"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"bullmq": "^5.67.3",
|
|
32
|
+
"@nwire/queue": "0.10.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/node": "^22.19.9",
|
|
36
|
+
"typescript": "^5.9.3",
|
|
37
|
+
"@nwire/test-kit": "0.10.0"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc && node ../../scripts/fix-dist-extensions.mjs dist",
|
|
41
|
+
"dev": "tsc --watch",
|
|
42
|
+
"typecheck": "tsc --noEmit"
|
|
43
|
+
}
|
|
44
|
+
}
|