@opens/bullmq 1.0.2 → 1.0.4

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.
@@ -0,0 +1,4 @@
1
+ import { Redis } from 'ioredis';
2
+ export declare class DefaultConnection {
3
+ static getInstance(): Redis;
4
+ }
@@ -0,0 +1,12 @@
1
+ import { Redis } from 'ioredis';
2
+ export class DefaultConnection {
3
+ static getInstance() {
4
+ const host = process.env.BULLMQ_REDIS_HOST;
5
+ const port = process.env.BULLMQ_REDIS_PORT;
6
+ if (!host)
7
+ throw new Error('Environment variable ${BULLMQ_REDIS_HOST} is undefined');
8
+ if (!port)
9
+ throw new Error('Environment variable ${BULLMQ_REDIS_PORT} is undefined');
10
+ return new Redis({ host, port: parseInt(port), maxRetriesPerRequest: null });
11
+ }
12
+ }
package/dist/index.d.ts CHANGED
@@ -1,51 +1,2 @@
1
- import BullMQ from 'bullmq';
2
- import { Redis } from 'ioredis';
3
-
4
- declare class BullMQWrapper {
5
- private defaultConnection;
6
- private bullMQ;
7
- private taskQueues;
8
- constructor(defaultConnection: Redis, bullMQ: typeof BullMQ);
9
- createTask(params: {
10
- taskName: string;
11
- defaultOptions: BullMQ.DefaultJobOptions;
12
- connection?: Redis;
13
- up?: (data: any, job: BullMQ.Job) => Promise<void>;
14
- down?: (data: any, job: BullMQ.Job, err: unknown) => Promise<void>;
15
- concurrency?: number;
16
- removeOnCompleteAge?: number;
17
- removeOnCompleteCount?: number;
18
- removeOnFailAge?: number;
19
- removeOnFailCount?: number;
20
- }): {
21
- queue: BullMQ.Queue<any, any, string, any, any, string>;
22
- worker?: undefined;
23
- } | {
24
- queue: BullMQ.Queue<any, any, string, any, any, string>;
25
- worker: BullMQ.Worker<any, any, string>;
26
- } | undefined;
27
- createWorker(params: {
28
- taskName: string;
29
- up: (data: any, job: BullMQ.Job) => Promise<void>;
30
- down?: (data: any, job: BullMQ.Job, err: unknown) => Promise<void>;
31
- connection?: Redis;
32
- concurrency: number;
33
- removeOnCompleteAge?: number;
34
- removeOnCompleteCount?: number;
35
- removeOnFailAge?: number;
36
- removeOnFailCount?: number;
37
- onError?: Function;
38
- }): {
39
- worker: BullMQ.Worker<any, any, string>;
40
- };
41
- publishTask(params: {
42
- taskName: string;
43
- taskData: any;
44
- taskOptions: BullMQ.JobsOptions;
45
- }): Promise<BullMQ.JobJson | null>;
46
- getTaskCount(): number;
47
- }
48
-
49
- declare const bullmq: BullMQWrapper;
50
-
51
- export { bullmq };
1
+ import { BullMQWrapper } from './wrapper';
2
+ export declare const bullmq: BullMQWrapper;
package/dist/index.js CHANGED
@@ -1,131 +1,3 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/index.ts
31
- var index_exports = {};
32
- __export(index_exports, {
33
- bullmq: () => bullmq
34
- });
35
- module.exports = __toCommonJS(index_exports);
36
-
37
- // src/wrapper.ts
38
- var BullMQWrapper = class {
39
- constructor(defaultConnection, bullMQ) {
40
- this.defaultConnection = defaultConnection;
41
- this.bullMQ = bullMQ;
42
- }
43
- taskQueues = /* @__PURE__ */ new Map();
44
- createTask(params) {
45
- const { taskName, up, down, defaultOptions, connection, concurrency } = params;
46
- if (this.taskQueues.has(taskName)) return;
47
- const queue = new this.bullMQ.Queue(taskName, {
48
- connection: this.defaultConnection ?? connection,
49
- defaultJobOptions: defaultOptions
50
- });
51
- this.taskQueues.set(taskName, queue);
52
- if (!up) return { queue };
53
- const { worker } = this.createWorker({
54
- up,
55
- down,
56
- concurrency: concurrency ?? 1,
57
- ...params
58
- });
59
- return { queue, worker };
60
- }
61
- createWorker(params) {
62
- const {
63
- taskName,
64
- up,
65
- down,
66
- concurrency,
67
- connection,
68
- removeOnFailCount,
69
- removeOnCompleteAge,
70
- removeOnCompleteCount,
71
- removeOnFailAge
72
- } = params;
73
- const registeredQueue = this.taskQueues.get(taskName);
74
- if (!registeredQueue) throw new Error(`No task queue for [${taskName}] found`);
75
- const worker = new this.bullMQ.Worker(
76
- taskName,
77
- async (job) => {
78
- try {
79
- await up(job.data, job);
80
- } catch (error) {
81
- if (down) await down(job.data, job, error);
82
- throw error;
83
- } finally {
84
- }
85
- },
86
- {
87
- concurrency,
88
- connection: connection ?? this.defaultConnection,
89
- removeOnComplete: {
90
- age: removeOnCompleteAge ?? 0,
91
- count: removeOnCompleteCount ?? 0
92
- },
93
- removeOnFail: {
94
- age: removeOnFailAge ?? 0,
95
- count: removeOnFailCount ?? 0
96
- }
97
- }
98
- );
99
- return { worker };
100
- }
101
- async publishTask(params) {
102
- const queue = this.taskQueues.get(params.taskName);
103
- if (!queue) return null;
104
- const job = await queue.add("", params.taskData, params.taskOptions);
105
- return job.asJSON();
106
- }
107
- getTaskCount() {
108
- return this.taskQueues.size;
109
- }
110
- };
111
-
112
- // src/default-connection.ts
113
- var import_ioredis = require("ioredis");
114
- var DefaultConnection = class {
115
- static getInstance() {
116
- const host = process.env.BULLMQ_REDIS_HOST;
117
- const port = process.env.BULLMQ_REDIS_PORT;
118
- if (!host) throw new Error("Environment variable ${BULLMQ_REDIS_HOST} is undefined");
119
- if (!port) throw new Error("Environment variable ${BULLMQ_REDIS_PORT} is undefined");
120
- return new import_ioredis.Redis({ host, port: parseInt(port), maxRetriesPerRequest: null });
121
- }
122
- };
123
-
124
- // src/index.ts
125
- var import_bullmq = __toESM(require("bullmq"));
126
- var bullmq = new BullMQWrapper(DefaultConnection.getInstance(), import_bullmq.default);
127
- // Annotate the CommonJS export names for ESM import in node:
128
- 0 && (module.exports = {
129
- bullmq
130
- });
131
- //# sourceMappingURL=index.js.map
1
+ import { BullMQWrapper } from './wrapper';
2
+ import { DefaultConnection } from './default-connection';
3
+ export const bullmq = new BullMQWrapper(DefaultConnection.getInstance());
@@ -0,0 +1,45 @@
1
+ import BullMQ from 'bullmq';
2
+ import { Redis } from 'ioredis';
3
+ export declare class BullMQWrapper {
4
+ private defaultConnection;
5
+ private taskQueues;
6
+ constructor(defaultConnection: Redis);
7
+ createTask(params: {
8
+ taskName: string;
9
+ defaultOptions: BullMQ.DefaultJobOptions;
10
+ connection?: Redis;
11
+ up?: (data: any, job: BullMQ.Job) => Promise<void>;
12
+ down?: (data: any, job: BullMQ.Job, err: unknown) => Promise<void>;
13
+ concurrency?: number;
14
+ removeOnCompleteAge?: number;
15
+ removeOnCompleteCount?: number;
16
+ removeOnFailAge?: number;
17
+ removeOnFailCount?: number;
18
+ }): {
19
+ queue: BullMQ.Queue<any, any, string, any, any, string>;
20
+ worker?: undefined;
21
+ } | {
22
+ queue: BullMQ.Queue<any, any, string, any, any, string>;
23
+ worker: BullMQ.Worker<any, any, string>;
24
+ } | undefined;
25
+ createWorker(params: {
26
+ taskName: string;
27
+ up: (data: any, job: BullMQ.Job) => Promise<void>;
28
+ down?: (data: any, job: BullMQ.Job, err: unknown) => Promise<void>;
29
+ connection?: Redis;
30
+ concurrency: number;
31
+ removeOnCompleteAge?: number;
32
+ removeOnCompleteCount?: number;
33
+ removeOnFailAge?: number;
34
+ removeOnFailCount?: number;
35
+ onError?: Function;
36
+ }): {
37
+ worker: BullMQ.Worker<any, any, string>;
38
+ };
39
+ publishTask(params: {
40
+ taskName: string;
41
+ taskData: any;
42
+ taskOptions: BullMQ.JobsOptions;
43
+ }): Promise<BullMQ.JobJson | null>;
44
+ getTaskCount(): number;
45
+ }
@@ -0,0 +1,68 @@
1
+ import BullMQ from 'bullmq';
2
+ export class BullMQWrapper {
3
+ defaultConnection;
4
+ taskQueues = new Map();
5
+ constructor(defaultConnection) {
6
+ this.defaultConnection = defaultConnection;
7
+ }
8
+ createTask(params) {
9
+ const { taskName, up, down, defaultOptions, connection, concurrency } = params;
10
+ if (this.taskQueues.has(taskName))
11
+ return;
12
+ const queue = new BullMQ.Queue(taskName, {
13
+ connection: this.defaultConnection ?? connection,
14
+ defaultJobOptions: { backoff: { delay: 1000 * 60 * 5, type: 'fixed' }, attempts: 10, ...defaultOptions },
15
+ });
16
+ this.taskQueues.set(taskName, queue);
17
+ console.debug("[@opens/bullmq]: task queue'${taskName}' created");
18
+ if (!up)
19
+ return { queue };
20
+ const { worker } = this.createWorker({
21
+ up,
22
+ down,
23
+ concurrency: concurrency ?? 1,
24
+ ...params,
25
+ });
26
+ return { queue, worker };
27
+ }
28
+ createWorker(params) {
29
+ const { taskName, up, down, concurrency, connection, removeOnFailCount, removeOnCompleteAge, removeOnCompleteCount, removeOnFailAge, } = params;
30
+ const registeredQueue = this.taskQueues.get(taskName);
31
+ if (!registeredQueue)
32
+ throw new Error(`No task queue for [${taskName}] found`);
33
+ const worker = new BullMQ.Worker(taskName, async (job) => {
34
+ try {
35
+ await up(job.data, job);
36
+ }
37
+ catch (error) {
38
+ if (down)
39
+ await down(job.data, job, error);
40
+ throw error;
41
+ }
42
+ finally {
43
+ }
44
+ }, {
45
+ concurrency,
46
+ connection: connection ?? this.defaultConnection,
47
+ removeOnComplete: {
48
+ age: removeOnCompleteAge ?? 0,
49
+ count: removeOnCompleteCount ?? 0,
50
+ },
51
+ removeOnFail: {
52
+ age: removeOnFailAge ?? 0,
53
+ count: removeOnFailCount ?? 0,
54
+ },
55
+ });
56
+ return { worker };
57
+ }
58
+ async publishTask(params) {
59
+ const queue = this.taskQueues.get(params.taskName);
60
+ if (!queue)
61
+ return null;
62
+ const job = await queue.add('', params.taskData, params.taskOptions);
63
+ return job.asJSON();
64
+ }
65
+ getTaskCount() {
66
+ return this.taskQueues.size;
67
+ }
68
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opens/bullmq",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "A wrapper around bullmq",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -9,7 +9,7 @@
9
9
  "dist"
10
10
  ],
