message-brokers 1.3.0 → 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 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
@@ -91,12 +92,13 @@ const client = new MongoClient(mongoUrl, {
91
92
 
92
93
  const mongoConnectionHolder = { connection: null };
93
94
 
94
- client.connect()
95
+ client
96
+ .connect()
95
97
  .then(() => {
96
98
  console.log("✅ MongoDB connected!");
97
99
  mongoConnectionHolder.connection = client.db(dbName);
98
100
  })
99
- .catch(error => console.error("❌ MongoDB connection failed:", error));
101
+ .catch((error) => console.error("❌ MongoDB connection failed:", error));
100
102
 
101
103
  const messageBrokers = new MessageBrokers(mongoConnectionHolder);
102
104
  ```
@@ -127,4 +129,10 @@ messageBrokers.publishHarStub(har_stub);
127
129
 
128
130
  // Publish raw_queue
129
131
  messageBrokers.publishRawQueue(raw_queue);
132
+
133
+ // Publish har_stub_with_route_params
134
+ messageBrokers.publishHarStubWithRouteParams(har_stub_to_report_gen);
135
+
136
+ // Publish reportgen_response
137
+ messageBrokers.publishReportgenResponse(rg_response);
130
138
  ```
@@ -15,15 +15,18 @@ const mqConfigs = [{
15
15
  }, {
16
16
  queueName: "raw-queue-processor",
17
17
  routingKeyList: ["raw_queue"],
18
- priorityQueue: false
18
+ priorityQueue: true,
19
+ maxPriority: 5
19
20
  }, {
20
21
  queueName: "har-stub-processor",
21
22
  routingKeyList: ["har_stub_to_report_gen"],
22
- priorityQueue: false
23
+ priorityQueue: true,
24
+ maxPriority: 5
23
25
  }, {
24
26
  queueName: "report-gen-response-processor",
25
27
  routingKeyList: ["rg_response"],
26
- priorityQueue: false
28
+ priorityQueue: true,
29
+ maxPriority: 5
27
30
  }
28
31
  // Example for priority queue.
29
32
  // {
@@ -110,8 +110,8 @@ class MessageBrokers {
110
110
  * @param {Object} raw_queue
111
111
  * @returns
112
112
  */
113
- async publishRawQueue(raw_queue) {
114
- const response = await AmqpController.publishMessage(hv_exchange, "raw_queue", raw_queue);
113
+ async publishRawQueue(raw_queue, priority) {
114
+ const response = await AmqpController.publishMessage(hv_exchange, "raw_queue", raw_queue, priority);
115
115
  return response;
116
116
  }
117
117
 
@@ -120,8 +120,8 @@ class MessageBrokers {
120
120
  * @param {Object} har_stub_to_report_gen
121
121
  * @returns
122
122
  */
123
- async publishHarStubWithRouteParams(har_stub_to_report_gen) {
124
- const response = await AmqpController.publishMessage(hv_exchange, "har_stub_to_report_gen", har_stub_to_report_gen);
123
+ async publishHarStubWithRouteParams(har_stub_to_report_gen, priority) {
124
+ const response = await AmqpController.publishMessage(hv_exchange, "har_stub_to_report_gen", har_stub_to_report_gen, priority);
125
125
  return response;
126
126
  }
127
127
 
@@ -130,8 +130,8 @@ class MessageBrokers {
130
130
  * @param {Object} rg_response
131
131
  * @returns
132
132
  */
133
- async publishReportgenResponse(rg_response) {
134
- const response = await AmqpController.publishMessage(hv_exchange, "rg_response", rg_response);
133
+ async publishReportgenResponse(rg_response, priority) {
134
+ const response = await AmqpController.publishMessage(hv_exchange, "rg_response", rg_response, priority);
135
135
  return response;
136
136
  }
137
137
  async publishPendingMessageInCache() {
@@ -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
- await rabbitmqChannel.assertExchange(mq.exchangeName, exchangeType, {
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(mq.exchangeName);
30
+ await exchangeLogger(exchangeName);
26
31
  for (const queueConfig of mq.queueConfigList) {
27
- await RabbitMQHelper.initializeAndBindQueue(rabbitmqChannel, mq.exchangeName, queueConfig);
32
+ await RabbitMQHelper.initializeAndBindQueue(rabbitmqChannel, exchangeName, queueConfig);
28
33
  }
29
34
  }
30
35
  } catch (error) {
@@ -41,16 +46,26 @@ RabbitMQHelper.initializeAndBindQueue = async (rabbitmqChannel, exchangeName, qu
41
46
  priorityQueue,
42
47
  maxPriority
43
48
  } = queueConfig;
44
- await rabbitmqChannel.assertQueue(queueName, {
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
- maxPriority
56
+ arguments: {
57
+ "x-max-priority": maxPriority
58
+ }
48
59
  })
49
60
  });
50
- await queueLogger(`${queueName} queue created!`);
61
+ await queueLogger(`${queue_name} queue created!`);
51
62
  for (const routingKey of routingKeyList) {
52
- rabbitmqChannel.bindQueue(queueName, exchangeName, routingKey);
53
- await bindingLogger([`${exchangeName}`, `${queueName} queue with routing key ${routingKey}`]);
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}`]);
54
69
  }
55
70
  } catch (error) {
56
71
  console.error(error);
@@ -71,17 +86,23 @@ RabbitMQHelper.publish = async (exchangeName, routingKey, message, priority = 0,
71
86
  const rabbitmqChannel = await rabbitmq();
72
87
  const parsedMessage = typeof message === "object" ? JSON.stringify(message) : message;
73
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
+ }
74
95
  if (CachingEnabled) {
75
96
  if (cacheKey) {} else {
76
97
  const setCacheResponse = await CacheService.setCache(cacheGeneratedKey, {
77
- exchangeName,
78
- routingKey,
98
+ exchange_name,
99
+ routing_key,
79
100
  priority,
80
101
  data: message
81
102
  });
82
103
  }
83
104
  }
84
- const publishResponse = await rabbitmqChannel.publish(exchangeName, routingKey, Buffer.from(parsedMessage), {
105
+ const publishResponse = await rabbitmqChannel.publish(exchange_name, routing_key, Buffer.from(parsedMessage), {
85
106
  persistent: true,
86
107
  mandatory: true,
87
108
  ...(priority && {
@@ -89,29 +110,35 @@ RabbitMQHelper.publish = async (exchangeName, routingKey, message, priority = 0,
89
110
  })
90
111
  });
91
112
  if (!publishResponse) {
92
- const message = `Error while publishing to the ${exchangeName} with the routing key ${routingKey}`;
113
+ const message = `Error while publishing to the ${exchange_name} with the routing key ${routing_key}`;
93
114
  console.log(message);
94
- offlinePubQueue.push([exchangeName, routingKey, message]);
115
+ offlinePubQueue.push([exchange_name, routing_key, message]);
95
116
  return {
96
117
  error: true,
97
118
  message: message,
98
119
  data: null
99
120
  };
100
121
  } else {
101
- console.log(" [amqp] Sent %s to '%s'", routingKey, exchangeName);
122
+ console.log(" [amqp] Sent %s to '%s'", routing_key, exchange_name);
102
123
  if (CachingEnabled) {
103
124
  const clearCacheByKeyResponse = await CacheService.clearCacheByKey(cacheGeneratedKey);
104
125
  }
105
126
  return {
106
127
  error: false,
107
- message: `Message Published Successfully into the ${exchangeName}`,
128
+ message: `Message Published Successfully into the ${exchange_name}`,
108
129
  data: null
109
130
  };
110
131
  }
111
132
  } catch (error) {
112
- const message = `Error while publishing to the ${exchangeName} with the routing key ${routingKey}`;
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}`;
113
140
  console.log(message, error.message);
114
- offlinePubQueue.push([exchangeName, routingKey, message]);
141
+ offlinePubQueue.push([exchange_name, routing_key, message]);
115
142
  return {
116
143
  error: true,
117
144
  message: message,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "message-brokers",
3
- "version": "1.3.0",
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",
@@ -13,6 +13,7 @@
13
13
  "scripts": {
14
14
  "install_deps": "NODE_ENV='development' yarn",
15
15
  "build": "babel ./src -d ./lib && cp -r ./src/types/message-brokers.d.ts ./lib/types",
16
+ "watch": "babel ./src -d ./lib --watch & cp -r ./src/types/message-brokers.d.ts ./lib/types",
16
17
  "prepublishOnly": "npm run build"
17
18
  },
18
19
  "repository": "https://code.360hvpl.com/development/message-brokers.git",