@motiadev/adapter-bullmq-events 0.14.0-beta.165-917560 → 0.14.0-beta.165-256670
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/dist/bullmq-event-adapter.d.ts +27 -0
- package/dist/bullmq-event-adapter.d.ts.map +1 -0
- package/dist/bullmq-event-adapter.js +75 -0
- package/dist/config-builder.d.ts +6 -0
- package/dist/config-builder.d.ts.map +1 -0
- package/dist/config-builder.js +29 -0
- package/dist/connection-manager.d.ts +10 -0
- package/dist/connection-manager.d.ts.map +1 -0
- package/dist/connection-manager.js +39 -0
- package/dist/constants.d.ts +14 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +16 -0
- package/dist/dlq-manager.d.ts +22 -0
- package/dist/dlq-manager.d.ts.map +1 -0
- package/dist/dlq-manager.js +112 -0
- package/dist/errors.d.ts +14 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +35 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/queue-manager.d.ts +20 -0
- package/dist/queue-manager.d.ts.map +1 -0
- package/dist/queue-manager.js +85 -0
- package/dist/types.d.ts +23 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/worker-manager.d.ts +38 -0
- package/dist/worker-manager.d.ts.map +1 -0
- package/dist/worker-manager.js +136 -0
- package/package.json +7 -11
- package/tsconfig.json +2 -2
- package/dist/bullmq-event-adapter.d.mts +0 -31
- package/dist/bullmq-event-adapter.d.mts.map +0 -1
- package/dist/bullmq-event-adapter.mjs +0 -74
- package/dist/bullmq-event-adapter.mjs.map +0 -1
- package/dist/config-builder.d.mts +0 -9
- package/dist/config-builder.d.mts.map +0 -1
- package/dist/config-builder.mjs +0 -28
- package/dist/config-builder.mjs.map +0 -1
- package/dist/connection-manager.mjs +0 -35
- package/dist/connection-manager.mjs.map +0 -1
- package/dist/constants.mjs +0 -18
- package/dist/constants.mjs.map +0 -1
- package/dist/dlq-manager.d.mts +0 -26
- package/dist/dlq-manager.d.mts.map +0 -1
- package/dist/dlq-manager.mjs +0 -105
- package/dist/dlq-manager.mjs.map +0 -1
- package/dist/errors.mjs +0 -31
- package/dist/errors.mjs.map +0 -1
- package/dist/index.d.mts +0 -6
- package/dist/index.mjs +0 -6
- package/dist/queue-manager.d.mts +0 -24
- package/dist/queue-manager.d.mts.map +0 -1
- package/dist/queue-manager.mjs +0 -83
- package/dist/queue-manager.mjs.map +0 -1
- package/dist/types.d.mts +0 -27
- package/dist/types.d.mts.map +0 -1
- package/dist/worker-manager.d.mts +0 -41
- package/dist/worker-manager.d.mts.map +0 -1
- package/dist/worker-manager.mjs +0 -130
- package/dist/worker-manager.mjs.map +0 -1
- package/tsdown.config.ts +0 -17
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WorkerManager = void 0;
|
|
4
|
+
const bullmq_1 = require("bullmq");
|
|
5
|
+
const uuid_1 = require("uuid");
|
|
6
|
+
const constants_1 = require("./constants");
|
|
7
|
+
const errors_1 = require("./errors");
|
|
8
|
+
class WorkerManager {
|
|
9
|
+
constructor(connection, config, getQueueName, dlqManager) {
|
|
10
|
+
this.workers = new Map();
|
|
11
|
+
this.topicSubscriptions = new Map();
|
|
12
|
+
this.connection = connection;
|
|
13
|
+
this.config = config;
|
|
14
|
+
this.getQueueName = getQueueName;
|
|
15
|
+
this.dlqManager = dlqManager ?? null;
|
|
16
|
+
}
|
|
17
|
+
createWorker(topic, stepName, handler, options) {
|
|
18
|
+
const id = (0, uuid_1.v4)();
|
|
19
|
+
const queueName = this.getQueueName(topic, stepName);
|
|
20
|
+
this.addTopicSubscription(topic, id);
|
|
21
|
+
const concurrency = options?.type === 'fifo' ? constants_1.FIFO_CONCURRENCY : this.config.concurrency;
|
|
22
|
+
const attempts = options?.maxRetries != null ? options.maxRetries + 1 : this.config.defaultJobOptions.attempts;
|
|
23
|
+
const lockDuration = options?.visibilityTimeout ? options.visibilityTimeout * constants_1.MILLISECONDS_PER_SECOND : undefined;
|
|
24
|
+
const worker = new bullmq_1.Worker(queueName, async (job) => {
|
|
25
|
+
const eventData = job.data;
|
|
26
|
+
const event = {
|
|
27
|
+
topic: eventData.topic,
|
|
28
|
+
data: eventData.data,
|
|
29
|
+
traceId: eventData.traceId,
|
|
30
|
+
flows: eventData.flows,
|
|
31
|
+
messageGroupId: eventData.messageGroupId,
|
|
32
|
+
};
|
|
33
|
+
await handler(event);
|
|
34
|
+
}, {
|
|
35
|
+
connection: this.connection,
|
|
36
|
+
prefix: this.config.prefix,
|
|
37
|
+
concurrency,
|
|
38
|
+
lockDuration,
|
|
39
|
+
removeOnComplete: this.config.defaultJobOptions.removeOnComplete,
|
|
40
|
+
removeOnFail: this.config.defaultJobOptions.removeOnFail,
|
|
41
|
+
});
|
|
42
|
+
this.setupWorkerHandlers(worker, topic, stepName, attempts ?? 3);
|
|
43
|
+
const handle = {
|
|
44
|
+
topic,
|
|
45
|
+
id,
|
|
46
|
+
unsubscribe: async () => {
|
|
47
|
+
await this.removeWorker(handle.id);
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
const workerInfo = {
|
|
51
|
+
worker,
|
|
52
|
+
topic,
|
|
53
|
+
stepName,
|
|
54
|
+
handle,
|
|
55
|
+
queueConfig: options,
|
|
56
|
+
};
|
|
57
|
+
this.workers.set(id, workerInfo);
|
|
58
|
+
return handle;
|
|
59
|
+
}
|
|
60
|
+
getSubscribers(topic) {
|
|
61
|
+
const subscriptionIds = this.topicSubscriptions.get(topic);
|
|
62
|
+
if (!subscriptionIds || subscriptionIds.size === 0) {
|
|
63
|
+
return [];
|
|
64
|
+
}
|
|
65
|
+
return Array.from(subscriptionIds)
|
|
66
|
+
.map((id) => this.workers.get(id))
|
|
67
|
+
.filter((info) => info !== undefined)
|
|
68
|
+
.map((info) => ({ topic: info.topic, stepName: info.stepName, queueConfig: info.queueConfig }));
|
|
69
|
+
}
|
|
70
|
+
getWorkerInfo(id) {
|
|
71
|
+
return this.workers.get(id);
|
|
72
|
+
}
|
|
73
|
+
async removeWorker(id) {
|
|
74
|
+
const workerInfo = this.workers.get(id);
|
|
75
|
+
if (!workerInfo) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
this.removeTopicSubscription(workerInfo.topic, id);
|
|
79
|
+
await workerInfo.worker.close();
|
|
80
|
+
this.workers.delete(id);
|
|
81
|
+
}
|
|
82
|
+
async closeAll() {
|
|
83
|
+
const promises = Array.from(this.workers.values()).map((info) => info.worker.close().catch((err) => {
|
|
84
|
+
console.error(`[BullMQ] Error closing worker for topic ${info.topic}, step ${info.stepName}:`, err);
|
|
85
|
+
}));
|
|
86
|
+
await Promise.allSettled(promises);
|
|
87
|
+
this.workers.clear();
|
|
88
|
+
this.topicSubscriptions.clear();
|
|
89
|
+
}
|
|
90
|
+
getSubscriptionCount(topic) {
|
|
91
|
+
return Array.from(this.workers.values()).filter((w) => w.topic === topic).length;
|
|
92
|
+
}
|
|
93
|
+
listTopics() {
|
|
94
|
+
return Array.from(new Set(Array.from(this.workers.values()).map((w) => w.topic)));
|
|
95
|
+
}
|
|
96
|
+
addTopicSubscription(topic, id) {
|
|
97
|
+
if (!this.topicSubscriptions.has(topic)) {
|
|
98
|
+
this.topicSubscriptions.set(topic, new Set());
|
|
99
|
+
}
|
|
100
|
+
this.topicSubscriptions.get(topic)?.add(id);
|
|
101
|
+
}
|
|
102
|
+
removeTopicSubscription(topic, id) {
|
|
103
|
+
const subscriptions = this.topicSubscriptions.get(topic);
|
|
104
|
+
if (subscriptions) {
|
|
105
|
+
subscriptions.delete(id);
|
|
106
|
+
if (subscriptions.size === 0) {
|
|
107
|
+
this.topicSubscriptions.delete(topic);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
setupWorkerHandlers(worker, topic, stepName, attempts) {
|
|
112
|
+
worker.on('error', (err) => {
|
|
113
|
+
const error = new errors_1.WorkerCreationError(topic, stepName, err);
|
|
114
|
+
console.error(`[BullMQ] Worker error for topic ${topic}, step ${stepName}:`, error);
|
|
115
|
+
});
|
|
116
|
+
worker.on('failed', async (job, err) => {
|
|
117
|
+
if (job) {
|
|
118
|
+
const attemptsMade = job.attemptsMade || 0;
|
|
119
|
+
if (attemptsMade >= attempts) {
|
|
120
|
+
if (this.dlqManager) {
|
|
121
|
+
const eventData = job.data;
|
|
122
|
+
const event = {
|
|
123
|
+
topic: eventData.topic || topic,
|
|
124
|
+
data: eventData.data,
|
|
125
|
+
traceId: eventData.traceId || 'unknown',
|
|
126
|
+
...(eventData.flows && { flows: eventData.flows }),
|
|
127
|
+
...(eventData.messageGroupId && { messageGroupId: eventData.messageGroupId }),
|
|
128
|
+
};
|
|
129
|
+
await this.dlqManager.moveToDLQ(topic, stepName, event, err, attemptsMade, job.id);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
exports.WorkerManager = WorkerManager;
|
package/package.json
CHANGED
|
@@ -1,29 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@motiadev/adapter-bullmq-events",
|
|
3
3
|
"description": "BullMQ event adapter for Motia framework, enabling distributed event handling with advanced features like retries, priorities, delays, and FIFO queues.",
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"types": "dist/index.d.mts",
|
|
8
|
-
"version": "0.14.0-beta.165-917560",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"version": "0.14.0-beta.165-256670",
|
|
9
7
|
"dependencies": {
|
|
10
8
|
"bullmq": "^5.63.0",
|
|
11
9
|
"ioredis": "^5.8.2",
|
|
12
10
|
"uuid": "^11.1.0",
|
|
13
|
-
"@motiadev/core": "0.14.0-beta.165-
|
|
11
|
+
"@motiadev/core": "0.14.0-beta.165-256670"
|
|
14
12
|
},
|
|
15
13
|
"devDependencies": {
|
|
16
|
-
"
|
|
17
|
-
"tsdown": "^0.16.6",
|
|
14
|
+
"@types/node": "^22.10.2",
|
|
18
15
|
"typescript": "^5.7.2"
|
|
19
16
|
},
|
|
20
17
|
"peerDependencies": {
|
|
21
18
|
"@motiadev/core": ">=0.8.0"
|
|
22
19
|
},
|
|
23
20
|
"scripts": {
|
|
24
|
-
"build": "
|
|
25
|
-
"dev": "tsdown --watch",
|
|
21
|
+
"build": "rm -rf dist && tsc",
|
|
26
22
|
"lint": "biome check .",
|
|
27
|
-
"
|
|
23
|
+
"watch": "tsc --watch"
|
|
28
24
|
}
|
|
29
25
|
}
|
package/tsconfig.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"target": "ES2020",
|
|
4
|
-
"module": "
|
|
4
|
+
"module": "commonjs",
|
|
5
5
|
"lib": ["ES2020"],
|
|
6
6
|
"declaration": true,
|
|
7
7
|
"declarationMap": true,
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"skipLibCheck": true,
|
|
13
13
|
"forceConsistentCasingInFileNames": true,
|
|
14
14
|
"resolveJsonModule": true,
|
|
15
|
-
"moduleResolution": "
|
|
15
|
+
"moduleResolution": "node"
|
|
16
16
|
},
|
|
17
17
|
"include": ["src/**/*"],
|
|
18
18
|
"exclude": ["node_modules", "dist"]
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { BullMQEventAdapterConfig } from "./types.mjs";
|
|
2
|
-
import { DLQManager } from "./dlq-manager.mjs";
|
|
3
|
-
import { WorkerManager } from "./worker-manager.mjs";
|
|
4
|
-
import { QueueManager } from "./queue-manager.mjs";
|
|
5
|
-
import { Redis } from "ioredis";
|
|
6
|
-
import { Event, EventAdapter, QueueConfig, SubscriptionHandle } from "@motiadev/core";
|
|
7
|
-
|
|
8
|
-
//#region src/bullmq-event-adapter.d.ts
|
|
9
|
-
declare class BullMQEventAdapter implements EventAdapter {
|
|
10
|
-
private readonly connectionManager;
|
|
11
|
-
private readonly _queueManager;
|
|
12
|
-
private readonly _workerManager;
|
|
13
|
-
private readonly _dlqManager;
|
|
14
|
-
private readonly _config;
|
|
15
|
-
constructor(config: BullMQEventAdapterConfig);
|
|
16
|
-
get connection(): Redis;
|
|
17
|
-
get prefix(): string;
|
|
18
|
-
get dlqSuffix(): string;
|
|
19
|
-
get queueManager(): QueueManager;
|
|
20
|
-
get workerManager(): WorkerManager;
|
|
21
|
-
get dlqManager(): DLQManager;
|
|
22
|
-
emit<TData>(event: Event<TData>): Promise<void>;
|
|
23
|
-
subscribe<TData>(topic: string, stepName: string, handler: (event: Event<TData>) => void | Promise<void>, options?: QueueConfig): Promise<SubscriptionHandle>;
|
|
24
|
-
unsubscribe(handle: SubscriptionHandle): Promise<void>;
|
|
25
|
-
shutdown(): Promise<void>;
|
|
26
|
-
getSubscriptionCount(topic: string): Promise<number>;
|
|
27
|
-
listTopics(): Promise<string[]>;
|
|
28
|
-
}
|
|
29
|
-
//#endregion
|
|
30
|
-
export { BullMQEventAdapter };
|
|
31
|
-
//# sourceMappingURL=bullmq-event-adapter.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"bullmq-event-adapter.d.mts","names":[],"sources":["../src/bullmq-event-adapter.ts"],"sourcesContent":[],"mappings":";;;;;;;;cASa,kBAAA,YAA8B;;EAA9B,iBAAA,aAAmB;EAOV,iBAAA,cAAA;EAaF,iBAAA,WAAA;EAYE,iBAAA,OAAA;EAIC,WAAA,CAAA,MAAA,EA7BD,wBA6BC;EAIH,IAAA,UAAA,CAAA,CAAA,EApBA,KAoBA;EAIa,IAAA,MAAA,CAAA,CAAA,EAAA,MAAA;EAAN,IAAA,SAAA,CAAA,CAAA,EAAA,MAAA;EAAe,IAAA,YAAA,CAAA,CAAA,EAZpB,YAYoB;EAYf,IAAA,aAAA,CAAA,CAAA,EApBJ,aAoBI;EAAN,IAAA,UAAA,CAAA,CAAA,EAhBD,UAgBC;EAAwB,IAAA,CAAA,KAAA,CAAA,CAAA,KAAA,EAZlB,KAYkB,CAZZ,KAYY,CAAA,CAAA,EAZH,OAYG,CAAA,IAAA,CAAA;EAC/B,SAAA,CAAA,KAAA,CAAA,CAAA,KAAA,EAAA,MAAA,EAAA,QAAA,EAAA,MAAA,EAAA,OAAA,EAAA,CAAA,KAAA,EADO,KACP,CADa,KACb,CAAA,EAAA,GAAA,IAAA,GAD+B,OAC/B,CAAA,IAAA,CAAA,EAAA,OAAA,CAAA,EAAA,WAAA,CAAA,EACT,OADS,CACD,kBADC,CAAA;EACD,WAAA,CAAA,MAAA,EAOe,kBAPf,CAAA,EAOoC,OAPpC,CAAA,IAAA,CAAA;EAAR,QAAA,CAAA,CAAA,EAgBe,OAhBf,CAAA,IAAA,CAAA;EAOuB,oBAAA,CAAA,KAAA,EAAA,MAAA,CAAA,EAuBiB,OAvBjB,CAAA,MAAA,CAAA;EAAqB,UAAA,CAAA,CAAA,EA2B3B,OA3B2B,CAAA,MAAA,EAAA,CAAA"}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { buildConfig } from "./config-builder.mjs";
|
|
2
|
-
import { ConnectionManager } from "./connection-manager.mjs";
|
|
3
|
-
import { DLQManager } from "./dlq-manager.mjs";
|
|
4
|
-
import { QueueManager } from "./queue-manager.mjs";
|
|
5
|
-
import { WorkerManager } from "./worker-manager.mjs";
|
|
6
|
-
|
|
7
|
-
//#region src/bullmq-event-adapter.ts
|
|
8
|
-
var BullMQEventAdapter = class {
|
|
9
|
-
constructor(config) {
|
|
10
|
-
this._config = buildConfig(config);
|
|
11
|
-
this.connectionManager = new ConnectionManager(config.connection);
|
|
12
|
-
this._queueManager = new QueueManager(this.connectionManager.connection, this._config);
|
|
13
|
-
this._dlqManager = new DLQManager(this.connectionManager.connection, this._config);
|
|
14
|
-
this._workerManager = new WorkerManager(this.connectionManager.connection, this._config, (topic, stepName) => this._queueManager.getQueueName(topic, stepName), this._dlqManager);
|
|
15
|
-
}
|
|
16
|
-
get connection() {
|
|
17
|
-
return this.connectionManager.connection;
|
|
18
|
-
}
|
|
19
|
-
get prefix() {
|
|
20
|
-
return this._config.prefix;
|
|
21
|
-
}
|
|
22
|
-
get dlqSuffix() {
|
|
23
|
-
return this._config.dlq.suffix;
|
|
24
|
-
}
|
|
25
|
-
get queueManager() {
|
|
26
|
-
return this._queueManager;
|
|
27
|
-
}
|
|
28
|
-
get workerManager() {
|
|
29
|
-
return this._workerManager;
|
|
30
|
-
}
|
|
31
|
-
get dlqManager() {
|
|
32
|
-
return this._dlqManager;
|
|
33
|
-
}
|
|
34
|
-
async emit(event) {
|
|
35
|
-
const subscribers = this._workerManager.getSubscribers(event.topic);
|
|
36
|
-
if (subscribers.length === 0) return;
|
|
37
|
-
await this._queueManager.enqueueToAll(event, subscribers);
|
|
38
|
-
}
|
|
39
|
-
async subscribe(topic, stepName, handler, options) {
|
|
40
|
-
const queueName = this._queueManager.getQueueName(topic, stepName);
|
|
41
|
-
this._queueManager.getQueue(queueName);
|
|
42
|
-
return this._workerManager.createWorker(topic, stepName, handler, options);
|
|
43
|
-
}
|
|
44
|
-
async unsubscribe(handle) {
|
|
45
|
-
const workerInfo = this._workerManager.getWorkerInfo(handle.id);
|
|
46
|
-
if (workerInfo) {
|
|
47
|
-
const queueName = this._queueManager.getQueueName(workerInfo.topic, workerInfo.stepName);
|
|
48
|
-
await this._queueManager.closeQueue(queueName);
|
|
49
|
-
await this._workerManager.removeWorker(handle.id);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
async shutdown() {
|
|
53
|
-
await Promise.allSettled([
|
|
54
|
-
this._workerManager.closeAll(),
|
|
55
|
-
this._queueManager.closeAll(),
|
|
56
|
-
this._dlqManager.closeAll()
|
|
57
|
-
]);
|
|
58
|
-
try {
|
|
59
|
-
await this.connectionManager.close();
|
|
60
|
-
} catch (err) {
|
|
61
|
-
console.error("[BullMQ] Error closing connection:", err);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
async getSubscriptionCount(topic) {
|
|
65
|
-
return this._workerManager.getSubscriptionCount(topic);
|
|
66
|
-
}
|
|
67
|
-
async listTopics() {
|
|
68
|
-
return this._workerManager.listTopics();
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
//#endregion
|
|
73
|
-
export { BullMQEventAdapter };
|
|
74
|
-
//# sourceMappingURL=bullmq-event-adapter.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"bullmq-event-adapter.mjs","names":[],"sources":["../src/bullmq-event-adapter.ts"],"sourcesContent":["import type { Event, EventAdapter, QueueConfig, SubscriptionHandle } from '@motiadev/core'\nimport type { Redis } from 'ioredis'\nimport { buildConfig, type MergedConfig } from './config-builder'\nimport { ConnectionManager } from './connection-manager'\nimport { DLQManager } from './dlq-manager'\nimport { QueueManager } from './queue-manager'\nimport type { BullMQEventAdapterConfig } from './types'\nimport { WorkerManager } from './worker-manager'\n\nexport class BullMQEventAdapter implements EventAdapter {\n private readonly connectionManager: ConnectionManager\n private readonly _queueManager: QueueManager\n private readonly _workerManager: WorkerManager\n private readonly _dlqManager: DLQManager\n private readonly _config: MergedConfig\n\n constructor(config: BullMQEventAdapterConfig) {\n this._config = buildConfig(config)\n this.connectionManager = new ConnectionManager(config.connection)\n this._queueManager = new QueueManager(this.connectionManager.connection, this._config)\n this._dlqManager = new DLQManager(this.connectionManager.connection, this._config)\n this._workerManager = new WorkerManager(\n this.connectionManager.connection,\n this._config,\n (topic, stepName) => this._queueManager.getQueueName(topic, stepName),\n this._dlqManager,\n )\n }\n\n get connection(): Redis {\n return this.connectionManager.connection\n }\n\n get prefix(): string {\n return this._config.prefix\n }\n\n get dlqSuffix(): string {\n return this._config.dlq.suffix\n }\n\n get queueManager(): QueueManager {\n return this._queueManager\n }\n\n get workerManager(): WorkerManager {\n return this._workerManager\n }\n\n get dlqManager(): DLQManager {\n return this._dlqManager\n }\n\n async emit<TData>(event: Event<TData>): Promise<void> {\n const subscribers = this._workerManager.getSubscribers(event.topic)\n if (subscribers.length === 0) {\n return\n }\n\n await this._queueManager.enqueueToAll(event, subscribers)\n }\n\n async subscribe<TData>(\n topic: string,\n stepName: string,\n handler: (event: Event<TData>) => void | Promise<void>,\n options?: QueueConfig,\n ): Promise<SubscriptionHandle> {\n const queueName = this._queueManager.getQueueName(topic, stepName)\n this._queueManager.getQueue(queueName)\n\n return this._workerManager.createWorker(topic, stepName, handler, options)\n }\n\n async unsubscribe(handle: SubscriptionHandle): Promise<void> {\n const workerInfo = this._workerManager.getWorkerInfo(handle.id)\n if (workerInfo) {\n const queueName = this._queueManager.getQueueName(workerInfo.topic, workerInfo.stepName)\n await this._queueManager.closeQueue(queueName)\n await this._workerManager.removeWorker(handle.id)\n }\n }\n\n async shutdown(): Promise<void> {\n await Promise.allSettled([\n this._workerManager.closeAll(),\n this._queueManager.closeAll(),\n this._dlqManager.closeAll(),\n ])\n\n try {\n await this.connectionManager.close()\n } catch (err) {\n console.error('[BullMQ] Error closing connection:', err)\n }\n }\n\n async getSubscriptionCount(topic: string): Promise<number> {\n return this._workerManager.getSubscriptionCount(topic)\n }\n\n async listTopics(): Promise<string[]> {\n return this._workerManager.listTopics()\n }\n}\n"],"mappings":";;;;;;;AASA,IAAa,qBAAb,MAAwD;CAOtD,YAAY,QAAkC;AAC5C,OAAK,UAAU,YAAY,OAAO;AAClC,OAAK,oBAAoB,IAAI,kBAAkB,OAAO,WAAW;AACjE,OAAK,gBAAgB,IAAI,aAAa,KAAK,kBAAkB,YAAY,KAAK,QAAQ;AACtF,OAAK,cAAc,IAAI,WAAW,KAAK,kBAAkB,YAAY,KAAK,QAAQ;AAClF,OAAK,iBAAiB,IAAI,cACxB,KAAK,kBAAkB,YACvB,KAAK,UACJ,OAAO,aAAa,KAAK,cAAc,aAAa,OAAO,SAAS,EACrE,KAAK,YACN;;CAGH,IAAI,aAAoB;AACtB,SAAO,KAAK,kBAAkB;;CAGhC,IAAI,SAAiB;AACnB,SAAO,KAAK,QAAQ;;CAGtB,IAAI,YAAoB;AACtB,SAAO,KAAK,QAAQ,IAAI;;CAG1B,IAAI,eAA6B;AAC/B,SAAO,KAAK;;CAGd,IAAI,gBAA+B;AACjC,SAAO,KAAK;;CAGd,IAAI,aAAyB;AAC3B,SAAO,KAAK;;CAGd,MAAM,KAAY,OAAoC;EACpD,MAAM,cAAc,KAAK,eAAe,eAAe,MAAM,MAAM;AACnE,MAAI,YAAY,WAAW,EACzB;AAGF,QAAM,KAAK,cAAc,aAAa,OAAO,YAAY;;CAG3D,MAAM,UACJ,OACA,UACA,SACA,SAC6B;EAC7B,MAAM,YAAY,KAAK,cAAc,aAAa,OAAO,SAAS;AAClE,OAAK,cAAc,SAAS,UAAU;AAEtC,SAAO,KAAK,eAAe,aAAa,OAAO,UAAU,SAAS,QAAQ;;CAG5E,MAAM,YAAY,QAA2C;EAC3D,MAAM,aAAa,KAAK,eAAe,cAAc,OAAO,GAAG;AAC/D,MAAI,YAAY;GACd,MAAM,YAAY,KAAK,cAAc,aAAa,WAAW,OAAO,WAAW,SAAS;AACxF,SAAM,KAAK,cAAc,WAAW,UAAU;AAC9C,SAAM,KAAK,eAAe,aAAa,OAAO,GAAG;;;CAIrD,MAAM,WAA0B;AAC9B,QAAM,QAAQ,WAAW;GACvB,KAAK,eAAe,UAAU;GAC9B,KAAK,cAAc,UAAU;GAC7B,KAAK,YAAY,UAAU;GAC5B,CAAC;AAEF,MAAI;AACF,SAAM,KAAK,kBAAkB,OAAO;WAC7B,KAAK;AACZ,WAAQ,MAAM,sCAAsC,IAAI;;;CAI5D,MAAM,qBAAqB,OAAgC;AACzD,SAAO,KAAK,eAAe,qBAAqB,MAAM;;CAGxD,MAAM,aAAgC;AACpC,SAAO,KAAK,eAAe,YAAY"}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { BullMQEventAdapterConfig } from "./types.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/config-builder.d.ts
|
|
4
|
-
type MergedConfig = Required<Pick<BullMQEventAdapterConfig, 'defaultJobOptions' | 'prefix' | 'concurrency'>> & {
|
|
5
|
-
dlq: Required<NonNullable<BullMQEventAdapterConfig['dlq']>>;
|
|
6
|
-
};
|
|
7
|
-
//#endregion
|
|
8
|
-
export { MergedConfig };
|
|
9
|
-
//# sourceMappingURL=config-builder.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"config-builder.d.mts","names":[],"sources":["../src/config-builder.ts"],"sourcesContent":[],"mappings":";;;KAYY,YAAA,GAAe,SAAS,KAAK;OAClC,SAAS,YAAY;AAD5B,CAAA"}
|
package/dist/config-builder.mjs
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { DEFAULT_ATTEMPTS, DEFAULT_BACKOFF_DELAY, DEFAULT_CONCURRENCY, DEFAULT_DLQ_SUFFIX, DEFAULT_DLQ_TTL, DEFAULT_PREFIX, DEFAULT_REMOVE_ON_COMPLETE_COUNT, DEFAULT_REMOVE_ON_FAIL_COUNT } from "./constants.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/config-builder.ts
|
|
4
|
-
function buildConfig(config) {
|
|
5
|
-
return {
|
|
6
|
-
concurrency: config.concurrency ?? DEFAULT_CONCURRENCY,
|
|
7
|
-
defaultJobOptions: {
|
|
8
|
-
attempts: DEFAULT_ATTEMPTS,
|
|
9
|
-
backoff: {
|
|
10
|
-
type: "fixed",
|
|
11
|
-
delay: DEFAULT_BACKOFF_DELAY
|
|
12
|
-
},
|
|
13
|
-
removeOnComplete: { count: DEFAULT_REMOVE_ON_COMPLETE_COUNT },
|
|
14
|
-
removeOnFail: { count: DEFAULT_REMOVE_ON_FAIL_COUNT },
|
|
15
|
-
...config.defaultJobOptions
|
|
16
|
-
},
|
|
17
|
-
prefix: config.prefix ?? DEFAULT_PREFIX,
|
|
18
|
-
dlq: {
|
|
19
|
-
enabled: config.dlq?.enabled ?? true,
|
|
20
|
-
ttl: config.dlq?.ttl ?? DEFAULT_DLQ_TTL,
|
|
21
|
-
suffix: config.dlq?.suffix ?? DEFAULT_DLQ_SUFFIX
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
//#endregion
|
|
27
|
-
export { buildConfig };
|
|
28
|
-
//# sourceMappingURL=config-builder.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"config-builder.mjs","names":[],"sources":["../src/config-builder.ts"],"sourcesContent":["import {\n DEFAULT_ATTEMPTS,\n DEFAULT_BACKOFF_DELAY,\n DEFAULT_CONCURRENCY,\n DEFAULT_DLQ_SUFFIX,\n DEFAULT_DLQ_TTL,\n DEFAULT_PREFIX,\n DEFAULT_REMOVE_ON_COMPLETE_COUNT,\n DEFAULT_REMOVE_ON_FAIL_COUNT,\n} from './constants'\nimport type { BullMQEventAdapterConfig } from './types'\n\nexport type MergedConfig = Required<Pick<BullMQEventAdapterConfig, 'defaultJobOptions' | 'prefix' | 'concurrency'>> & {\n dlq: Required<NonNullable<BullMQEventAdapterConfig['dlq']>>\n}\n\nexport function buildConfig(config: BullMQEventAdapterConfig): MergedConfig {\n return {\n concurrency: config.concurrency ?? DEFAULT_CONCURRENCY,\n defaultJobOptions: {\n attempts: DEFAULT_ATTEMPTS,\n backoff: {\n type: 'fixed',\n delay: DEFAULT_BACKOFF_DELAY,\n },\n removeOnComplete: {\n count: DEFAULT_REMOVE_ON_COMPLETE_COUNT,\n },\n removeOnFail: {\n count: DEFAULT_REMOVE_ON_FAIL_COUNT,\n },\n ...config.defaultJobOptions,\n },\n prefix: config.prefix ?? DEFAULT_PREFIX,\n dlq: {\n enabled: config.dlq?.enabled ?? true,\n ttl: config.dlq?.ttl ?? DEFAULT_DLQ_TTL,\n suffix: config.dlq?.suffix ?? DEFAULT_DLQ_SUFFIX,\n },\n }\n}\n"],"mappings":";;;AAgBA,SAAgB,YAAY,QAAgD;AAC1E,QAAO;EACL,aAAa,OAAO,eAAe;EACnC,mBAAmB;GACjB,UAAU;GACV,SAAS;IACP,MAAM;IACN,OAAO;IACR;GACD,kBAAkB,EAChB,OAAO,kCACR;GACD,cAAc,EACZ,OAAO,8BACR;GACD,GAAG,OAAO;GACX;EACD,QAAQ,OAAO,UAAU;EACzB,KAAK;GACH,SAAS,OAAO,KAAK,WAAW;GAChC,KAAK,OAAO,KAAK,OAAO;GACxB,QAAQ,OAAO,KAAK,UAAU;GAC/B;EACF"}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { ConnectionError } from "./errors.mjs";
|
|
2
|
-
import IORedis from "ioredis";
|
|
3
|
-
|
|
4
|
-
//#region src/connection-manager.ts
|
|
5
|
-
var ConnectionManager = class {
|
|
6
|
-
constructor(config) {
|
|
7
|
-
if (config instanceof IORedis) {
|
|
8
|
-
this.connection = config;
|
|
9
|
-
this.ownsConnection = false;
|
|
10
|
-
} else {
|
|
11
|
-
this.connection = new IORedis({
|
|
12
|
-
maxRetriesPerRequest: null,
|
|
13
|
-
...config
|
|
14
|
-
});
|
|
15
|
-
this.ownsConnection = true;
|
|
16
|
-
}
|
|
17
|
-
this.setupEventHandlers();
|
|
18
|
-
}
|
|
19
|
-
setupEventHandlers() {
|
|
20
|
-
this.connection.on("error", (err) => {
|
|
21
|
-
const error = new ConnectionError(err.message, err);
|
|
22
|
-
console.error("[BullMQ] Connection error:", error);
|
|
23
|
-
});
|
|
24
|
-
this.connection.on("close", () => {
|
|
25
|
-
console.warn("[BullMQ] Connection closed");
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
async close() {
|
|
29
|
-
if (this.ownsConnection && this.connection.status !== "end") await this.connection.quit();
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
//#endregion
|
|
34
|
-
export { ConnectionManager };
|
|
35
|
-
//# sourceMappingURL=connection-manager.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"connection-manager.mjs","names":[],"sources":["../src/connection-manager.ts"],"sourcesContent":["import IORedis, { type Redis } from 'ioredis'\nimport { ConnectionError } from './errors'\nimport type { BullMQConnectionConfig } from './types'\n\nexport class ConnectionManager {\n readonly connection: Redis\n readonly ownsConnection: boolean\n\n constructor(config: BullMQConnectionConfig) {\n if (config instanceof IORedis) {\n this.connection = config\n this.ownsConnection = false\n } else {\n this.connection = new IORedis({\n maxRetriesPerRequest: null,\n ...config,\n })\n this.ownsConnection = true\n }\n\n this.setupEventHandlers()\n }\n\n private setupEventHandlers(): void {\n this.connection.on('error', (err: Error) => {\n const error = new ConnectionError(err.message, err)\n console.error('[BullMQ] Connection error:', error)\n })\n\n this.connection.on('close', () => {\n console.warn('[BullMQ] Connection closed')\n })\n }\n\n async close(): Promise<void> {\n if (this.ownsConnection && this.connection.status !== 'end') {\n await this.connection.quit()\n }\n }\n}\n"],"mappings":";;;;AAIA,IAAa,oBAAb,MAA+B;CAI7B,YAAY,QAAgC;AAC1C,MAAI,kBAAkB,SAAS;AAC7B,QAAK,aAAa;AAClB,QAAK,iBAAiB;SACjB;AACL,QAAK,aAAa,IAAI,QAAQ;IAC5B,sBAAsB;IACtB,GAAG;IACJ,CAAC;AACF,QAAK,iBAAiB;;AAGxB,OAAK,oBAAoB;;CAG3B,AAAQ,qBAA2B;AACjC,OAAK,WAAW,GAAG,UAAU,QAAe;GAC1C,MAAM,QAAQ,IAAI,gBAAgB,IAAI,SAAS,IAAI;AACnD,WAAQ,MAAM,8BAA8B,MAAM;IAClD;AAEF,OAAK,WAAW,GAAG,eAAe;AAChC,WAAQ,KAAK,6BAA6B;IAC1C;;CAGJ,MAAM,QAAuB;AAC3B,MAAI,KAAK,kBAAkB,KAAK,WAAW,WAAW,MACpD,OAAM,KAAK,WAAW,MAAM"}
|
package/dist/constants.mjs
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
//#region src/constants.ts
|
|
2
|
-
const DEFAULT_CONCURRENCY = 5;
|
|
3
|
-
const DEFAULT_ATTEMPTS = 3;
|
|
4
|
-
const DEFAULT_BACKOFF_DELAY = 2e3;
|
|
5
|
-
const DEFAULT_REMOVE_ON_COMPLETE_COUNT = 1e3;
|
|
6
|
-
const DEFAULT_REMOVE_ON_FAIL_COUNT = 5e3;
|
|
7
|
-
const DEFAULT_PREFIX = "motia";
|
|
8
|
-
const FIFO_CONCURRENCY = 1;
|
|
9
|
-
const MILLISECONDS_PER_SECOND = 1e3;
|
|
10
|
-
const SECONDS_PER_DAY = 86400;
|
|
11
|
-
const DEFAULT_DLQ_TTL = 30 * SECONDS_PER_DAY;
|
|
12
|
-
const DEFAULT_DLQ_SUFFIX = ".dlq";
|
|
13
|
-
const DLQ_JOB_PREFIX = "dlq-";
|
|
14
|
-
const LOG_PREFIX = "[BullMQ]";
|
|
15
|
-
|
|
16
|
-
//#endregion
|
|
17
|
-
export { DEFAULT_ATTEMPTS, DEFAULT_BACKOFF_DELAY, DEFAULT_CONCURRENCY, DEFAULT_DLQ_SUFFIX, DEFAULT_DLQ_TTL, DEFAULT_PREFIX, DEFAULT_REMOVE_ON_COMPLETE_COUNT, DEFAULT_REMOVE_ON_FAIL_COUNT, DLQ_JOB_PREFIX, FIFO_CONCURRENCY, LOG_PREFIX, MILLISECONDS_PER_SECOND };
|
|
18
|
-
//# sourceMappingURL=constants.mjs.map
|
package/dist/constants.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"constants.mjs","names":[],"sources":["../src/constants.ts"],"sourcesContent":["export const DEFAULT_CONCURRENCY = 5\nexport const DEFAULT_ATTEMPTS = 3\nexport const DEFAULT_BACKOFF_DELAY = 2000\nexport const DEFAULT_REMOVE_ON_COMPLETE_COUNT = 1000\nexport const DEFAULT_REMOVE_ON_FAIL_COUNT = 5000\nexport const DEFAULT_PREFIX = 'motia'\nexport const FIFO_CONCURRENCY = 1\nexport const MILLISECONDS_PER_SECOND = 1000\nexport const SECONDS_PER_DAY = 86400\nexport const DEFAULT_DLQ_TTL = 30 * SECONDS_PER_DAY\nexport const DEFAULT_DLQ_SUFFIX = '.dlq'\nexport const DLQ_JOB_PREFIX = 'dlq-'\n\nexport const LOG_PREFIX = '[BullMQ]'\n"],"mappings":";AAAA,MAAa,sBAAsB;AACnC,MAAa,mBAAmB;AAChC,MAAa,wBAAwB;AACrC,MAAa,mCAAmC;AAChD,MAAa,+BAA+B;AAC5C,MAAa,iBAAiB;AAC9B,MAAa,mBAAmB;AAChC,MAAa,0BAA0B;AACvC,MAAa,kBAAkB;AAC/B,MAAa,kBAAkB,KAAK;AACpC,MAAa,qBAAqB;AAClC,MAAa,iBAAiB;AAE9B,MAAa,aAAa"}
|
package/dist/dlq-manager.d.mts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { MergedConfig } from "./config-builder.mjs";
|
|
2
|
-
import { Redis } from "ioredis";
|
|
3
|
-
import { Queue } from "bullmq";
|
|
4
|
-
import { Event } from "@motiadev/core";
|
|
5
|
-
|
|
6
|
-
//#region src/dlq-manager.d.ts
|
|
7
|
-
declare class DLQManager {
|
|
8
|
-
private readonly dlqQueues;
|
|
9
|
-
private readonly connection;
|
|
10
|
-
private readonly config;
|
|
11
|
-
constructor(connection: Redis, config: MergedConfig);
|
|
12
|
-
getDLQQueueName(topic: string, stepName: string): string;
|
|
13
|
-
private getOrCreateDLQQueue;
|
|
14
|
-
moveToDLQ<TData>(topic: string, stepName: string, event: Event<TData>, error: Error, attemptsMade: number, originalJobId?: string): Promise<void>;
|
|
15
|
-
closeDLQQueue(queueName: string): Promise<void>;
|
|
16
|
-
closeAll(): Promise<void>;
|
|
17
|
-
getDLQQueue(queueName: string): Queue | undefined;
|
|
18
|
-
getOrCreateDLQ(queueName: string): Queue;
|
|
19
|
-
listDLQQueueNames(): string[];
|
|
20
|
-
getDLQSuffix(): string;
|
|
21
|
-
getPrefix(): string;
|
|
22
|
-
getConnection(): Redis;
|
|
23
|
-
}
|
|
24
|
-
//#endregion
|
|
25
|
-
export { DLQManager };
|
|
26
|
-
//# sourceMappingURL=dlq-manager.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dlq-manager.d.mts","names":[],"sources":["../src/dlq-manager.ts"],"sourcesContent":[],"mappings":";;;;;;cAea,UAAA;;EAAA,iBAAU,UAAA;EAKG,iBAAA,MAAA;EAAe,WAAA,CAAA,UAAA,EAAf,KAAe,EAAA,MAAA,EAAA,YAAA;EA2CxB,eAAA,CAAA,KAAA,EAAA,MAAA,EAAA,QAAA,EAAA,MAAA,CAAA,EAAA,MAAA;EAAN,QAAA,mBAAA;EACA,SAAA,CAAA,KAAA,CAAA,CAAA,KAAA,EAAA,MAAA,EAAA,QAAA,EAAA,MAAA,EAAA,KAAA,EADA,KACA,CADM,KACN,CAAA,EAAA,KAAA,EAAA,KAAA,EAAA,YAAA,EAAA,MAAA,EAAA,aAAA,CAAA,EAAA,MAAA,CAAA,EAGN,OAHM,CAAA,IAAA,CAAA;EAGN,aAAA,CAAA,SAAA,EAAA,MAAA,CAAA,EA0CqC,OA1CrC,CAAA,IAAA,CAAA;EA0CqC,QAAA,CAAA,CAAA,EAQtB,OARsB,CAAA,IAAA,CAAA;EAQtB,WAAA,CAAA,SAAA,EAAA,MAAA,CAAA,EAUc,KAVd,GAAA,SAAA;EAUc,cAAA,CAAA,SAAA,EAAA,MAAA,CAAA,EAIG,KAJH;EAIG,iBAAA,CAAA,CAAA,EAAA,MAAA,EAAA;EAgBlB,YAAA,CAAA,CAAA,EAAA,MAAA;EAAK,SAAA,CAAA,CAAA,EAAA,MAAA;mBAAL"}
|
package/dist/dlq-manager.mjs
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { DLQ_JOB_PREFIX, LOG_PREFIX, MILLISECONDS_PER_SECOND } from "./constants.mjs";
|
|
2
|
-
import { QueueCreationError } from "./errors.mjs";
|
|
3
|
-
import { Queue } from "bullmq";
|
|
4
|
-
|
|
5
|
-
//#region src/dlq-manager.ts
|
|
6
|
-
var DLQManager = class {
|
|
7
|
-
constructor(connection, config) {
|
|
8
|
-
this.dlqQueues = /* @__PURE__ */ new Map();
|
|
9
|
-
this.connection = connection;
|
|
10
|
-
this.config = config;
|
|
11
|
-
}
|
|
12
|
-
getDLQQueueName(topic, stepName) {
|
|
13
|
-
return `${`${topic}.${stepName}`}${this.config.dlq.suffix}`;
|
|
14
|
-
}
|
|
15
|
-
getOrCreateDLQQueue(queueName) {
|
|
16
|
-
if (!this.dlqQueues.has(queueName)) {
|
|
17
|
-
const ttlMs = this.config.dlq.ttl * MILLISECONDS_PER_SECOND;
|
|
18
|
-
const queue$1 = new Queue(queueName, {
|
|
19
|
-
connection: this.connection,
|
|
20
|
-
prefix: this.config.prefix,
|
|
21
|
-
defaultJobOptions: {
|
|
22
|
-
removeOnComplete: { age: ttlMs },
|
|
23
|
-
removeOnFail: { age: ttlMs }
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
queue$1.on("error", (err) => {
|
|
27
|
-
console.error(`${LOG_PREFIX} DLQ error for ${queueName}:`, err);
|
|
28
|
-
});
|
|
29
|
-
this.dlqQueues.set(queueName, queue$1);
|
|
30
|
-
}
|
|
31
|
-
const queue = this.dlqQueues.get(queueName);
|
|
32
|
-
if (!queue) throw new QueueCreationError(queueName);
|
|
33
|
-
return queue;
|
|
34
|
-
}
|
|
35
|
-
async moveToDLQ(topic, stepName, event, error, attemptsMade, originalJobId) {
|
|
36
|
-
if (!this.config.dlq.enabled) {
|
|
37
|
-
console.warn(`${LOG_PREFIX} DLQ is disabled, skipping move to DLQ for topic ${topic}, step ${stepName}`);
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
try {
|
|
41
|
-
const dlqQueueName = this.getDLQQueueName(topic, stepName);
|
|
42
|
-
const dlqQueue = this.getOrCreateDLQQueue(dlqQueueName);
|
|
43
|
-
const dlqJobData = {
|
|
44
|
-
originalEvent: {
|
|
45
|
-
topic: event.topic,
|
|
46
|
-
data: event.data,
|
|
47
|
-
traceId: event.traceId || "unknown",
|
|
48
|
-
...event.flows && { flows: event.flows },
|
|
49
|
-
...event.messageGroupId && { messageGroupId: event.messageGroupId }
|
|
50
|
-
},
|
|
51
|
-
failureReason: error.message || "Unknown error",
|
|
52
|
-
failureTimestamp: Date.now(),
|
|
53
|
-
attemptsMade,
|
|
54
|
-
...originalJobId && { originalJobId }
|
|
55
|
-
};
|
|
56
|
-
const jobOptions = originalJobId ? { jobId: `${DLQ_JOB_PREFIX}${originalJobId}` } : {};
|
|
57
|
-
await dlqQueue.add(`${topic}.dlq`, dlqJobData, jobOptions);
|
|
58
|
-
console.warn(`${LOG_PREFIX} Moved failed job to DLQ: ${dlqQueueName}`, {
|
|
59
|
-
topic,
|
|
60
|
-
stepName,
|
|
61
|
-
attemptsMade,
|
|
62
|
-
error: error.message
|
|
63
|
-
});
|
|
64
|
-
} catch (err) {
|
|
65
|
-
const dlqError = err instanceof Error ? err : new Error(String(err));
|
|
66
|
-
console.error(`${LOG_PREFIX} Failed to move job to DLQ for topic ${topic}, step ${stepName}:`, dlqError);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
async closeDLQQueue(queueName) {
|
|
70
|
-
const queue = this.dlqQueues.get(queueName);
|
|
71
|
-
if (queue) {
|
|
72
|
-
await queue.close();
|
|
73
|
-
this.dlqQueues.delete(queueName);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
async closeAll() {
|
|
77
|
-
const promises = Array.from(this.dlqQueues.values()).map((queue) => queue.close().catch((err) => {
|
|
78
|
-
console.error(`${LOG_PREFIX} Error closing DLQ queue:`, err);
|
|
79
|
-
}));
|
|
80
|
-
await Promise.allSettled(promises);
|
|
81
|
-
this.dlqQueues.clear();
|
|
82
|
-
}
|
|
83
|
-
getDLQQueue(queueName) {
|
|
84
|
-
return this.dlqQueues.get(queueName);
|
|
85
|
-
}
|
|
86
|
-
getOrCreateDLQ(queueName) {
|
|
87
|
-
return this.getOrCreateDLQQueue(queueName);
|
|
88
|
-
}
|
|
89
|
-
listDLQQueueNames() {
|
|
90
|
-
return Array.from(this.dlqQueues.keys());
|
|
91
|
-
}
|
|
92
|
-
getDLQSuffix() {
|
|
93
|
-
return this.config.dlq.suffix;
|
|
94
|
-
}
|
|
95
|
-
getPrefix() {
|
|
96
|
-
return this.config.prefix;
|
|
97
|
-
}
|
|
98
|
-
getConnection() {
|
|
99
|
-
return this.connection;
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
//#endregion
|
|
104
|
-
export { DLQManager };
|
|
105
|
-
//# sourceMappingURL=dlq-manager.mjs.map
|
package/dist/dlq-manager.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dlq-manager.mjs","names":["queue","dlqJobData: DLQJobData<TData>"],"sources":["../src/dlq-manager.ts"],"sourcesContent":["import type { Event } from '@motiadev/core'\nimport { Queue } from 'bullmq'\nimport type { Redis } from 'ioredis'\nimport type { MergedConfig } from './config-builder'\nimport { DLQ_JOB_PREFIX, LOG_PREFIX, MILLISECONDS_PER_SECOND } from './constants'\nimport { QueueCreationError } from './errors'\n\ntype DLQJobData<TData> = {\n originalEvent: Event<TData>\n failureReason: string\n failureTimestamp: number\n attemptsMade: number\n originalJobId?: string\n}\n\nexport class DLQManager {\n private readonly dlqQueues: Map<string, Queue> = new Map()\n private readonly connection: Redis\n private readonly config: MergedConfig\n\n constructor(connection: Redis, config: MergedConfig) {\n this.connection = connection\n this.config = config\n }\n\n getDLQQueueName(topic: string, stepName: string): string {\n const baseQueueName = `${topic}.${stepName}`\n return `${baseQueueName}${this.config.dlq.suffix}`\n }\n\n private getOrCreateDLQQueue(queueName: string): Queue {\n if (!this.dlqQueues.has(queueName)) {\n const ttlMs = this.config.dlq.ttl * MILLISECONDS_PER_SECOND\n const queue = new Queue(queueName, {\n connection: this.connection,\n prefix: this.config.prefix,\n defaultJobOptions: {\n removeOnComplete: {\n age: ttlMs,\n },\n removeOnFail: {\n age: ttlMs,\n },\n },\n })\n\n queue.on('error', (err: Error) => {\n console.error(`${LOG_PREFIX} DLQ error for ${queueName}:`, err)\n })\n\n this.dlqQueues.set(queueName, queue)\n }\n\n const queue = this.dlqQueues.get(queueName)\n if (!queue) {\n throw new QueueCreationError(queueName)\n }\n return queue\n }\n\n async moveToDLQ<TData>(\n topic: string,\n stepName: string,\n event: Event<TData>,\n error: Error,\n attemptsMade: number,\n originalJobId?: string,\n ): Promise<void> {\n if (!this.config.dlq!.enabled) {\n console.warn(`${LOG_PREFIX} DLQ is disabled, skipping move to DLQ for topic ${topic}, step ${stepName}`)\n return\n }\n\n try {\n const dlqQueueName = this.getDLQQueueName(topic, stepName)\n const dlqQueue = this.getOrCreateDLQQueue(dlqQueueName)\n\n const sanitizedEvent = {\n topic: event.topic,\n data: event.data,\n traceId: event.traceId || 'unknown',\n ...(event.flows && { flows: event.flows }),\n ...(event.messageGroupId && { messageGroupId: event.messageGroupId }),\n } as Event<TData>\n\n const dlqJobData: DLQJobData<TData> = {\n originalEvent: sanitizedEvent,\n failureReason: error.message || 'Unknown error',\n failureTimestamp: Date.now(),\n attemptsMade,\n ...(originalJobId && { originalJobId }),\n }\n\n const jobOptions = originalJobId ? { jobId: `${DLQ_JOB_PREFIX}${originalJobId}` } : {}\n\n await dlqQueue.add(`${topic}.dlq`, dlqJobData, jobOptions)\n\n console.warn(`${LOG_PREFIX} Moved failed job to DLQ: ${dlqQueueName}`, {\n topic,\n stepName,\n attemptsMade,\n error: error.message,\n })\n } catch (err) {\n const dlqError = err instanceof Error ? err : new Error(String(err))\n console.error(`${LOG_PREFIX} Failed to move job to DLQ for topic ${topic}, step ${stepName}:`, dlqError)\n }\n }\n\n async closeDLQQueue(queueName: string): Promise<void> {\n const queue = this.dlqQueues.get(queueName)\n if (queue) {\n await queue.close()\n this.dlqQueues.delete(queueName)\n }\n }\n\n async closeAll(): Promise<void> {\n const promises = Array.from(this.dlqQueues.values()).map((queue) =>\n queue.close().catch((err) => {\n console.error(`${LOG_PREFIX} Error closing DLQ queue:`, err)\n }),\n )\n await Promise.allSettled(promises)\n this.dlqQueues.clear()\n }\n\n getDLQQueue(queueName: string): Queue | undefined {\n return this.dlqQueues.get(queueName)\n }\n\n getOrCreateDLQ(queueName: string): Queue {\n return this.getOrCreateDLQQueue(queueName)\n }\n\n listDLQQueueNames(): string[] {\n return Array.from(this.dlqQueues.keys())\n }\n\n getDLQSuffix(): string {\n return this.config.dlq.suffix\n }\n\n getPrefix(): string {\n return this.config.prefix\n }\n\n getConnection(): Redis {\n return this.connection\n }\n}\n"],"mappings":";;;;;AAeA,IAAa,aAAb,MAAwB;CAKtB,YAAY,YAAmB,QAAsB;mCAJJ,IAAI,KAAK;AAKxD,OAAK,aAAa;AAClB,OAAK,SAAS;;CAGhB,gBAAgB,OAAe,UAA0B;AAEvD,SAAO,GADe,GAAG,MAAM,GAAG,aACR,KAAK,OAAO,IAAI;;CAG5C,AAAQ,oBAAoB,WAA0B;AACpD,MAAI,CAAC,KAAK,UAAU,IAAI,UAAU,EAAE;GAClC,MAAM,QAAQ,KAAK,OAAO,IAAI,MAAM;GACpC,MAAMA,UAAQ,IAAI,MAAM,WAAW;IACjC,YAAY,KAAK;IACjB,QAAQ,KAAK,OAAO;IACpB,mBAAmB;KACjB,kBAAkB,EAChB,KAAK,OACN;KACD,cAAc,EACZ,KAAK,OACN;KACF;IACF,CAAC;AAEF,WAAM,GAAG,UAAU,QAAe;AAChC,YAAQ,MAAM,GAAG,WAAW,iBAAiB,UAAU,IAAI,IAAI;KAC/D;AAEF,QAAK,UAAU,IAAI,WAAWA,QAAM;;EAGtC,MAAM,QAAQ,KAAK,UAAU,IAAI,UAAU;AAC3C,MAAI,CAAC,MACH,OAAM,IAAI,mBAAmB,UAAU;AAEzC,SAAO;;CAGT,MAAM,UACJ,OACA,UACA,OACA,OACA,cACA,eACe;AACf,MAAI,CAAC,KAAK,OAAO,IAAK,SAAS;AAC7B,WAAQ,KAAK,GAAG,WAAW,mDAAmD,MAAM,SAAS,WAAW;AACxG;;AAGF,MAAI;GACF,MAAM,eAAe,KAAK,gBAAgB,OAAO,SAAS;GAC1D,MAAM,WAAW,KAAK,oBAAoB,aAAa;GAUvD,MAAMC,aAAgC;IACpC,eATqB;KACrB,OAAO,MAAM;KACb,MAAM,MAAM;KACZ,SAAS,MAAM,WAAW;KAC1B,GAAI,MAAM,SAAS,EAAE,OAAO,MAAM,OAAO;KACzC,GAAI,MAAM,kBAAkB,EAAE,gBAAgB,MAAM,gBAAgB;KACrE;IAIC,eAAe,MAAM,WAAW;IAChC,kBAAkB,KAAK,KAAK;IAC5B;IACA,GAAI,iBAAiB,EAAE,eAAe;IACvC;GAED,MAAM,aAAa,gBAAgB,EAAE,OAAO,GAAG,iBAAiB,iBAAiB,GAAG,EAAE;AAEtF,SAAM,SAAS,IAAI,GAAG,MAAM,OAAO,YAAY,WAAW;AAE1D,WAAQ,KAAK,GAAG,WAAW,4BAA4B,gBAAgB;IACrE;IACA;IACA;IACA,OAAO,MAAM;IACd,CAAC;WACK,KAAK;GACZ,MAAM,WAAW,eAAe,QAAQ,MAAM,IAAI,MAAM,OAAO,IAAI,CAAC;AACpE,WAAQ,MAAM,GAAG,WAAW,uCAAuC,MAAM,SAAS,SAAS,IAAI,SAAS;;;CAI5G,MAAM,cAAc,WAAkC;EACpD,MAAM,QAAQ,KAAK,UAAU,IAAI,UAAU;AAC3C,MAAI,OAAO;AACT,SAAM,MAAM,OAAO;AACnB,QAAK,UAAU,OAAO,UAAU;;;CAIpC,MAAM,WAA0B;EAC9B,MAAM,WAAW,MAAM,KAAK,KAAK,UAAU,QAAQ,CAAC,CAAC,KAAK,UACxD,MAAM,OAAO,CAAC,OAAO,QAAQ;AAC3B,WAAQ,MAAM,GAAG,WAAW,4BAA4B,IAAI;IAC5D,CACH;AACD,QAAM,QAAQ,WAAW,SAAS;AAClC,OAAK,UAAU,OAAO;;CAGxB,YAAY,WAAsC;AAChD,SAAO,KAAK,UAAU,IAAI,UAAU;;CAGtC,eAAe,WAA0B;AACvC,SAAO,KAAK,oBAAoB,UAAU;;CAG5C,oBAA8B;AAC5B,SAAO,MAAM,KAAK,KAAK,UAAU,MAAM,CAAC;;CAG1C,eAAuB;AACrB,SAAO,KAAK,OAAO,IAAI;;CAGzB,YAAoB;AAClB,SAAO,KAAK,OAAO;;CAGrB,gBAAuB;AACrB,SAAO,KAAK"}
|
package/dist/errors.mjs
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
//#region src/errors.ts
|
|
2
|
-
var BullMQAdapterError = class extends Error {
|
|
3
|
-
constructor(message, cause) {
|
|
4
|
-
super(message);
|
|
5
|
-
this.cause = cause;
|
|
6
|
-
this.name = "BullMQAdapterError";
|
|
7
|
-
if (cause) this.stack = `${this.stack}\nCaused by: ${cause.stack}`;
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
var QueueCreationError = class extends BullMQAdapterError {
|
|
11
|
-
constructor(queueName, cause) {
|
|
12
|
-
super(`Failed to create queue: ${queueName}`, cause);
|
|
13
|
-
this.name = "QueueCreationError";
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
var WorkerCreationError = class extends BullMQAdapterError {
|
|
17
|
-
constructor(topic, stepName, cause) {
|
|
18
|
-
super(`Failed to create worker for topic ${topic}, step ${stepName}`, cause);
|
|
19
|
-
this.name = "WorkerCreationError";
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
var ConnectionError = class extends BullMQAdapterError {
|
|
23
|
-
constructor(message, cause) {
|
|
24
|
-
super(`Connection error: ${message}`, cause);
|
|
25
|
-
this.name = "ConnectionError";
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
//#endregion
|
|
30
|
-
export { ConnectionError, QueueCreationError, WorkerCreationError };
|
|
31
|
-
//# sourceMappingURL=errors.mjs.map
|
package/dist/errors.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"errors.mjs","names":["cause?: Error"],"sources":["../src/errors.ts"],"sourcesContent":["export class BullMQAdapterError extends Error {\n constructor(\n message: string,\n public readonly cause?: Error,\n ) {\n super(message)\n this.name = 'BullMQAdapterError'\n if (cause) {\n this.stack = `${this.stack}\\nCaused by: ${cause.stack}`\n }\n }\n}\n\nexport class QueueCreationError extends BullMQAdapterError {\n constructor(queueName: string, cause?: Error) {\n super(`Failed to create queue: ${queueName}`, cause)\n this.name = 'QueueCreationError'\n }\n}\n\nexport class WorkerCreationError extends BullMQAdapterError {\n constructor(topic: string, stepName: string, cause?: Error) {\n super(`Failed to create worker for topic ${topic}, step ${stepName}`, cause)\n this.name = 'WorkerCreationError'\n }\n}\n\nexport class ConnectionError extends BullMQAdapterError {\n constructor(message: string, cause?: Error) {\n super(`Connection error: ${message}`, cause)\n this.name = 'ConnectionError'\n }\n}\n"],"mappings":";AAAA,IAAa,qBAAb,cAAwC,MAAM;CAC5C,YACE,SACA,AAAgBA,OAChB;AACA,QAAM,QAAQ;EAFE;AAGhB,OAAK,OAAO;AACZ,MAAI,MACF,MAAK,QAAQ,GAAG,KAAK,MAAM,eAAe,MAAM;;;AAKtD,IAAa,qBAAb,cAAwC,mBAAmB;CACzD,YAAY,WAAmB,OAAe;AAC5C,QAAM,2BAA2B,aAAa,MAAM;AACpD,OAAK,OAAO;;;AAIhB,IAAa,sBAAb,cAAyC,mBAAmB;CAC1D,YAAY,OAAe,UAAkB,OAAe;AAC1D,QAAM,qCAAqC,MAAM,SAAS,YAAY,MAAM;AAC5E,OAAK,OAAO;;;AAIhB,IAAa,kBAAb,cAAqC,mBAAmB;CACtD,YAAY,SAAiB,OAAe;AAC1C,QAAM,qBAAqB,WAAW,MAAM;AAC5C,OAAK,OAAO"}
|
package/dist/index.d.mts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { BullMQConnectionConfig, BullMQEventAdapterConfig } from "./types.mjs";
|
|
2
|
-
import { DLQManager } from "./dlq-manager.mjs";
|
|
3
|
-
import { SubscriberInfo, WorkerManager } from "./worker-manager.mjs";
|
|
4
|
-
import { QueueManager } from "./queue-manager.mjs";
|
|
5
|
-
import { BullMQEventAdapter } from "./bullmq-event-adapter.mjs";
|
|
6
|
-
export { type BullMQConnectionConfig, BullMQEventAdapter, type BullMQEventAdapterConfig, DLQManager, QueueManager, type SubscriberInfo, WorkerManager };
|
package/dist/index.mjs
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { DLQManager } from "./dlq-manager.mjs";
|
|
2
|
-
import { QueueManager } from "./queue-manager.mjs";
|
|
3
|
-
import { WorkerManager } from "./worker-manager.mjs";
|
|
4
|
-
import { BullMQEventAdapter } from "./bullmq-event-adapter.mjs";
|
|
5
|
-
|
|
6
|
-
export { BullMQEventAdapter, DLQManager, QueueManager, WorkerManager };
|