11
11
  "scripts": {
12
- "build": "tsup --dts",
12
+ "build": "tsc",
13
13
  "dev": "tsup --watch",
14
14
  "test": "vitest run",
15
15
  "test:watch": "vitest"
@@ -19,8 +19,7 @@
19
19
  "src/index.ts"
20
20
  ],
21
21
  "splitting": false,
22
- "sourcemap": true,
23
- "clean": true
22
+ "sourcemap": true
24
23
  },
25
24
  "author": "Joao Victor Clementino",
26
25
  "license": "ISC",
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/index.ts","../src/wrapper.ts","../src/default-connection.ts"],"sourcesContent":["import { BullMQWrapper } from './wrapper';\nimport { DefaultConnection } from './default-connection';\nimport BullMQ from 'bullmq';\nexport const bullmq = new BullMQWrapper(DefaultConnection.getInstance(), BullMQ);\n","import BullMQ from 'bullmq';\nimport { Redis } from 'ioredis';\n\nexport class BullMQWrapper {\n private taskQueues: Map<string, BullMQ.Queue> = new Map();\n\n constructor(private defaultConnection: Redis, private bullMQ: typeof BullMQ) {}\n\n public createTask(params: {\n taskName: string;\n defaultOptions: BullMQ.DefaultJobOptions;\n connection?: Redis;\n up?: (data: any, job: BullMQ.Job) => Promise<void>;\n down?: (data: any, job: BullMQ.Job, err: unknown) => Promise<void>;\n concurrency?: number;\n removeOnCompleteAge?: number;\n removeOnCompleteCount?: number;\n removeOnFailAge?: number;\n removeOnFailCount?: number;\n }) {\n const { taskName, up, down, defaultOptions, connection, concurrency } = params;\n if (this.taskQueues.has(taskName)) return;\n\n const queue = new this.bullMQ.Queue(taskName, {\n connection: this.defaultConnection ?? connection,\n defaultJobOptions: defaultOptions,\n });\n this.taskQueues.set(taskName, queue);\n\n if (!up) return { queue };\n\n const { worker } = this.createWorker({\n up,\n down,\n concurrency: concurrency ?? 1,\n ...params,\n });\n return { queue, worker };\n }\n\n public createWorker(params: {\n taskName: string;\n up: (data: any, job: BullMQ.Job) => Promise<void>;\n down?: (data: any, job: BullMQ.Job, err: unknown) => Promise<void>;\n connection?: Redis;\n concurrency: number;\n removeOnCompleteAge?: number;\n removeOnCompleteCount?: number;\n removeOnFailAge?: number;\n removeOnFailCount?: number;\n onError?: Function;\n }) {\n const {\n taskName,\n up,\n down,\n concurrency,\n connection,\n removeOnFailCount,\n removeOnCompleteAge,\n removeOnCompleteCount,\n removeOnFailAge,\n } = params;\n const registeredQueue = this.taskQueues.get(taskName);\n if (!registeredQueue) throw new Error(`No task queue for [${taskName}] found`);\n\n const worker = new this.bullMQ.Worker(\n taskName,\n async (job) => {\n try {\n await up(job.data, job);\n } catch (error) {\n if (down) await down(job.data, job, error);\n throw error;\n } finally {\n }\n },\n {\n concurrency,\n connection: connection ?? this.defaultConnection,\n removeOnComplete: {\n age: removeOnCompleteAge ?? 0,\n count: removeOnCompleteCount ?? 0,\n },\n removeOnFail: {\n age: removeOnFailAge ?? 0,\n count: removeOnFailCount ?? 0,\n },\n },\n );\n\n return { worker };\n }\n\n public async publishTask(params: { taskName: string; taskData: any; taskOptions: BullMQ.JobsOptions }) {\n const queue = this.taskQueues.get(params.taskName);\n if (!queue) return null;\n const job = await queue.add('', params.taskData, params.taskOptions);\n return job.asJSON();\n }\n\n public getTaskCount() {\n return this.taskQueues.size;\n }\n}\n","import { Redis } from 'ioredis';\nexport class DefaultConnection {\n static getInstance() {\n const host = process.env.BULLMQ_REDIS_HOST;\n const port = process.env.BULLMQ_REDIS_PORT;\n if (!host) throw new Error('Environment variable ${BULLMQ_REDIS_HOST} is undefined');\n if (!port) throw new Error('Environment variable ${BULLMQ_REDIS_PORT} is undefined');\n return new Redis({ host, port: parseInt(port), maxRetriesPerRequest: null });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGO,IAAM,gBAAN,MAAoB;AAAA,EAGzB,YAAoB,mBAAkC,QAAuB;AAAzD;AAAkC;AAAA,EAAwB;AAAA,EAFtE,aAAwC,oBAAI,IAAI;AAAA,EAIjD,WAAW,QAWf;AACD,UAAM,EAAE,UAAU,IAAI,MAAM,gBAAgB,YAAY,YAAY,IAAI;AACxE,QAAI,KAAK,WAAW,IAAI,QAAQ,EAAG;AAEnC,UAAM,QAAQ,IAAI,KAAK,OAAO,MAAM,UAAU;AAAA,MAC5C,YAAY,KAAK,qBAAqB;AAAA,MACtC,mBAAmB;AAAA,IACrB,CAAC;AACD,SAAK,WAAW,IAAI,UAAU,KAAK;AAEnC,QAAI,CAAC,GAAI,QAAO,EAAE,MAAM;AAExB,UAAM,EAAE,OAAO,IAAI,KAAK,aAAa;AAAA,MACnC;AAAA,MACA;AAAA,MACA,aAAa,eAAe;AAAA,MAC5B,GAAG;AAAA,IACL,CAAC;AACD,WAAO,EAAE,OAAO,OAAO;AAAA,EACzB;AAAA,EAEO,aAAa,QAWjB;AACD,UAAM;AAAA,MACJ;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,IAAI;AACJ,UAAM,kB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