@hastehaul/common 2.4.4 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,11 @@
1
1
  import { Job, Worker } from "bullmq";
2
+ import { EventEmitter } from 'events';
2
3
  /**
3
4
  * Abstract class representing a worker for processing jobs from a Job Queue.
4
5
  */
5
6
  export declare abstract class AbstractWorker {
6
7
  protected worker: Worker;
8
+ protected events: EventEmitter;
7
9
  /**
8
10
  * Creates an instance of AbstractWorker.
9
11
  *
@@ -4,6 +4,7 @@ exports.AbstractWorker = void 0;
4
4
  const bullmq_1 = require("bullmq");
5
5
  const redis_connection_wrapper_1 = require("../../../connections-wrappers/redis-connection-wrapper");
6
6
  const redis_wrapper_1 = require("../../../connections-wrappers/redis-wrapper");
7
+ const events_1 = require("events");
7
8
  /**
8
9
  * Abstract class representing a worker for processing jobs from a Job Queue.
9
10
  */
@@ -14,11 +15,17 @@ class AbstractWorker {
14
15
  * @param {string} queueName - The name of the job queue that this worker will process jobs from.
15
16
  */
16
17
  constructor(queueName, useReplicas = true, opts) {
18
+ this.events = new events_1.EventEmitter();
17
19
  // Create a worker instance with the provided queueName and bind the 'process' method to this worker.
18
20
  this.worker = new bullmq_1.Worker(queueName, this.process.bind(this), Object.assign({ connection: useReplicas ? redis_wrapper_1.rWrapper.masterClient : redis_connection_wrapper_1.redisWrapper.client, autorun: false, prefix: '{BULLMQ}' }, opts));
19
21
  // Handle errors that may occur during job processing.
20
22
  this.worker.on("error", (err) => {
21
23
  console.error(`${queueName} Error: `, err);
24
+ this.events.emit(`${queueName}-error`, err);
25
+ });
26
+ // Emit an event when a job is completed.
27
+ this.worker.on('completed', (job, returnvalue) => {
28
+ this.events.emit(`${queueName}-completed`, job, returnvalue);
22
29
  });
23
30
  // Start the worker in non-autorun mode, which means it won't start processing jobs automatically.
24
31
  console.log(" --- WORKER STARTED --- ", queueName);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hastehaul/common",
3
- "version": "2.4.4",
3
+ "version": "2.5.0",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",