@hastehaul/common 2.0.48 → 2.0.49
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
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
39
|
-
this.master
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
console.log('
|
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 });
|
51
|
+
this.replicas = replicaHosts.map(replicaHost => new ioredis_1.default({ host: replicaHost, port: 6379 }));
|
52
|
+
const masterConnected = new Promise((resolve) => {
|
53
|
+
this.master.on('connect', () => {
|
54
|
+
console.log('Master connected');
|
55
55
|
resolve();
|
56
56
|
});
|
57
|
-
|
58
|
-
console.log('
|
57
|
+
this.master.on('error', (error) => {
|
58
|
+
console.log('Master connection error:', error.message);
|
59
59
|
resolve();
|
60
60
|
});
|
61
61
|
});
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
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
|
-
|
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
|
-
|
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:
|
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
|
-
|
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:
|
19
|
+
connection: redis_wrapper_1.rWrapper.masterClient,
|
19
20
|
autorun: false,
|
20
21
|
prefix: '{BULLMQ}',
|
21
22
|
});
|