message-brokers 1.3.1 → 1.4.0
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.
- package/README.md +1 -0
- package/lib/rabbitmq/rabbitmq.helper.js +42 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -38,6 +38,7 @@ RABBITMQ_URL=amqp://admin:pwd@localhost:5672
|
|
|
38
38
|
ACTIVE_MESSAGE_BROKER=rabbitmq
|
|
39
39
|
REDIS_PORT=6260
|
|
40
40
|
CACHING_ENABLED=true
|
|
41
|
+
RABBITMQ_DEPLOYMENT_ENV=stage (if using same rabbitmq stack for multiple environments, exchanges/queues/routing-keys get appended with this value. e.g. if rabbitmq deployment env is stage, then exchanges/queues/routing-keys will be appended with .stage)
|
|
41
42
|
```
|
|
42
43
|
|
|
43
44
|
```javascript
|
|
@@ -13,18 +13,23 @@ const CacheService = require("../services/cache.service");
|
|
|
13
13
|
const offlinePubQueue = [];
|
|
14
14
|
const RabbitMQHelper = {};
|
|
15
15
|
const CachingEnabled = mbConfigs.cachingEnabled === "true";
|
|
16
|
+
const RABBITMQ_DEPLOYMENT_ENV = process.env.RABBITMQ_DEPLOYMENT_ENV;
|
|
16
17
|
RabbitMQHelper.connect = rabbitmq;
|
|
17
18
|
RabbitMQHelper.initializePubSub = async () => {
|
|
18
19
|
try {
|
|
19
20
|
const rabbitmqChannel = await rabbitmq();
|
|
20
21
|
const exchangeType = "direct";
|
|
21
22
|
for (const mq of mqConfigs) {
|
|
22
|
-
|
|
23
|
+
let exchangeName = mq.exchangeName;
|
|
24
|
+
if (RABBITMQ_DEPLOYMENT_ENV) {
|
|
25
|
+
exchangeName = `${mq.exchangeName}.${RABBITMQ_DEPLOYMENT_ENV}`;
|
|
26
|
+
}
|
|
27
|
+
await rabbitmqChannel.assertExchange(exchangeName, exchangeType, {
|
|
23
28
|
durable: true
|
|
24
29
|
});
|
|
25
|
-
await exchangeLogger(
|
|
30
|
+
await exchangeLogger(exchangeName);
|
|
26
31
|
for (const queueConfig of mq.queueConfigList) {
|
|
27
|
-
await RabbitMQHelper.initializeAndBindQueue(rabbitmqChannel,
|
|
32
|
+
await RabbitMQHelper.initializeAndBindQueue(rabbitmqChannel, exchangeName, queueConfig);
|
|
28
33
|
}
|
|
29
34
|
}
|
|
30
35
|
} catch (error) {
|
|
@@ -41,18 +46,26 @@ RabbitMQHelper.initializeAndBindQueue = async (rabbitmqChannel, exchangeName, qu
|
|
|
41
46
|
priorityQueue,
|
|
42
47
|
maxPriority
|
|
43
48
|
} = queueConfig;
|
|
44
|
-
|
|
49
|
+
let queue_name = queueName;
|
|
50
|
+
if (RABBITMQ_DEPLOYMENT_ENV) {
|
|
51
|
+
queue_name = `${queueName}.${RABBITMQ_DEPLOYMENT_ENV}`;
|
|
52
|
+
}
|
|
53
|
+
await rabbitmqChannel.assertQueue(queue_name, {
|
|
45
54
|
durable: true,
|
|
46
55
|
...(priorityQueue && {
|
|
47
56
|
arguments: {
|
|
48
|
-
|
|
57
|
+
"x-max-priority": maxPriority
|
|
49
58
|
}
|
|
50
59
|
})
|
|
51
60
|
});
|
|
52
|
-
await queueLogger(`${
|
|
61
|
+
await queueLogger(`${queue_name} queue created!`);
|
|
53
62
|
for (const routingKey of routingKeyList) {
|
|
54
|
-
|
|
55
|
-
|
|
63
|
+
let routing_key = routingKey;
|
|
64
|
+
if (RABBITMQ_DEPLOYMENT_ENV) {
|
|
65
|
+
routing_key = `${routingKey}-${RABBITMQ_DEPLOYMENT_ENV}`;
|
|
66
|
+
}
|
|
67
|
+
rabbitmqChannel.bindQueue(queue_name, exchangeName, routing_key);
|
|
68
|
+
await bindingLogger([`${exchangeName}`, `${queue_name} queue with routing key ${routing_key}`]);
|
|
56
69
|
}
|
|
57
70
|
} catch (error) {
|
|
58
71
|
console.error(error);
|
|
@@ -73,17 +86,23 @@ RabbitMQHelper.publish = async (exchangeName, routingKey, message, priority = 0,
|
|
|
73
86
|
const rabbitmqChannel = await rabbitmq();
|
|
74
87
|
const parsedMessage = typeof message === "object" ? JSON.stringify(message) : message;
|
|
75
88
|
const cacheGeneratedKey = cacheKey || randomUUID();
|
|
89
|
+
let exchange_name = exchangeName;
|
|
90
|
+
let routing_key = routingKey;
|
|
91
|
+
if (RABBITMQ_DEPLOYMENT_ENV) {
|
|
92
|
+
exchange_name = `${exchangeName}.${RABBITMQ_DEPLOYMENT_ENV}`;
|
|
93
|
+
routing_key = `${routingKey}-${RABBITMQ_DEPLOYMENT_ENV}`;
|
|
94
|
+
}
|
|
76
95
|
if (CachingEnabled) {
|
|
77
96
|
if (cacheKey) {} else {
|
|
78
97
|
const setCacheResponse = await CacheService.setCache(cacheGeneratedKey, {
|
|
79
|
-
|
|
80
|
-
|
|
98
|
+
exchange_name,
|
|
99
|
+
routing_key,
|
|
81
100
|
priority,
|
|
82
101
|
data: message
|
|
83
102
|
});
|
|
84
103
|
}
|
|
85
104
|
}
|
|
86
|
-
const publishResponse = await rabbitmqChannel.publish(
|
|
105
|
+
const publishResponse = await rabbitmqChannel.publish(exchange_name, routing_key, Buffer.from(parsedMessage), {
|
|
87
106
|
persistent: true,
|
|
88
107
|
mandatory: true,
|
|
89
108
|
...(priority && {
|
|
@@ -91,29 +110,35 @@ RabbitMQHelper.publish = async (exchangeName, routingKey, message, priority = 0,
|
|
|
91
110
|
})
|
|
92
111
|
});
|
|
93
112
|
if (!publishResponse) {
|
|
94
|
-
const message = `Error while publishing to the ${
|
|
113
|
+
const message = `Error while publishing to the ${exchange_name} with the routing key ${routing_key}`;
|
|
95
114
|
console.log(message);
|
|
96
|
-
offlinePubQueue.push([
|
|
115
|
+
offlinePubQueue.push([exchange_name, routing_key, message]);
|
|
97
116
|
return {
|
|
98
117
|
error: true,
|
|
99
118
|
message: message,
|
|
100
119
|
data: null
|
|
101
120
|
};
|
|
102
121
|
} else {
|
|
103
|
-
console.log(" [amqp] Sent %s to '%s'",
|
|
122
|
+
console.log(" [amqp] Sent %s to '%s'", routing_key, exchange_name);
|
|
104
123
|
if (CachingEnabled) {
|
|
105
124
|
const clearCacheByKeyResponse = await CacheService.clearCacheByKey(cacheGeneratedKey);
|
|
106
125
|
}
|
|
107
126
|
return {
|
|
108
127
|
error: false,
|
|
109
|
-
message: `Message Published Successfully into the ${
|
|
128
|
+
message: `Message Published Successfully into the ${exchange_name}`,
|
|
110
129
|
data: null
|
|
111
130
|
};
|
|
112
131
|
}
|
|
113
132
|
} catch (error) {
|
|
114
|
-
|
|
133
|
+
let exchange_name = exchangeName;
|
|
134
|
+
let routing_key = routingKey;
|
|
135
|
+
if (RABBITMQ_DEPLOYMENT_ENV) {
|
|
136
|
+
exchange_name = `${exchangeName}.${RABBITMQ_DEPLOYMENT_ENV}`;
|
|
137
|
+
routing_key = `${routingKey}-${RABBITMQ_DEPLOYMENT_ENV}`;
|
|
138
|
+
}
|
|
139
|
+
const message = `Error while publishing to the ${exchange_name} with the routing key ${routing_key}`;
|
|
115
140
|
console.log(message, error.message);
|
|
116
|
-
offlinePubQueue.push([
|
|
141
|
+
offlinePubQueue.push([exchange_name, routing_key, message]);
|
|
117
142
|
return {
|
|
118
143
|
error: true,
|
|
119
144
|
message: message,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "message-brokers",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "This package contains functions to publish the messages into the message brokers like rabbitmq.",
|
|
5
5
|
"main": "lib/message-brokers.js",
|
|
6
6
|
"types": "lib/types/message-brokers.d.ts",
|