@motiadev/adapter-bullmq-events 0.15.0-beta.165 → 0.15.1-beta.166

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 (66) hide show
  1. package/LICENSE +1 -1
  2. package/dist/bullmq-event-adapter.d.mts +31 -0
  3. package/dist/bullmq-event-adapter.d.mts.map +1 -0
  4. package/dist/bullmq-event-adapter.mjs +74 -0
  5. package/dist/bullmq-event-adapter.mjs.map +1 -0
  6. package/dist/config-builder.d.mts +9 -0
  7. package/dist/config-builder.d.mts.map +1 -0
  8. package/dist/config-builder.mjs +28 -0
  9. package/dist/config-builder.mjs.map +1 -0
  10. package/dist/connection-manager.mjs +35 -0
  11. package/dist/connection-manager.mjs.map +1 -0
  12. package/dist/constants.mjs +18 -0
  13. package/dist/constants.mjs.map +1 -0
  14. package/dist/dlq-manager.d.mts +26 -0
  15. package/dist/dlq-manager.d.mts.map +1 -0
  16. package/dist/dlq-manager.mjs +105 -0
  17. package/dist/dlq-manager.mjs.map +1 -0
  18. package/dist/errors.mjs +31 -0
  19. package/dist/errors.mjs.map +1 -0
  20. package/dist/index.d.mts +6 -0
  21. package/dist/index.mjs +6 -0
  22. package/dist/queue-manager.d.mts +24 -0
  23. package/dist/queue-manager.d.mts.map +1 -0
  24. package/dist/queue-manager.mjs +83 -0
  25. package/dist/queue-manager.mjs.map +1 -0
  26. package/dist/types.d.mts +27 -0
  27. package/dist/types.d.mts.map +1 -0
  28. package/dist/worker-manager.d.mts +41 -0
  29. package/dist/worker-manager.d.mts.map +1 -0
  30. package/dist/worker-manager.mjs +130 -0
  31. package/dist/worker-manager.mjs.map +1 -0
  32. package/package.json +11 -7
  33. package/src/constants.ts +1 -1
  34. package/src/worker-manager.ts +1 -1
  35. package/tsconfig.json +2 -2
  36. package/tsdown.config.ts +17 -0
  37. package/dist/bullmq-event-adapter.d.ts +0 -27
  38. package/dist/bullmq-event-adapter.d.ts.map +0 -1
  39. package/dist/bullmq-event-adapter.js +0 -75
  40. package/dist/config-builder.d.ts +0 -6
  41. package/dist/config-builder.d.ts.map +0 -1
  42. package/dist/config-builder.js +0 -29
  43. package/dist/connection-manager.d.ts +0 -10
  44. package/dist/connection-manager.d.ts.map +0 -1
  45. package/dist/connection-manager.js +0 -39
  46. package/dist/constants.d.ts +0 -14
  47. package/dist/constants.d.ts.map +0 -1
  48. package/dist/constants.js +0 -16
  49. package/dist/dlq-manager.d.ts +0 -22
  50. package/dist/dlq-manager.d.ts.map +0 -1
  51. package/dist/dlq-manager.js +0 -112
  52. package/dist/errors.d.ts +0 -14
  53. package/dist/errors.d.ts.map +0 -1
  54. package/dist/errors.js +0 -35
  55. package/dist/index.d.ts +0 -7
  56. package/dist/index.d.ts.map +0 -1
  57. package/dist/index.js +0 -11
  58. package/dist/queue-manager.d.ts +0 -20
  59. package/dist/queue-manager.d.ts.map +0 -1
  60. package/dist/queue-manager.js +0 -85
  61. package/dist/types.d.ts +0 -23
  62. package/dist/types.d.ts.map +0 -1
  63. package/dist/types.js +0 -2
  64. package/dist/worker-manager.d.ts +0 -38
  65. package/dist/worker-manager.d.ts.map +0 -1
  66. package/dist/worker-manager.js +0 -136
