message-brokers 1.3.1 → 1.4.1

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
@@ -15,18 +15,7 @@ const mqConfigs = [{
15
15
  }, {
16
16
  queueName: "raw-queue-processor",
17
17
  routingKeyList: ["raw_queue"],
18
- priorityQueue: true,
19
- maxPriority: 5
20
- }, {
21
- queueName: "har-stub-processor",
22
- routingKeyList: ["har_stub_to_report_gen"],
23
- priorityQueue: true,
24
- maxPriority: 5
25
- }, {
26
- queueName: "report-gen-response-processor",
27
- routingKeyList: ["rg_response"],
28
- priorityQueue: true,
29
- maxPriority: 5
18
+ priorityQueue: false
30
19
  }
31
20
  // Example for priority queue.
32
21
  // {
@@ -110,28 +110,8 @@ class MessageBrokers {
110
110
  * @param {Object} raw_queue
111
111
  * @returns
112
112
  */
113
- async publishRawQueue(raw_queue, priority) {
114
- const response = await AmqpController.publishMessage(hv_exchange, "raw_queue", raw_queue, priority);
115
- return response;
116
- }
117
-
118
- /**
119
- * This function is used to publish request that parser sends to report_gen into the exchange.
120
- * @param {Object} har_stub_to_report_gen
121
- * @returns
122
- */
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
- return response;
126
- }
127
-
128
- /**
129
- * This function is used to publish report_gen's response to parser's request into the exchange.
130
- * @param {Object} rg_response
131
- * @returns
132
- */
133
- async publishReportgenResponse(rg_response, priority) {
134
- const response = await AmqpController.publishMessage(hv_exchange, "rg_response", rg_response, priority);
113
+ async publishRawQueue(raw_queue) {
114
+ const response = await AmqpController.publishMessage(hv_exchange, "raw_queue", raw_queue);
135
115
  return response;
136
116
  }
137
117
  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,18 +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
56
  arguments: {
48
- 'x-max-priority': maxPriority
57
+ "x-max-priority": maxPriority
49
58
  }
50
59
  })
51
60
  });
52
- await queueLogger(`${queueName} queue created!`);
61
+ await queueLogger(`${queue_name} queue created!`);
53
62
  for (const routingKey of routingKeyList) {
54
- rabbitmqChannel.bindQueue(queueName, exchangeName, routingKey);
55
- 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}`]);
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
- exchangeName,
80
- routingKey,
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(exchangeName, routingKey, Buffer.from(parsedMessage), {
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 ${exchangeName} with the routing key ${routingKey}`;
113
+ const message = `Error while publishing to the ${exchange_name} with the routing key ${routing_key}`;
95
114
  console.log(message);
96
- offlinePubQueue.push([exchangeName, routingKey, message]);
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'", routingKey, exchangeName);
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 ${exchangeName}`,
128
+ message: `Message Published Successfully into the ${exchange_name}`,
110
129
  data: null
111
130
  };
112
131
  }
113
132
  } catch (error) {
114
- 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}`;
115
140
  console.log(message, error.message);
116
- offlinePubQueue.push([exchangeName, routingKey, message]);
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.1",
3
+ "version": "1.4.1",
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",