@motiadev/adapter-bullmq-events 0.14.0-beta.165-454838 → 0.14.0-beta.165-602289

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.
Files changed (63) hide show
  1. package/dist/bullmq-event-adapter.d.mts +31 -0
  2. package/dist/bullmq-event-adapter.d.mts.map +1 -0
  3. package/dist/bullmq-event-adapter.mjs +74 -0
  4. package/dist/bullmq-event-adapter.mjs.map +1 -0
  5. package/dist/config-builder.d.mts +9 -0
  6. package/dist/config-builder.d.mts.map +1 -0
  7. package/dist/config-builder.mjs +28 -0
  8. package/dist/config-builder.mjs.map +1 -0
  9. package/dist/connection-manager.mjs +35 -0
  10. package/dist/connection-manager.mjs.map +1 -0
  11. package/dist/constants.mjs +18 -0
  12. package/dist/constants.mjs.map +1 -0
  13. package/dist/dlq-manager.d.mts +26 -0
  14. package/dist/dlq-manager.d.mts.map +1 -0
  15. package/dist/dlq-manager.mjs +105 -0
  16. package/dist/dlq-manager.mjs.map +1 -0
  17. package/dist/errors.mjs +31 -0
  18. package/dist/errors.mjs.map +1 -0
  19. package/dist/index.d.mts +6 -0
  20. package/dist/index.mjs +6 -0
  21. package/dist/queue-manager.d.mts +24 -0
  22. package/dist/queue-manager.d.mts.map +1 -0
  23. package/dist/queue-manager.mjs +83 -0
  24. package/dist/queue-manager.mjs.map +1 -0
  25. package/dist/types.d.mts +27 -0
  26. package/dist/types.d.mts.map +1 -0
  27. package/dist/worker-manager.d.mts +41 -0
  28. package/dist/worker-manager.d.mts.map +1 -0
  29. package/dist/worker-manager.mjs +130 -0
  30. package/dist/worker-manager.mjs.map +1 -0
  31. package/package.json +11 -7
  32. package/tsconfig.json +2 -2
  33. package/tsdown.config.ts +17 -0
  34. package/dist/bullmq-event-adapter.d.ts +0 -27
  35. package/dist/bullmq-event-adapter.d.ts.map +0 -1
  36. package/dist/bullmq-event-adapter.js +0 -75
  37. package/dist/config-builder.d.ts +0 -6
  38. package/dist/config-builder.d.ts.map +0 -1
  39. package/dist/config-builder.js +0 -29
  40. package/dist/connection-manager.d.ts +0 -10
  41. package/dist/connection-manager.d.ts.map +0 -1
  42. package/dist/connection-manager.js +0 -39
  43. package/dist/constants.d.ts +0 -14
  44. package/dist/constants.d.ts.map +0 -1
  45. package/dist/constants.js +0 -16
  46. package/dist/dlq-manager.d.ts +0 -22
  47. package/dist/dlq-manager.d.ts.map +0 -1
  48. package/dist/dlq-manager.js +0 -112
  49. package/dist/errors.d.ts +0 -14
  50. package/dist/errors.d.ts.map +0 -1
  51. package/dist/errors.js +0 -35
  52. package/dist/index.d.ts +0 -7
  53. package/dist/index.d.ts.map +0 -1
  54. package/dist/index.js +0 -11
  55. package/dist/queue-manager.d.ts +0 -20
  56. package/dist/queue-manager.d.ts.map +0 -1
  57. package/dist/queue-manager.js +0 -85
  58. package/dist/types.d.ts +0 -23
  59. package/dist/types.d.ts.map +0 -1
  60. package/dist/types.js +0 -2
  61. package/dist/worker-manager.d.ts +0 -38
  62. package/dist/worker-manager.d.ts.map +0 -1
  63. package/dist/worker-manager.js +0 -136