@@ -1,112 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DLQManager = void 0;
4
- const bullmq_1 = require("bullmq");
5
- const constants_1 = require("./constants");
6
- const errors_1 = require("./errors");
7
- class DLQManager {
8
- constructor(connection, config) {
9
- this.dlqQueues = new Map();
10
- this.connection = connection;
11
- this.config = config;
12
- }
13
- getDLQQueueName(topic, stepName) {
14
- const baseQueueName = `${topic}.${stepName}`;
15
- return `${baseQueueName}${this.config.dlq.suffix}`;
16
- }
17
- getOrCreateDLQQueue(queueName) {
18
- if (!this.dlqQueues.has(queueName)) {
19
- const ttlMs = this.config.dlq.ttl * constants_1.MILLISECONDS_PER_SECOND;
20
- const queue = new bullmq_1.Queue(queueName, {
21
- connection: this.connection,
22
- prefix: this.config.prefix,
23
- defaultJobOptions: {
24
- removeOnComplete: {
25
- age: ttlMs,
26
- },
27
- removeOnFail: {
28
- age: ttlMs,
29
- },
30
- },
31
- });
32
- queue.on('error', (err) => {
33
- console.error(`${constants_1.LOG_PREFIX} DLQ error for ${queueName}:`, err);
34
- });
35
- this.dlqQueues.set(queueName, queue);
36
- }
37
- const queue = this.dlqQueues.get(queueName);
38
- if (!queue) {
39
- throw new errors_1.QueueCreationError(queueName);
40
- }
41
- return queue;
42
- }
43
- async moveToDLQ(topic, stepName, event, error, attemptsMade, originalJobId) {
44
- if (!this.config.dlq.enabled) {
45
- console.warn(`${constants_1.LOG_PREFIX} DLQ is disabled, skipping move to DLQ for topic ${topic}, step ${stepName}`);
46
- return;
47
- }
48
- try {
49
- const dlqQueueName = this.getDLQQueueName(topic, stepName);
50
- const dlqQueue = this.getOrCreateDLQQueue(dlqQueueName);
51
- const sanitizedEvent = {
52
- topic: event.topic,
53
- data: event.data,
54
- traceId: event.traceId || 'unknown',
55
- ...(event.flows && { flows: event.flows }),
56
- ...(event.messageGroupId && { messageGroupId: event.messageGroupId }),
57
- };
58
- const dlqJobData = {
59
- originalEvent: sanitizedEvent,
60
- failureReason: error.message || 'Unknown error',
61
- failureTimestamp: Date.now(),
62
- attemptsMade,
63
- ...(originalJobId && { originalJobId }),
64
- };
65
- const jobOptions = originalJobId ? { jobId: `${constants_1.DLQ_JOB_PREFIX}${originalJobId}` } : {};
66
- await dlqQueue.add(`${topic}.dlq`, dlqJobData, jobOptions);
67
- console.warn(`${constants_1.LOG_PREFIX} Moved failed job to DLQ: ${dlqQueueName}`, {
68
- topic,
69
- stepName,
70
- attemptsMade,
71
- error: error.message,
72
- });
73
- }
74
- catch (err) {
75
- const dlqError = err instanceof Error ? err : new Error(String(err));
76
- console.error(`${constants_1.LOG_PREFIX} Failed to move job to DLQ for topic ${topic}, step ${stepName}:`, dlqError);
77
- }
78
- }
79
- async closeDLQQueue(queueName) {
80
- const queue = this.dlqQueues.get(queueName);
81
- if (queue) {
82
- await queue.close();
83
- this.dlqQueues.delete(queueName);
84
- }
85
- }
86
- async closeAll() {
87
- const promises = Array.from(this.dlqQueues.values()).map((queue) => queue.close().catch((err) => {
88
- console.error(`${constants_1.LOG_PREFIX} Error closing DLQ queue:`, err);
89
- }));
90
- await Promise.allSettled(promises);
91
- this.dlqQueues.clear();
92
- }
93
- getDLQQueue(queueName) {
94
- return this.dlqQueues.get(queueName);
95
- }
96
- getOrCreateDLQ(queueName) {
97
- return this.getOrCreateDLQQueue(queueName);
98
- }
99
- listDLQQueueNames() {
100
- return Array.from(this.dlqQueues.keys());
101
- }
102
- getDLQSuffix() {
103
- return this.config.dlq.suffix;
104
- }
105
- getPrefix() {
106
- return this.config.prefix;
107
- }
108
- getConnection() {
109
- return this.connection;
110
- }
111
- }
112
- exports.DLQManager = DLQManager;
package/dist/errors.d.ts DELETED
@@ -1,14 +0,0 @@
1
- export declare class BullMQAdapterError extends Error {
2
- readonly cause?: Error | undefined;
3
- constructor(message: string, cause?: Error | undefined);
4
- }
5
- export declare class QueueCreationError extends BullMQAdapterError {
6
- constructor(queueName: string, cause?: Error);
7
- }
8
- export declare class WorkerCreationError extends BullMQAdapterError {
9
- constructor(topic: string, stepName: string, cause?: Error);
10
- }
11
- export declare class ConnectionError extends BullMQAdapterError {
12
- constructor(message: string, cause?: Error);
13
- }
14
- //# sourceMappingURL=errors.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,kBAAmB,SAAQ,KAAK;aAGzB,KAAK,CAAC,EAAE,KAAK;gBAD7B,OAAO,EAAE,MAAM,EACC,KAAK,CAAC,EAAE,KAAK,YAAA;CAQhC;AAED,qBAAa,kBAAmB,SAAQ,kBAAkB;gBAC5C,SAAS,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,KAAK;CAI7C;AAED,qBAAa,mBAAoB,SAAQ,kBAAkB;gBAC7C,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,KAAK;CAI3D;AAED,qBAAa,eAAgB,SAAQ,kBAAkB;gBACzC,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,KAAK;CAI3C"}
package/dist/errors.js DELETED
@@ -1,35 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ConnectionError = exports.WorkerCreationError = exports.QueueCreationError = exports.BullMQAdapterError = void 0;
4
- class BullMQAdapterError extends Error {
5
- constructor(message, cause) {
6
- super(message);
7
- this.cause = cause;
8
- this.name = 'BullMQAdapterError';
9
- if (cause) {
10
- this.stack = `${this.stack}\nCaused by: ${cause.stack}`;
11
- }
12
- }
13
- }
14
- exports.BullMQAdapterError = BullMQAdapterError;
15
- class QueueCreationError extends BullMQAdapterError {
16
- constructor(queueName, cause) {
17
- super(`Failed to create queue: ${queueName}`, cause);
18
- this.name = 'QueueCreationError';
19
- }
20
- }
21
- exports.QueueCreationError = QueueCreationError;
22
- class WorkerCreationError extends BullMQAdapterError {
23
- constructor(topic, stepName, cause) {
24
- super(`Failed to create worker for topic ${topic}, step ${stepName}`, cause);
25
- this.name = 'WorkerCreationError';
26
- }
27
- }
28
- exports.WorkerCreationError = WorkerCreationError;
29
- class ConnectionError extends BullMQAdapterError {
30
- constructor(message, cause) {
31
- super(`Connection error: ${message}`, cause);
32
- this.name = 'ConnectionError';
33
- }
34
- }
35
- exports.ConnectionError = ConnectionError;
package/dist/index.d.ts DELETED
@@ -1,7 +0,0 @@
1
- export { BullMQEventAdapter } from './bullmq-event-adapter';
2
- export { DLQManager } from './dlq-manager';
3
- export { QueueManager } from './queue-manager';
4
- export type { BullMQConnectionConfig, BullMQEventAdapterConfig } from './types';
5
- export type { SubscriberInfo } from './worker-manager';
6
- export { WorkerManager } from './worker-manager';
7
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAA;AAC3D,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAC1C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9C,YAAY,EAAE,sBAAsB,EAAE,wBAAwB,EAAE,MAAM,SAAS,CAAA;AAC/E,YAAY,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA"}
package/dist/index.js DELETED
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WorkerManager = exports.QueueManager = exports.DLQManager = exports.BullMQEventAdapter = void 0;
4
- var bullmq_event_adapter_1 = require("./bullmq-event-adapter");
5
- Object.defineProperty(exports, "BullMQEventAdapter", { enumerable: true, get: function () { return bullmq_event_adapter_1.BullMQEventAdapter; } });
6
- var dlq_manager_1 = require("./dlq-manager");
7
- Object.defineProperty(exports, "DLQManager", { enumerable: true, get: function () { return dlq_manager_1.DLQManager; } });
8
- var queue_manager_1 = require("./queue-manager");
9
- Object.defineProperty(exports, "QueueManager", { enumerable: true, get: function () { return queue_manager_1.QueueManager; } });
10
- var worker_manager_1 = require("./worker-manager");
11
- Object.defineProperty(exports, "WorkerManager", { enumerable: true, get: function () { return worker_manager_1.WorkerManager; } });
@@ -1,20 +0,0 @@
1
- import type { Event } from '@motiadev/core';
2
- import { Queue } from 'bullmq';
3
- import type { Redis } from 'ioredis';
4
- import type { MergedConfig } from './config-builder';
5
- import type { SubscriberInfo } from './worker-manager';
6
- export declare class QueueManager {
7
- private readonly queues;
8
- private readonly connection;
9
- private readonly config;
10
- constructor(connection: Redis, config: MergedConfig);
11
- getQueue(queueName: string): Queue;
12
- getQueueName(topic: string, stepName: string): string;
13
- enqueueToAll<TData>(event: Event<TData>, subscribers: SubscriberInfo[]): Promise<void>;
14
- closeQueue(queueName: string): Promise<void>;
15
- closeAll(): Promise<void>;
16
- listQueueNames(): string[];
17
- getPrefix(): string;
18
- getConnection(): Redis;
19
- }
20
- //# sourceMappingURL=queue-manager.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"queue-manager.d.ts","sourceRoot":"","sources":["../src/queue-manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA;AAC3C,OAAO,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAA;AAC9B,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AACpC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAA;AAGpD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AAEtD,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgC;IACvD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAO;IAClC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAc;gBAEzB,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,YAAY;IAKnD,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,KAAK;IAsBlC,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM;IAI/C,YAAY,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,EAAE,WAAW,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAiCtF,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQ5C,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAU/B,cAAc,IAAI,MAAM,EAAE;IAI1B,SAAS,IAAI,MAAM;IAInB,aAAa,IAAI,KAAK;CAGvB"}
@@ -1,85 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.QueueManager = void 0;
4
- const bullmq_1 = require("bullmq");
5
- const constants_1 = require("./constants");
6
- const errors_1 = require("./errors");
7
- class QueueManager {
8
- constructor(connection, config) {
9
- this.queues = new Map();
10
- this.connection = connection;
11
- this.config = config;
12
- }
13
- getQueue(queueName) {
14
- if (!this.queues.has(queueName)) {
15
- const queue = new bullmq_1.Queue(queueName, {
16
- connection: this.connection,
17
- prefix: this.config.prefix,
18
- defaultJobOptions: this.config.defaultJobOptions,
19
- });
20
- queue.on('error', (err) => {
21
- console.error(`[BullMQ] Queue error for ${queueName}:`, err);
22
- });
23
- this.queues.set(queueName, queue);
24
- }
25
- const queue = this.queues.get(queueName);
26
- if (!queue) {
27
- throw new errors_1.QueueCreationError(queueName);
28
- }
29
- return queue;
30
- }
31
- getQueueName(topic, stepName) {
32
- return `${topic}.${stepName}`;
33
- }
34
- async enqueueToAll(event, subscribers) {
35
- const promises = subscribers.map((subscriber) => {
36
- const queueName = this.getQueueName(subscriber.topic, subscriber.stepName);
37
- const queue = this.getQueue(queueName);
38
- const jobId = event.messageGroupId ? `${queueName}.${event.messageGroupId}` : undefined;
39
- const jobData = {
40
- topic: event.topic,
41
- data: event.data,
42
- traceId: event.traceId,
43
- flows: event.flows,
44
- messageGroupId: event.messageGroupId,
45
- };
46
- const maxRetries = subscriber.queueConfig?.maxRetries;
47
- const attempts = maxRetries != null ? maxRetries + 1 : this.config.defaultJobOptions.attempts;
48
- const delay = subscriber.queueConfig?.delaySeconds
49
- ? subscriber.queueConfig.delaySeconds * constants_1.MILLISECONDS_PER_SECOND
50
- : undefined;
51
- const jobOptions = {
52
- jobId,
53
- attempts,
54
- backoff: this.config.defaultJobOptions.backoff,
55
- delay,
56
- };
57
- return queue.add(event.topic, jobData, jobOptions).then(() => undefined);
58
- });
59
- await Promise.all(promises);
60
- }
61
- async closeQueue(queueName) {
62
- const queue = this.queues.get(queueName);
63
- if (queue) {
64
- await queue.close();
65
- this.queues.delete(queueName);
66
- }
67
- }
68
- async closeAll() {
69
- const promises = Array.from(this.queues.values()).map((queue) => queue.close().catch((err) => {
70
- console.error(`[BullMQ] Error closing queue:`, err);
71
- }));
72
- await Promise.allSettled(promises);
73
- this.queues.clear();
74
- }
75
- listQueueNames() {
76
- return Array.from(this.queues.keys());
77
- }
78
- getPrefix() {
79
- return this.config.prefix;
80
- }
81
- getConnection() {
82
- return this.connection;
83
- }
84
- }
85
- exports.QueueManager = QueueManager;
package/dist/types.d.ts DELETED
@@ -1,23 +0,0 @@
1
- import type { KeepJobs } from 'bullmq';
2
- import type { Redis, RedisOptions } from 'ioredis';
3
- export type BullMQConnectionConfig = Redis | RedisOptions;
4
- export interface BullMQEventAdapterConfig {
5
- connection: BullMQConnectionConfig;
6
- concurrency?: number;
7
- defaultJobOptions?: {
8
- attempts?: number;
9
- backoff?: {
10
- type: 'fixed' | 'exponential';
11
- delay: number;
12
- };
13
- removeOnComplete?: KeepJobs;
14
- removeOnFail?: KeepJobs;
15
- };
16
- prefix?: string;
17
- dlq?: {
18
- enabled?: boolean;
19
- ttl?: number;
20
- suffix?: string;
21
- };
22
- }
23
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAA;AACtC,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAElD,MAAM,MAAM,sBAAsB,GAAG,KAAK,GAAG,YAAY,CAAA;AAEzD,MAAM,WAAW,wBAAwB;IACvC,UAAU,EAAE,sBAAsB,CAAA;IAClC,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,iBAAiB,CAAC,EAAE;QAClB,QAAQ,CAAC,EAAE,MAAM,CAAA;QACjB,OAAO,CAAC,EAAE;YACR,IAAI,EAAE,OAAO,GAAG,aAAa,CAAA;YAC7B,KAAK,EAAE,MAAM,CAAA;SACd,CAAA;QACD,gBAAgB,CAAC,EAAE,QAAQ,CAAA;QAC3B,YAAY,CAAC,EAAE,QAAQ,CAAA;KACxB,CAAA;IACD,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,GAAG,CAAC,EAAE;QACJ,OAAO,CAAC,EAAE,OAAO,CAAA;QACjB,GAAG,CAAC,EAAE,MAAM,CAAA;QACZ,MAAM,CAAC,EAAE,MAAM,CAAA;KAChB,CAAA;CACF"}
package/dist/types.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,38 +0,0 @@
1
- import type { Event, QueueConfig, SubscriptionHandle } from '@motiadev/core';
2
- import { Worker } from 'bullmq';
3
- import type { Redis } from 'ioredis';
4
- import type { MergedConfig } from './config-builder';
5
- import type { DLQManager } from './dlq-manager';
6
- export type SubscriberInfo = {
7
- topic: string;
8
- stepName: string;
9
- queueConfig?: QueueConfig;
10
- };
11
- type WorkerInfo = {
12
- worker: Worker;
13
- topic: string;
14
- stepName: string;
15
- handle: SubscriptionHandle;
16
- queueConfig?: QueueConfig;
17
- };
18
- export declare class WorkerManager {
19
- private readonly workers;
20
- private readonly topicSubscriptions;
21
- private readonly connection;
22
- private readonly config;
23
- private readonly getQueueName;
24
- private readonly dlqManager;
25
- constructor(connection: Redis, config: MergedConfig, getQueueName: (topic: string, stepName: string) => string, dlqManager?: DLQManager);
26
- createWorker<TData>(topic: string, stepName: string, handler: (event: Event<TData>) => void | Promise<void>, options?: QueueConfig): SubscriptionHandle;
27
- getSubscribers(topic: string): SubscriberInfo[];
28
- getWorkerInfo(id: string): WorkerInfo | undefined;
29
- removeWorker(id: string): Promise<void>;
30
- closeAll(): Promise<void>;
31
- getSubscriptionCount(topic: string): number;
32
- listTopics(): string[];
33
- private addTopicSubscription;
34
- private removeTopicSubscription;
35
- private setupWorkerHandlers;
36
- }
37
- export {};
38
- //# sourceMappingURL=worker-manager.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"worker-manager.d.ts","sourceRoot":"","sources":["../src/worker-manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAA;AAC5E,OAAO,EAAY,MAAM,EAAE,MAAM,QAAQ,CAAA;AACzC,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAEpC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAA;AAEpD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAG/C,MAAM,MAAM,cAAc,GAAG;IAC3B,KAAK,EAAE,MAAM,CAAA;IACb,QAAQ,EAAE,MAAM,CAAA;IAChB,WAAW,CAAC,EAAE,WAAW,CAAA;CAC1B,CAAA;AAED,KAAK,UAAU,GAAG;IAChB,MAAM,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,MAAM,CAAA;IACb,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,kBAAkB,CAAA;IAC1B,WAAW,CAAC,EAAE,WAAW,CAAA;CAC1B,CAAA;AAUD,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAqC;IAC7D,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAsC;IACzE,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAO;IAClC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAc;IACrC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAA6C;IAC1E,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAmB;gBAG5C,UAAU,EAAE,KAAK,EACjB,MAAM,EAAE,YAAY,EACpB,YAAY,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,MAAM,EACzD,UAAU,CAAC,EAAE,UAAU;IAQzB,YAAY,CAAC,KAAK,EAChB,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,EACtD,OAAO,CAAC,EAAE,WAAW,GACpB,kBAAkB;IAuDrB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,cAAc,EAAE;IAY/C,aAAa,CAAC,EAAE,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS;IAI3C,YAAY,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAWvC,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAW/B,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAI3C,UAAU,IAAI,MAAM,EAAE;IAItB,OAAO,CAAC,oBAAoB;IAO5B,OAAO,CAAC,uBAAuB;IAU/B,OAAO,CAAC,mBAAmB;CAyB5B"}
@@ -1,136 +0,0 @@
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;