@hastehaul/common 2.0.48 → 2.0.50

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,10 @@
1
+ import IORedis from 'ioredis';
1
2
  export declare class RedisClient {
2
- private master;
3
- private replicas;
4
- private static instance;
5
- private constructor();
6
- get client(): RedisClient;
7
- static connect(masterHost: string, replicaHosts: string[]): Promise<RedisClient>;
3
+ private master?;
4
+ private replicas?;
5
+ get masterClient(): IORedis;
6
+ get relicaClients(): IORedis[];
7
+ connect(masterHost: string, replicaHosts: string[]): Promise<void>;
8
8
  private executeMasterOperation;
9
9
  private executeReplicaOperation;
10
10
  private executeOperation;
@@ -14,3 +14,4 @@ export declare class RedisClient {
14
14
  setex(key: string, value: string, seconds: number): Promise<any>;
15
15
  quit(): void;
16
16
  }
17
+ export declare const rWrapper: RedisClient;
@@ -32,52 +32,48 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
32
32
  });
33
33
  };
34
34
  Object.defineProperty(exports, "__esModule", { value: true });
35
- exports.RedisClient = void 0;
35
+ exports.rWrapper = exports.RedisClient = void 0;
36
36
  const ioredis_1 = __importStar(require("ioredis"));
37
37
  class RedisClient {
38
- constructor(masterHost, replicaHosts) {
39
- this.master = new ioredis_1.default({ host: masterHost, port: 6379 });
40
- this.replicas = replicaHosts.map(replicaHost => new ioredis_1.default({ host: replicaHost, port: 6379 }));
41
- const masterConnected = new Promise((resolve) => {
42
- this.master.on('connect', () => {
43
- console.log('Master connected');
44
- resolve();
45
- });
46
- this.master.on('error', (error) => {
47
- console.log('Master connection error:', error.message);
48
- resolve();
49
- });
50
- });
51
- const replicaPromises = this.replicas.map((replica) => {
52
- return new Promise((resolve) => {
53
- replica.on('connect', () => {
54
- console.log('Replica connected');
38
+ get masterClient() {
39
+ if (!this.master)
40
+ throw new Error("Cannot access Master Redis Client before connecting");
41
+ return this.master;
42
+ }
43
+ get relicaClients() {
44
+ if (!this.replicas)
45
+ throw new Error("Cannot access Replica Redis Client before connecting");
46
+ return this.replicas;
47
+ }
48
+ connect(masterHost, replicaHosts) {
49
+ return __awaiter(this, void 0, void 0, function* () {
50
+ this.master = new ioredis_1.default({ host: masterHost, port: 6379, maxRetriesPerRequest: null, });
51
+ this.replicas = replicaHosts.map(replicaHost => new ioredis_1.default({ host: replicaHost, port: 6379, maxRetriesPerRequest: null, }));
52
+ const masterConnected = new Promise((resolve) => {
53
+ this.master.on('connect', () => {
54
+ console.log('Master connected');
55
55
  resolve();
56
56
  });
57
- replica.on('error', (error) => {
58
- console.log('Replica connection error:', error.message);
57
+ this.master.on('error', (error) => {
58
+ console.log('Master connection error:', error.message);
59
59
  resolve();
60
60
  });
61
61
  });
62
- });
63
- // Wait for all connections to be established
64
- Promise.all([masterConnected, ...replicaPromises]).then(() => {
65
- console.log('All connections established');
66
- });
67
- }
68
- get client() {
69
- if (!RedisClient.instance)
70
- throw new Error("Cannot access Redis Client before connecting");
71
- return RedisClient.instance;
72
- }
73
- static connect(masterHost, replicaHosts) {
74
- return new Promise((resolve) => {
75
- if (!RedisClient.instance) {
76
- const redisClient = new RedisClient(masterHost, replicaHosts);
77
- RedisClient.instance = redisClient;
78
- resolve(redisClient);
79
- }
80
- resolve(RedisClient.instance);
62
+ const replicaPromises = this.replicas.map((replica) => {
63
+ return new Promise((resolve) => {
64
+ replica.on('connect', () => {
65
+ console.log('Replica connected');
66
+ resolve();
67
+ });
68
+ replica.on('error', (error) => {
69
+ console.log('Replica connection error:', error.message);
70
+ resolve();
71
+ });
72
+ });
73
+ });
74
+ // Wait for all connections to be established
75
+ yield Promise.all([masterConnected, ...replicaPromises]);
76
+ return console.log('All connections established');
81
77
  });
82
78
  }
83
79
  executeMasterOperation(command, ...args) {
@@ -116,4 +112,4 @@ class RedisClient {
116
112
  }
117
113
  }
118
114
  exports.RedisClient = RedisClient;
119
- RedisClient.instance = null;
115
+ exports.rWrapper = new RedisClient();
@@ -2,7 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AbstractJobQueue = void 0;
4
4
  const bullmq_1 = require("bullmq");
5
- const redis_connection_wrapper_1 = require("../../../connections-wrappers/redis-connection-wrapper");
5
+ // import { redisWrapper } from '../../../connections-wrappers/redis-connection-wrapper';
6
+ const redis_wrapper_1 = require("../../../connections-wrappers/redis-wrapper");
6
7
  /**
7
8
  * Abstract class representing a Job Queue.
8
9
  */
@@ -14,7 +15,7 @@ class AbstractJobQueue {
14
15
  */
15
16
  constructor(queueName) {
16
17
  // Initialize the job queue with the provided name and a Redis connection from the redisWrapper.
17
- this.que = new bullmq_1.Queue(queueName, { connection: redis_connection_wrapper_1.redisWrapper.client, prefix: '{BULLMQ}' });
18
+ this.que = new bullmq_1.Queue(queueName, { connection: redis_wrapper_1.rWrapper.masterClient, prefix: '{BULLMQ}' });
18
19
  }
19
20
  }
20
21
  exports.AbstractJobQueue = AbstractJobQueue;
@@ -2,7 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AbstractWorker = void 0;
4
4
  const bullmq_1 = require("bullmq");
5
- const redis_connection_wrapper_1 = require("../../../connections-wrappers/redis-connection-wrapper");
5
+ // import { redisWrapper } from '../../../connections-wrappers/redis-connection-wrapper';
6
+ const redis_wrapper_1 = require("../../../connections-wrappers/redis-wrapper");
6
7
  /**
7
8
  * Abstract class representing a worker for processing jobs from a Job Queue.
8
9
  */
@@ -15,7 +16,7 @@ class AbstractWorker {
15
16
  constructor(queueName) {
16
17
  // Create a worker instance with the provided queueName and bind the 'process' method to this worker.
17
18
  this.worker = new bullmq_1.Worker(queueName, this.process.bind(this), {
18
- connection: redis_connection_wrapper_1.redisWrapper.client,
19
+ connection: redis_wrapper_1.rWrapper.masterClient,
19
20
  autorun: false,
20
21
  prefix: '{BULLMQ}',
21
22
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hastehaul/common",
3
- "version": "2.0.48",
3
+ "version": "2.0.50",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",