@@ -0,0 +1,31 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,74 @@
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
@@ -0,0 +1 @@
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,OAA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;;CAI5D,MAAM,qBAAqB,OAAgC;AACzD,SAAO,KAAK,eAAe,qBAAqB,MAAM;;CAGxD,MAAM,aAAgC;AACpC,SAAO,KAAK,eAAe,YAAY"}
@@ -0,0 +1,9 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,28 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,35 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,18 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,26 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,105 @@
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
@@ -0,0 +1 @@
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,Q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ACvC;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"}
@@ -0,0 +1,31 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,6 @@
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 ADDED
@@ -0,0 +1,6 @@
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 };
@@ -0,0 +1,24 @@
1
+ import { MergedConfig } from "./config-builder.mjs";
2
+ import { SubscriberInfo } from "./worker-manager.mjs";
3
+ import { Redis } from "ioredis";
4
+ import { Queue } from "bullmq";
5
+ import { Event } from "@motiadev/core";
6
+
7
+ //#region src/queue-manager.d.ts
8
+ declare class QueueManager {
9
+ private readonly queues;
10
+ private readonly connection;
11
+ private readonly config;
12
+ constructor(connection: Redis, config: MergedConfig);
13
+ getQueue(queueName: string): Queue;
14
+ getQueueName(topic: string, stepName: string): string;
15
+ enqueueToAll<TData>(event: Event<TData>, subscribers: SubscriberInfo[]): Promise<void>;
16
+ closeQueue(queueName: string): Promise<void>;
17
+ closeAll(): Promise<void>;
18
+ listQueueNames(): string[];
19
+ getPrefix(): string;
20
+ getConnection(): Redis;
21
+ }
22
+ //#endregion
23
+ export { QueueManager };
24
+ //# sourceMappingURL=queue-manager.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queue-manager.d.mts","names":[],"sources":["../src/queue-manager.ts"],"sourcesContent":[],"mappings":";;;;;;;cAQa,YAAA;;EAAA,iBAAY,UAAA;EAKC,iBAAA,MAAA;EAAe,WAAA,CAAA,UAAA,EAAf,KAAe,EAAA,MAAA,EAAA,YAAA;EAKV,QAAA,CAAA,SAAA,EAAA,MAAA,CAAA,EAAA,KAAA;EA0BU,YAAA,CAAA,KAAA,EAAA,MAAA,EAAA,QAAA,EAAA,MAAA,CAAA,EAAA,MAAA;EAAN,YAAA,CAAA,KAAA,CAAA,CAAA,KAAA,EAAA,KAAA,CAAM,KAAN,CAAA,EAAA,WAAA,EAA2B,cAA3B,EAAA,CAAA,EAA8C,OAA9C,CAAA,IAAA,CAAA;EAA2B,UAAA,CAAA,SAAA,EAAA,MAAA,CAAA,EAiCvB,OAjCuB,CAAA,IAAA,CAAA;EAAmB,QAAA,CAAA,CAAA,EAyC7D,OAzC6D,CAAA,IAAA,CAAA;EAiC1C,cAAA,CAAA,CAAA,EAAA,MAAA,EAAA;EAQnB,SAAA,CAAA,CAAA,EAAA,MAAA;EAkBD,aAAA,CAAA,CAAA,EAAA,KAAA"}
@@ -0,0 +1,83 @@
1
+ import { MILLISECONDS_PER_SECOND } from "./constants.mjs";
2
+ import { QueueCreationError } from "./errors.mjs";
3
+ import { Queue } from "bullmq";
4
+
5
+ //#region src/queue-manager.ts
6
+ var QueueManager = class {
7
+ constructor(connection, config) {
8
+ this.queues = /* @__PURE__ */ new Map();
9
+ this.connection = connection;
10
+ this.config = config;
11
+ }
12
+ getQueue(queueName) {
13
+ if (!this.queues.has(queueName)) {
14
+ const queue$1 = new Queue(queueName, {
15
+ connection: this.connection,
16
+ prefix: this.config.prefix,
17
+ defaultJobOptions: this.config.defaultJobOptions
18
+ });
19
+ queue$1.on("error", (err) => {
20
+ console.error(`[BullMQ] Queue error for ${queueName}:`, err);
21
+ });
22
+ this.queues.set(queueName, queue$1);
23
+ }
24
+ const queue = this.queues.get(queueName);
25
+ if (!queue) throw new QueueCreationError(queueName);
26
+ return queue;
27
+ }
28
+ getQueueName(topic, stepName) {
29
+ return `${topic}.${stepName}`;
30
+ }
31
+ async enqueueToAll(event, subscribers) {
32
+ const promises = subscribers.map((subscriber) => {
33
+ const queueName = this.getQueueName(subscriber.topic, subscriber.stepName);
34
+ const queue = this.getQueue(queueName);
35
+ const jobId = event.messageGroupId ? `${queueName}.${event.messageGroupId}` : void 0;
36
+ const jobData = {
37
+ topic: event.topic,
38
+ data: event.data,
39
+ traceId: event.traceId,
40
+ flows: event.flows,
41
+ messageGroupId: event.messageGroupId
42
+ };
43
+ const maxRetries = subscriber.queueConfig?.maxRetries;
44
+ const attempts = maxRetries != null ? maxRetries + 1 : this.config.defaultJobOptions.attempts;
45
+ const delay = subscriber.queueConfig?.delaySeconds ? subscriber.queueConfig.delaySeconds * MILLISECONDS_PER_SECOND : void 0;
46
+ const jobOptions = {
47
+ jobId,
48
+ attempts,
49
+ backoff: this.config.defaultJobOptions.backoff,
50
+ delay
51
+ };
52
+ return queue.add(event.topic, jobData, jobOptions).then(() => void 0);
53
+ });
54
+ await Promise.all(promises);
55
+ }
56
+ async closeQueue(queueName) {
57
+ const queue = this.queues.get(queueName);
58
+ if (queue) {
59
+ await queue.close();
60
+ this.queues.delete(queueName);
61
+ }
62
+ }
63
+ async closeAll() {
64
+ const promises = Array.from(this.queues.values()).map((queue) => queue.close().catch((err) => {
65
+ console.error(`[BullMQ] Error closing queue:`, err);
66
+ }));
67
+ await Promise.allSettled(promises);
68
+ this.queues.clear();
69
+ }
70
+ listQueueNames() {
71
+ return Array.from(this.queues.keys());
72
+ }
73
+ getPrefix() {
74
+ return this.config.prefix;
75
+ }
76
+ getConnection() {
77
+ return this.connection;
78
+ }
79
+ };
80
+
81
+ //#endregion
82
+ export { QueueManager };
83
+ //# sourceMappingURL=queue-manager.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queue-manager.mjs","names":["queue"],"sources":["../src/queue-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 { MILLISECONDS_PER_SECOND } from './constants'\nimport { QueueCreationError } from './errors'\nimport type { SubscriberInfo } from './worker-manager'\n\nexport class QueueManager {\n private readonly queues: 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 getQueue(queueName: string): Queue {\n if (!this.queues.has(queueName)) {\n const queue = new Queue(queueName, {\n connection: this.connection,\n prefix: this.config.prefix,\n defaultJobOptions: this.config.defaultJobOptions,\n })\n\n queue.on('error', (err: Error) => {\n console.error(`[BullMQ] Queue error for ${queueName}:`, err)\n })\n\n this.queues.set(queueName, queue)\n }\n\n const queue = this.queues.get(queueName)\n if (!queue) {\n throw new QueueCreationError(queueName)\n }\n return queue\n }\n\n getQueueName(topic: string, stepName: string): string {\n return `${topic}.${stepName}`\n }\n\n async enqueueToAll<TData>(event: Event<TData>, subscribers: SubscriberInfo[]): Promise<void> {\n const promises = subscribers.map((subscriber) => {\n const queueName = this.getQueueName(subscriber.topic, subscriber.stepName)\n const queue = this.getQueue(queueName)\n const jobId = event.messageGroupId ? `${queueName}.${event.messageGroupId}` : undefined\n\n const jobData = {\n topic: event.topic,\n data: event.data,\n traceId: event.traceId,\n flows: event.flows,\n messageGroupId: event.messageGroupId,\n }\n\n const maxRetries = subscriber.queueConfig?.maxRetries\n const attempts = maxRetries != null ? maxRetries + 1 : this.config.defaultJobOptions.attempts\n const delay = subscriber.queueConfig?.delaySeconds\n ? subscriber.queueConfig.delaySeconds * MILLISECONDS_PER_SECOND\n : undefined\n\n const jobOptions = {\n jobId,\n attempts,\n backoff: this.config.defaultJobOptions.backoff,\n delay,\n }\n\n return queue.add(event.topic, jobData, jobOptions).then(() => undefined)\n })\n\n await Promise.all(promises)\n }\n\n async closeQueue(queueName: string): Promise<void> {\n const queue = this.queues.get(queueName)\n if (queue) {\n await queue.close()\n this.queues.delete(queueName)\n }\n }\n\n async closeAll(): Promise<void> {\n const promises = Array.from(this.queues.values()).map((queue) =>\n queue.close().catch((err) => {\n console.error(`[BullMQ] Error closing queue:`, err)\n }),\n )\n await Promise.allSettled(promises)\n this.queues.clear()\n }\n\n listQueueNames(): string[] {\n return Array.from(this.queues.keys())\n }\n\n getPrefix(): string {\n return this.config.prefix\n }\n\n getConnection(): Redis {\n return this.connection\n }\n}\n"],"mappings":";;;;;AAQA,IAAa,eAAb,MAA0B;CAKxB,YAAY,YAAmB,QAAsB;gCAJP,IAAI,KAAK;AAKrD,OAAK,aAAa;AAClB,OAAK,SAAS;;CAGhB,SAAS,WAA0B;AACjC,MAAI,CAAC,KAAK,OAAO,IAAI,UAAU,EAAE;GAC/B,MAAMA,UAAQ,IAAI,MAAM,WAAW;IACjC,YAAY,KAAK;IACjB,QAAQ,KAAK,OAAO;IACpB,mBAAmB,KAAK,OAAO;IAChC,CAAC;AAEF,WAAM,GAAG,UAAU,QAAe;AAChC,YAAQ,MAAM,4BAA4B,UAAU,IAAI,IAAI;KAC5D;AAEF,QAAK,OAAO,IAAI,WAAWA,QAAM;;EAGnC,MAAM,QAAQ,KAAK,OAAO,IAAI,UAAU;AACxC,MAAI,CAAC,MACH,OAAM,IAAI,mBAAmB,UAAU;AAEzC,SAAO;;CAGT,aAAa,OAAe,UAA0B;AACpD,SAAO,GAAG,MAAM,GAAG;;CAGrB,MAAM,aAAoB,OAAqB,aAA8C;EAC3F,MAAM,WAAW,YAAY,KAAK,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nectionConfig = Redis | RedisOptions;
6
+ interface BullMQEventAdapterConfig {
7
+ connection: BullMQConnectionConfig;
8
+ concurrency?: number;
9
+ defaultJobOptions?: {
10
+ attempts?: number;
11
+ backoff?: {
12
+ type: 'fixed' | 'exponential';
13
+ delay: number;
14
+ };
15
+ removeOnComplete?: KeepJobs;
16
+ removeOnFail?: KeepJobs;
17
+ };
18
+ prefix?: string;
19
+ dlq?: {
20
+ enabled?: boolean;
21
+ ttl?: number;
22
+ suffix?: string;
23
+ };
24
+ }
25
+ //#endregion
26
+ export { BullMQConnectionConfig, BullMQEventAdapterConfig };
27
+ //# sourceMappingURL=types.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.mts","names":[],"sources":["../src/types.ts"],"sourcesContent":[],"mappings":";;;;KAGY,sBAAA,GAAyB,QAAQ;UAE5B,wBAAA;EAFL,UAAA,EAGE,sBAHoB;EAEjB,WAAA,CAAA,EAAA,MAAA;EACH,iBAAA,CAAA,EAAA;IAQS,QAAA,CAAA,EAAA,MAAA;IACJ,OAAA,CAAA,EAAA;MAAQ,IAAA,EAAA,OAAA,GAAA,aAAA;;;uBADJ;mBACJ"}
@@ -0,0 +1,41 @@
1
+ import { MergedConfig } from "./config-builder.mjs";
2
+ import { DLQManager } from "./dlq-manager.mjs";
3
+ import { Redis } from "ioredis";
4
+ import { Worker } from "bullmq";
5
+ import { Event, QueueConfig, SubscriptionHandle } from "@motiadev/core";
6
+
7
+ //#region src/worker-manager.d.ts
8
+ type SubscriberInfo = {
9
+ topic: string;
10
+ stepName: string;
11
+ queueConfig?: QueueConfig;
12
+ };
13
+ type WorkerInfo = {
14
+ worker: Worker;
15
+ topic: string;
16
+ stepName: string;
17
+ handle: SubscriptionHandle;
18
+ queueConfig?: QueueConfig;
19
+ };
20
+ declare class WorkerManager {
21
+ private readonly workers;
22
+ private readonly topicSubscriptions;
23
+ private readonly connection;
24
+ private readonly config;
25
+ private readonly getQueueName;
26
+ private readonly dlqManager;
27
+ constructor(connection: Redis, config: MergedConfig, getQueueName: (topic: string, stepName: string) => string, dlqManager?: DLQManager);
28
+ createWorker<TData>(topic: string, stepName: string, handler: (event: Event<TData>) => void | Promise<void>, options?: QueueConfig): SubscriptionHandle;
29
+ getSubscribers(topic: string): SubscriberInfo[];
30
+ getWorkerInfo(id: string): WorkerInfo | undefined;
31
+ removeWorker(id: string): Promise<void>;
32
+ closeAll(): Promise<void>;
33
+ getSubscriptionCount(topic: string): number;
34
+ listTopics(): string[];
35
+ private addTopicSubscription;
36
+ private removeTopicSubscription;
37
+ private setupWorkerHandlers;
38
+ }
39
+ //#endregion
40
+ export { SubscriberInfo, WorkerManager };
41
+ //# sourceMappingURL=worker-manager.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker-manager.d.mts","names":[],"sources":["../src/worker-manager.ts"],"sourcesContent":[],"mappings":";;;;;;;KASY,cAAA;;EAAA,QAAA,EAAA,MAAA;EAMP,WAAA,CAAA,EAHW,WAGD;CACL;KADL,UAAA,GAIK;EACM,MAAA,EAJN,MAIM;EAAW,KAAA,EAAA,MAAA;EAWd,QAAA,EAAA,MAAa;EASV,MAAA,EArBN,kBAqBM;EACJ,WAAA,CAAA,EArBI,WAqBJ;CAEK;AAWU,cAvBd,aAAA,CAuBc;EAAN,iBAAA,OAAA;EAAwB,iBAAA,kBAAA;EAC/B,iBAAA,UAAA;EACT,iBAAA,MAAA;EAuD4B,iBAAA,YAAA;EAYJ,iBAAA,UAAA;EAIK,WAAA,CAAA,UAAA,EAvFlB,KAuFkB,EAAA,MAAA,EAtFtB,YAsFsB,EAAA,YAAA,EAAA,CAAA,KAAA,EAAA,MAAA,EAAA,QAAA,EAAA,MAAA,EAAA,GAAA,MAAA,EAAA,UAAA,CAAA,EApFjB,UAoFiB;EAWd,YAAA,CAAA,KAAA,CAAA,CAAA,KAAA,EAAA,MAAA,EAAA,QAAA,EAAA,MAAA,EAAA,OAAA,EAAA,CAAA,KAAA,EApFC,KAoFD,CApFO,KAoFP,CAAA,EAAA,GAAA,IAAA,GApFyB,OAoFzB,CAAA,IAAA,CAAA,EAAA,OAAA,CAAA,EAnFN,WAmFM,CAAA,EAlFf,kBAkFe;EAAO,cAAA,CAAA,KAAA,EAAA,MAAA,CAAA,EA3BM,cA2BN,EAAA;6BAfE;4BAIK;cAWd"}