message-brokers 1.9.0 → 1.10.0-beta
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/lib/configs/centralMqConfigs.js +17 -0
- package/lib/configs/mb.configs.js +1 -0
- package/lib/controllers/amqp.controller.js +109 -0
- package/lib/helpers/centralRabbitmq/centralRabbitmq.helper.js +156 -0
- package/lib/helpers/centralRabbitmq/centralRabbitmqConnection.js +38 -0
- package/lib/message-brokers.js +21 -0
- package/lib/package-loader.js +11 -0
- package/lib/rabbitmq/rabbitmq.helper.js +6 -4
- package/lib/services/deadqueue.service.js +6 -6
- package/lib/types +43 -0
- package/lib/utils/log.util.js +6 -6
- package/lib/utils/maskPid.js +147 -0
- package/package.json +1 -1
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const centralMqConfigs = [{
|
|
2
|
+
exchangeName: "central.hv.exchange",
|
|
3
|
+
queueConfigList: [{
|
|
4
|
+
queueName: "central.support.queue",
|
|
5
|
+
routingKeyList: ["purchase_details"],
|
|
6
|
+
priorityQueue: false
|
|
7
|
+
}
|
|
8
|
+
// Example for priority queue.
|
|
9
|
+
// {
|
|
10
|
+
// queueName: "lab_api.har.queue",
|
|
11
|
+
// routingKeyList: ["har", "lab_api_response"],
|
|
12
|
+
// priorityQueue: true,
|
|
13
|
+
// maxPriority: 2,
|
|
14
|
+
// },
|
|
15
|
+
]
|
|
16
|
+
}];
|
|
17
|
+
module.exports = centralMqConfigs;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const mbConfigs = {
|
|
2
2
|
rabbitmqUrl: process.env.RABBITMQ_URL,
|
|
3
|
+
centralRabbitmqUrl: process.env.CENTRAL_RABBITMQ_URL,
|
|
3
4
|
activeMessageBroker: process.env.ACTIVE_MESSAGE_BROKER || "rabbitmq",
|
|
4
5
|
redisPort: process.env.REDIS_PORT || 6260,
|
|
5
6
|
cachingEnabled: process.env.CACHING_ENABLED || false,
|
|
@@ -2,6 +2,8 @@ const MessageBroker = require("../helpers/message-brokers.helper");
|
|
|
2
2
|
const CacheService = require("../services/cache.service");
|
|
3
3
|
const DeadQueueService = require("../services/deadqueue.service");
|
|
4
4
|
const mbConfigs = require("../configs/mb.configs");
|
|
5
|
+
const maskPid = require("../utils/maskPid");
|
|
6
|
+
const CentralRabbitMQHelper = require("../helpers/centralRabbitmq/centralRabbitmq.helper");
|
|
5
7
|
const AmqpController = {};
|
|
6
8
|
|
|
7
9
|
/**
|
|
@@ -125,4 +127,111 @@ AmqpController.publishPendingMessageInCache = async () => {
|
|
|
125
127
|
};
|
|
126
128
|
}
|
|
127
129
|
};
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Publishes a message to the specified exchange with a given routing key and priority (pushes to central rabbitmq).
|
|
133
|
+
*
|
|
134
|
+
* This function interacts with the CentralRabbitMQHelper instance to send messages
|
|
135
|
+
* to a message broker (e.g., RabbitMQ). It returns a structured response indicating
|
|
136
|
+
* whether the message was successfully published or if an error occurred.
|
|
137
|
+
*
|
|
138
|
+
* @param {string} exchange - The name of the exchange to publish the message to.
|
|
139
|
+
* @param {string} routingKey - The routing key used for message routing.
|
|
140
|
+
* @param {Object|string} message - The message payload to be published. Can be an object or a string.
|
|
141
|
+
* @param {number} [priority=1] - (Optional) The priority level of the message. Defaults to 1.
|
|
142
|
+
*
|
|
143
|
+
* @returns {Promise<Object>} - A promise that resolves to an object containing:
|
|
144
|
+
* - `success` (boolean): Indicates if the message was successfully published.
|
|
145
|
+
* - `message` (string): A descriptive message about the operation's outcome.
|
|
146
|
+
* - `data` (any): Additional data related to the operation or error details if the operation failed.
|
|
147
|
+
*
|
|
148
|
+
*/
|
|
149
|
+
AmqpController.publishCentralMQMessage = async (exchange, routingKey, message, priority = 1) => {
|
|
150
|
+
if (process.env.DISABLE_CENTRAL_RABBITMQ) {
|
|
151
|
+
return {
|
|
152
|
+
success: false,
|
|
153
|
+
message: "CENTRAL_RABBITMQ is disabled.",
|
|
154
|
+
data: null
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
let publishMessage = message;
|
|
158
|
+
if (!mbConfigs.centralRabbitmqUrl) {
|
|
159
|
+
await DeadQueueService.storeFailureMessage({
|
|
160
|
+
exchange,
|
|
161
|
+
routingKey,
|
|
162
|
+
message,
|
|
163
|
+
priority,
|
|
164
|
+
failureMessage: "No central rabbitmq url found."
|
|
165
|
+
}, "central_deadqueue", "Central-MQ");
|
|
166
|
+
return {
|
|
167
|
+
success: false,
|
|
168
|
+
message: "CENTRAL_RABBITMQ_URL is required for central message brokers initialization.",
|
|
169
|
+
data: null
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
const rawMessage = JSON.parse(JSON.stringify(message));
|
|
173
|
+
const result = await maskPid(rawMessage, routingKey);
|
|
174
|
+
if (result.success) {
|
|
175
|
+
publishMessage = result.message;
|
|
176
|
+
} else {
|
|
177
|
+
await DeadQueueService.storeFailureMessage({
|
|
178
|
+
exchange,
|
|
179
|
+
routingKey,
|
|
180
|
+
message,
|
|
181
|
+
priority,
|
|
182
|
+
failureMessage: "Error while masking pid.",
|
|
183
|
+
errorObject: result.error
|
|
184
|
+
}, "central_deadqueue_masking", "Central-MQ");
|
|
185
|
+
return {
|
|
186
|
+
success: false,
|
|
187
|
+
message: "Error while masking pid.",
|
|
188
|
+
data: error
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
try {
|
|
192
|
+
const {
|
|
193
|
+
error,
|
|
194
|
+
message: msg,
|
|
195
|
+
data
|
|
196
|
+
} = await CentralRabbitMQHelper.publish(exchange, routingKey, publishMessage, priority);
|
|
197
|
+
if (!error) {
|
|
198
|
+
console.log(`INFO: [${new Date().toLocaleString()}] [Central-MQ]: ${routingKey} successfully published into the exchange.`);
|
|
199
|
+
return {
|
|
200
|
+
success: true,
|
|
201
|
+
message: msg,
|
|
202
|
+
data
|
|
203
|
+
};
|
|
204
|
+
} else {
|
|
205
|
+
console.log(`ERROR: [${new Date().toLocaleString()}] [Central-MQ]: Error while publishing ${routingKey} into the exchange.`);
|
|
206
|
+
await DeadQueueService.storeFailureMessage({
|
|
207
|
+
exchange,
|
|
208
|
+
routingKey,
|
|
209
|
+
message: publishMessage,
|
|
210
|
+
priority,
|
|
211
|
+
failureMessage: "Error while publishing into central rabbitmq.",
|
|
212
|
+
errorObject: error
|
|
213
|
+
}, "central_deadqueue", "Central-MQ");
|
|
214
|
+
return {
|
|
215
|
+
success: false,
|
|
216
|
+
message: msg,
|
|
217
|
+
data: `${error}`
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
} catch (error) {
|
|
221
|
+
console.log(`ERROR: [${new Date().toLocaleString()}] [Central-MQ]: Error while publishing ${routingKey} into the exchange.`);
|
|
222
|
+
await DeadQueueService.storeFailureMessage({
|
|
223
|
+
exchange,
|
|
224
|
+
routingKey,
|
|
225
|
+
message,
|
|
226
|
+
priority,
|
|
227
|
+
failureMessage: "Unexpected error while publishing into central rabbitmq.",
|
|
228
|
+
errorObject: error
|
|
229
|
+
}, "central_deadqueue", "Central-MQ");
|
|
230
|
+
return {
|
|
231
|
+
success: false,
|
|
232
|
+
message: `Error publishing into central rabbitmq for ${routingKey}`,
|
|
233
|
+
data: error
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
};
|
|
128
237
|
module.exports = AmqpController;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
const {
|
|
2
|
+
randomUUID
|
|
3
|
+
} = require("crypto");
|
|
4
|
+
const centralRabbitmq = require("./centralRabbitmqConnection");
|
|
5
|
+
const {
|
|
6
|
+
exchangeLogger,
|
|
7
|
+
queueLogger,
|
|
8
|
+
bindingLogger
|
|
9
|
+
} = require("../../utils/log.util");
|
|
10
|
+
const centralMqConfigs = require("../../configs/centralMqConfigs");
|
|
11
|
+
const mbConfigs = require("../../configs/mb.configs");
|
|
12
|
+
const CacheService = require("../../services/cache.service");
|
|
13
|
+
const {
|
|
14
|
+
applyQueueName,
|
|
15
|
+
applyRoutingKey,
|
|
16
|
+
applyExchangeName,
|
|
17
|
+
applyRoutingKeyIsolationForPublish
|
|
18
|
+
} = require("../../utils/rabbitmq.utils");
|
|
19
|
+
|
|
20
|
+
// const offlinePubQueue = [];
|
|
21
|
+
|
|
22
|
+
const CentralRabbitMQHelper = {};
|
|
23
|
+
const CachingEnabled = mbConfigs.cachingEnabled === "true";
|
|
24
|
+
CentralRabbitMQHelper.connect = centralRabbitmq;
|
|
25
|
+
CentralRabbitMQHelper.initializePubSub = async () => {
|
|
26
|
+
try {
|
|
27
|
+
const rabbitmqChannel = await centralRabbitmq();
|
|
28
|
+
const exchangeType = "direct";
|
|
29
|
+
for (const mq of centralMqConfigs) {
|
|
30
|
+
let exchangeName = applyExchangeName(mq.exchangeName);
|
|
31
|
+
await rabbitmqChannel.assertExchange(exchangeName, exchangeType, {
|
|
32
|
+
durable: true
|
|
33
|
+
});
|
|
34
|
+
await exchangeLogger(exchangeName, "central-amqp");
|
|
35
|
+
for (const queueConfig of mq.queueConfigList) {
|
|
36
|
+
await CentralRabbitMQHelper.initializeAndBindQueue(rabbitmqChannel, exchangeName, queueConfig);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
} catch (error) {
|
|
40
|
+
console.log("[central-amqp] Error in initializing the rabbitmq exchanges and queues");
|
|
41
|
+
console.error(error);
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
CentralRabbitMQHelper.initializeAndBindQueue = async (rabbitmqChannel, exchangeName, queueConfig) => {
|
|
46
|
+
try {
|
|
47
|
+
const {
|
|
48
|
+
queueName,
|
|
49
|
+
routingKeyList,
|
|
50
|
+
priorityQueue,
|
|
51
|
+
maxPriority
|
|
52
|
+
} = queueConfig;
|
|
53
|
+
let queue_name = applyQueueName(queueName);
|
|
54
|
+
await rabbitmqChannel.assertQueue(queue_name, {
|
|
55
|
+
durable: true,
|
|
56
|
+
...(priorityQueue && {
|
|
57
|
+
arguments: {
|
|
58
|
+
"x-max-priority": maxPriority
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
});
|
|
62
|
+
await queueLogger(`${queue_name} queue created!`, "central-amqp");
|
|
63
|
+
for (const routingKey of routingKeyList) {
|
|
64
|
+
let routing_key = applyRoutingKey(queueName, routingKey);
|
|
65
|
+
rabbitmqChannel.bindQueue(queue_name, exchangeName, routing_key);
|
|
66
|
+
await bindingLogger([`${exchangeName}`, `${queue_name} queue with routing key ${routing_key}`], "central-amqp");
|
|
67
|
+
}
|
|
68
|
+
} catch (error) {
|
|
69
|
+
console.log(`[central-amqp] Error while initializing and binding queue`);
|
|
70
|
+
console.error(error);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* This function is used publish into the exchange.
|
|
76
|
+
* @param exchangeName
|
|
77
|
+
* @param routingKey
|
|
78
|
+
* @param message
|
|
79
|
+
* @param priority (Optional)
|
|
80
|
+
* @param cacheKey (Optional)
|
|
81
|
+
* @returns
|
|
82
|
+
*/
|
|
83
|
+
CentralRabbitMQHelper.publish = async (exchangeName, routingKey, message, priority = 0, cacheKey = null) => {
|
|
84
|
+
try {
|
|
85
|
+
const rabbitmqChannel = await centralRabbitmq();
|
|
86
|
+
const parsedMessage = typeof message === "object" ? JSON.stringify(message) : message;
|
|
87
|
+
const cacheGeneratedKey = cacheKey || randomUUID();
|
|
88
|
+
let exchange_name = applyExchangeName(exchangeName);
|
|
89
|
+
let routing_key = applyRoutingKeyIsolationForPublish(routingKey);
|
|
90
|
+
if (CachingEnabled) {
|
|
91
|
+
if (cacheKey) {} else {
|
|
92
|
+
const setCacheResponse = await CacheService.setCache(cacheGeneratedKey, {
|
|
93
|
+
exchange_name,
|
|
94
|
+
routing_key,
|
|
95
|
+
priority,
|
|
96
|
+
data: message
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const publishResponse = await rabbitmqChannel.publish(exchange_name, routing_key, Buffer.from(parsedMessage), {
|
|
101
|
+
persistent: true,
|
|
102
|
+
mandatory: true,
|
|
103
|
+
...(priority && {
|
|
104
|
+
priority
|
|
105
|
+
})
|
|
106
|
+
});
|
|
107
|
+
if (!publishResponse) {
|
|
108
|
+
const message = `[central-amqp] Error while publishing to the ${exchange_name} with the routing key ${routing_key}`;
|
|
109
|
+
console.log(message);
|
|
110
|
+
// offlinePubQueue.push([exchange_name, routing_key, message]);
|
|
111
|
+
return {
|
|
112
|
+
error: true,
|
|
113
|
+
message: message,
|
|
114
|
+
data: null
|
|
115
|
+
};
|
|
116
|
+
} else {
|
|
117
|
+
console.log("[central-amqp] Sent %s to '%s'", routing_key, exchange_name);
|
|
118
|
+
if (CachingEnabled) {
|
|
119
|
+
const clearCacheByKeyResponse = await CacheService.clearCacheByKey(cacheGeneratedKey);
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
error: false,
|
|
123
|
+
message: `[central-amqp] Message Published Successfully into the ${exchange_name}`,
|
|
124
|
+
data: null
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
} catch (error) {
|
|
128
|
+
let exchange_name = applyExchangeName(exchangeName);
|
|
129
|
+
let routing_key = applyRoutingKeyIsolationForPublish(routingKey);
|
|
130
|
+
const message = `[central-amqp] Error while publishing to the ${exchange_name} with the routing key ${routing_key}`;
|
|
131
|
+
console.log(message, error.message);
|
|
132
|
+
// offlinePubQueue.push([exchange_name, routing_key, message]);
|
|
133
|
+
return {
|
|
134
|
+
error: true,
|
|
135
|
+
message: message,
|
|
136
|
+
data: null
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* This function is used to delete queue.
|
|
143
|
+
* @param {String} queueName
|
|
144
|
+
* @returns
|
|
145
|
+
*/
|
|
146
|
+
CentralRabbitMQHelper.deleteQueue = async queueName => {
|
|
147
|
+
try {
|
|
148
|
+
const rabbitmqChannel = await centralRabbitmq();
|
|
149
|
+
await rabbitmqChannel.deleteQueue(queueName);
|
|
150
|
+
console.log(`[central-amqp] Queue ${queueName} deleted successfully`);
|
|
151
|
+
} catch (error) {
|
|
152
|
+
console.log(`[central-amqp] Error while deleting queue ${queueName}`);
|
|
153
|
+
console.error(error);
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
module.exports = CentralRabbitMQHelper;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const amqp = require("amqplib");
|
|
2
|
+
const {
|
|
3
|
+
colorLogger
|
|
4
|
+
} = require("../../utils/log.util");
|
|
5
|
+
const {
|
|
6
|
+
timeout
|
|
7
|
+
} = require("../../utils/func.util");
|
|
8
|
+
const {
|
|
9
|
+
amqpReconnectTimeout
|
|
10
|
+
} = require("../../configs/main.configs");
|
|
11
|
+
const mbConfigs = require("../../configs/mb.configs");
|
|
12
|
+
let centralRabbitmqChannel;
|
|
13
|
+
async function centralRabbitmq() {
|
|
14
|
+
if (centralRabbitmqChannel) return centralRabbitmqChannel;
|
|
15
|
+
try {
|
|
16
|
+
connection = await amqp.connect(mbConfigs.centralRabbitmqUrl + "?heartbeat=60");
|
|
17
|
+
centralRabbitmqChannel = await connection.createConfirmChannel();
|
|
18
|
+
connection.on("error", err => {
|
|
19
|
+
if (err.message !== "Connection closing") {
|
|
20
|
+
console.error("[central-amqp] central rabbitmq conn error", err.message);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
connection.on("close", async () => {
|
|
24
|
+
console.error("[central-amqp] central rabbitmq reconnecting...");
|
|
25
|
+
centralRabbitmqChannel = null;
|
|
26
|
+
await timeout(amqpReconnectTimeout);
|
|
27
|
+
return centralRabbitmq();
|
|
28
|
+
});
|
|
29
|
+
await colorLogger("fe6f27", "ffe042", "143d59", `Connected to central rabbitmq successfully!`, "[central-amqp]");
|
|
30
|
+
return centralRabbitmqChannel;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
console.error("[central-amqp] Error connecting to central rabbitmq:", error.message);
|
|
33
|
+
centralRabbitmqChannel = null;
|
|
34
|
+
await timeout(amqpReconnectTimeout);
|
|
35
|
+
return centralRabbitmq();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
module.exports = centralRabbitmq;
|
package/lib/message-brokers.js
CHANGED
|
@@ -6,12 +6,18 @@ const {
|
|
|
6
6
|
setActiveApplication
|
|
7
7
|
} = require("./helpers/app.helper");
|
|
8
8
|
const hv_exchange = "hv.exchange";
|
|
9
|
+
const central_hv_exchange = "central.hv.exchange";
|
|
9
10
|
class MessageBrokers {
|
|
10
11
|
constructor(mongoConnectionHolder, applicationName) {
|
|
11
12
|
this.mongoConnectionHolder = mongoConnectionHolder;
|
|
12
13
|
this.applicationName = applicationName || "mb_app";
|
|
13
14
|
this._initialized = false;
|
|
14
15
|
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Initializes the AMQP connection and MongoDB connection.
|
|
19
|
+
* @returns A promise that resolves to a response object.
|
|
20
|
+
*/
|
|
15
21
|
async init() {
|
|
16
22
|
if (this._initialized) return;
|
|
17
23
|
try {
|
|
@@ -155,6 +161,21 @@ class MessageBrokers {
|
|
|
155
161
|
const response = await AmqpController.publishMessage(hv_exchange, "billing", billing_events);
|
|
156
162
|
return response;
|
|
157
163
|
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* This function is used publish purchase_details into the central exchange.
|
|
167
|
+
* @param {Object} purchase_details
|
|
168
|
+
* @returns
|
|
169
|
+
*/
|
|
170
|
+
async publishCentralPurchaseDetails(purchase_details) {
|
|
171
|
+
const response = await AmqpController.publishCentralMQMessage(central_hv_exchange, "purchase_details", purchase_details);
|
|
172
|
+
return response;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* This function is used publish pending messages from cache into the exchange.
|
|
177
|
+
* @returns
|
|
178
|
+
*/
|
|
158
179
|
async publishPendingMessageInCache() {
|
|
159
180
|
return await AmqpController.publishPendingMessageInCache();
|
|
160
181
|
}
|
package/lib/package-loader.js
CHANGED
|
@@ -4,6 +4,8 @@ const {
|
|
|
4
4
|
const redisDB = require("./helpers/redis.helper");
|
|
5
5
|
const mongoDB = require("./helpers/mongodb.helper");
|
|
6
6
|
const MessageBroker = require("./helpers/message-brokers.helper");
|
|
7
|
+
const CentralRabbitMQHelper = require("./helpers/centralRabbitmq/centralRabbitmq.helper");
|
|
8
|
+
const mbConfigs = require("./configs/mb.configs");
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
11
|
* This function is used to load all the db connections,
|
|
@@ -17,6 +19,15 @@ const package_loader = async (mongoConnectionHolder, rabbitMqEnable = true) => {
|
|
|
17
19
|
await MessageBroker.connect();
|
|
18
20
|
await MessageBroker.initializePubSub();
|
|
19
21
|
}
|
|
22
|
+
if (!process.env.DISABLE_CENTRAL_RABBITMQ) {
|
|
23
|
+
if (!mbConfigs.centralRabbitmqUrl) {
|
|
24
|
+
throw new Error(`[central-amqp] CENTRAL_RABBITMQ_URL is not set for central RabbitMQ initialization!`);
|
|
25
|
+
}
|
|
26
|
+
await CentralRabbitMQHelper.connect();
|
|
27
|
+
await CentralRabbitMQHelper.initializePubSub();
|
|
28
|
+
} else {
|
|
29
|
+
console.log("[central-amqp] Central RabbitMQ is disabled!");
|
|
30
|
+
}
|
|
20
31
|
if (mongoConnectionHolder.connection) {
|
|
21
32
|
await mongoDB(mongoConnectionHolder.connection);
|
|
22
33
|
}
|
|
@@ -15,8 +15,10 @@ const {
|
|
|
15
15
|
applyRoutingKey,
|
|
16
16
|
applyExchangeName,
|
|
17
17
|
applyRoutingKeyIsolationForPublish
|
|
18
|
-
} = require("
|
|
19
|
-
|
|
18
|
+
} = require("../utils/rabbitmq.utils");
|
|
19
|
+
|
|
20
|
+
// const offlinePubQueue = [];
|
|
21
|
+
|
|
20
22
|
const RabbitMQHelper = {};
|
|
21
23
|
const CachingEnabled = mbConfigs.cachingEnabled === "true";
|
|
22
24
|
const RABBITMQ_DEPLOYMENT_ENV = process.env.RABBITMQ_DEPLOYMENT_ENV;
|
|
@@ -112,7 +114,7 @@ RabbitMQHelper.publish = async (exchangeName, routingKey, message, priority = 0,
|
|
|
112
114
|
if (!publishResponse) {
|
|
113
115
|
const message = `Error while publishing to the ${exchange_name} with the routing key ${routing_key}`;
|
|
114
116
|
console.log(message);
|
|
115
|
-
offlinePubQueue.push([exchange_name, routing_key, message]);
|
|
117
|
+
// offlinePubQueue.push([exchange_name, routing_key, message]);
|
|
116
118
|
return {
|
|
117
119
|
error: true,
|
|
118
120
|
message: message,
|
|
@@ -134,7 +136,7 @@ RabbitMQHelper.publish = async (exchangeName, routingKey, message, priority = 0,
|
|
|
134
136
|
let routing_key = applyRoutingKeyIsolationForPublish(routingKey);
|
|
135
137
|
const message = `Error while publishing to the ${exchange_name} with the routing key ${routing_key}`;
|
|
136
138
|
console.log(message, error.message);
|
|
137
|
-
offlinePubQueue.push([exchange_name, routing_key, message]);
|
|
139
|
+
// offlinePubQueue.push([exchange_name, routing_key, message]);
|
|
138
140
|
return {
|
|
139
141
|
error: true,
|
|
140
142
|
message: message,
|
|
@@ -3,10 +3,10 @@ const {
|
|
|
3
3
|
getActiveApplication
|
|
4
4
|
} = require("../helpers/app.helper");
|
|
5
5
|
const DeadQueueService = {};
|
|
6
|
-
DeadQueueService.storeFailureMessage = async failedMessage => {
|
|
6
|
+
DeadQueueService.storeFailureMessage = async (failedMessage, deadqueueCollectionName = "deadqueue", loggingScope = "MQ") => {
|
|
7
7
|
try {
|
|
8
8
|
const db = await mongoDB();
|
|
9
|
-
const collection = await db.collection(
|
|
9
|
+
const collection = await db.collection(deadqueueCollectionName);
|
|
10
10
|
const applicationName = getActiveApplication();
|
|
11
11
|
const new_failed_message = {
|
|
12
12
|
...failedMessage,
|
|
@@ -14,17 +14,17 @@ DeadQueueService.storeFailureMessage = async failedMessage => {
|
|
|
14
14
|
created_at: new Date()
|
|
15
15
|
};
|
|
16
16
|
await collection.insertOne(new_failed_message);
|
|
17
|
-
console.log(`INFO: [${new Date().toLocaleString()}] [
|
|
17
|
+
console.log(`INFO: [${new Date().toLocaleString()}] [${loggingScope}]: ${failedMessage?.routingKey} added to ${deadqueueCollectionName}.`);
|
|
18
18
|
return {
|
|
19
19
|
success: true,
|
|
20
|
-
message:
|
|
20
|
+
message: `Successfully stored failed message in the ${deadqueueCollectionName} collection.`,
|
|
21
21
|
data: null
|
|
22
22
|
};
|
|
23
23
|
} catch (error) {
|
|
24
|
-
console.log(`ERROR: [${new Date().toLocaleString()}] [
|
|
24
|
+
console.log(`ERROR: [${new Date().toLocaleString()}] [${loggingScope}]: Error while saving ${failedMessage?.routingKey} to ${deadqueueCollectionName}.`, error.message);
|
|
25
25
|
return {
|
|
26
26
|
success: false,
|
|
27
|
-
message:
|
|
27
|
+
message: `Error while storing failed message into the ${deadqueueCollectionName} collection.`,
|
|
28
28
|
data: error
|
|
29
29
|
};
|
|
30
30
|
}
|
package/lib/types
CHANGED
|
@@ -39,6 +39,12 @@ declare module "message-brokers" {
|
|
|
39
39
|
config?: MessageBrokersConfig
|
|
40
40
|
);
|
|
41
41
|
|
|
42
|
+
/**
|
|
43
|
+
* Initializes the AMQP connection and MongoDB connection.
|
|
44
|
+
* @returns A promise that resolves to a response object.
|
|
45
|
+
*/
|
|
46
|
+
init(): Promise<void>;
|
|
47
|
+
|
|
42
48
|
/**
|
|
43
49
|
* Publishes a HAR object into the exchange.
|
|
44
50
|
* @param har - The HAR object to be published.
|
|
@@ -102,6 +108,43 @@ declare module "message-brokers" {
|
|
|
102
108
|
*/
|
|
103
109
|
publishTrendingData(trending_data: object): Promise<PublishResponse>;
|
|
104
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Publishes raw queue messages into the exchange.
|
|
113
|
+
* @param raw_queue - The raw queue object to be published.
|
|
114
|
+
* @returns A promise that resolves to a response object.
|
|
115
|
+
*/
|
|
116
|
+
publishRawQueue(raw_queue: object): Promise<PublishResponse>;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Publishes notification messages logs into the exchange.
|
|
120
|
+
* @param notification_messages_logs - The notification messages logs object to be published.
|
|
121
|
+
* @returns A promise that resolves to a response object.
|
|
122
|
+
*/
|
|
123
|
+
publishNotificationMessagesLogs(
|
|
124
|
+
notification_messages_logs: object
|
|
125
|
+
): Promise<PublishResponse>;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Publishes lab API response into the exchange.
|
|
129
|
+
* @param lab_api_response - The lab API response object to be published.
|
|
130
|
+
* @returns A promise that resolves to a response object.
|
|
131
|
+
*/
|
|
132
|
+
publishAmqpLabApiResponse(lab_api_response: object): Promise<PublishResponse>;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Publishes billing events into the exchange.
|
|
136
|
+
* @param billing_events - The billing events object to be published.
|
|
137
|
+
* @returns A promise that resolves to a response object.
|
|
138
|
+
*/
|
|
139
|
+
publishBillingEvents(billing_events: object): Promise<PublishResponse>;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Publishes purchase details into the central rabbitmq exchange.
|
|
143
|
+
* @param purchase_details - The purchase details object to be published.
|
|
144
|
+
* @returns A promise that resolves to a response object.
|
|
145
|
+
*/
|
|
146
|
+
publishCentralPurchaseDetails(purchase_details: object): Promise<PublishResponse>;
|
|
147
|
+
|
|
105
148
|
/**
|
|
106
149
|
* Publishes all pending messages stored in the cache.
|
|
107
150
|
* @returns A promise that resolves to a response object indicating the result of the operation.
|
package/lib/utils/log.util.js
CHANGED
|
@@ -7,26 +7,26 @@ const colorLogger = async (subBackgroundColor = "", mainBackgroundColor = "", ma
|
|
|
7
7
|
console.log(error);
|
|
8
8
|
}
|
|
9
9
|
};
|
|
10
|
-
const exchangeLogger = async exchangeName => {
|
|
10
|
+
const exchangeLogger = async (exchangeName, amqpScope = "amqp") => {
|
|
11
11
|
try {
|
|
12
12
|
const _chalk = await chalk;
|
|
13
|
-
console.log(_chalk.bgHex("5d4331").bold(
|
|
13
|
+
console.log(_chalk.bgHex("5d4331").bold(`[*${amqpScope}*] Exchange creation: ` + _chalk.bgGreen(` ${exchangeName}`)));
|
|
14
14
|
} catch (error) {
|
|
15
15
|
console.log(error);
|
|
16
16
|
}
|
|
17
17
|
};
|
|
18
|
-
const queueLogger = async message => {
|
|
18
|
+
const queueLogger = async (message, amqpScope = "amqp") => {
|
|
19
19
|
try {
|
|
20
20
|
const _chalk = await chalk;
|
|
21
|
-
console.log(_chalk.black.bgWhite.bold(
|
|
21
|
+
console.log(_chalk.black.bgWhite.bold(` [*${amqpScope}*] `) + " " + _chalk.green(_chalk.italic(message)));
|
|
22
22
|
} catch (error) {
|
|
23
23
|
console.log(error);
|
|
24
24
|
}
|
|
25
25
|
};
|
|
26
|
-
const bindingLogger = async text => {
|
|
26
|
+
const bindingLogger = async (text, amqpScope = "amqp") => {
|
|
27
27
|
try {
|
|
28
28
|
const _chalk = await chalk;
|
|
29
|
-
console.log(_chalk.black.bgWhite.bold(
|
|
29
|
+
console.log(_chalk.black.bgWhite.bold(` [*${amqpScope}*] `) + " " + _chalk.yellow(_chalk.italic(text[0]) + " -> " + _chalk.italic(text[1])));
|
|
30
30
|
} catch (error) {
|
|
31
31
|
console.log(error);
|
|
32
32
|
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
const crypto = require("crypto");
|
|
2
|
+
async function hashItem(item) {
|
|
3
|
+
try {
|
|
4
|
+
const hashedItem = await crypto.createHash("sha256").update(item).digest("hex");
|
|
5
|
+
return {
|
|
6
|
+
error: null,
|
|
7
|
+
hashedItem
|
|
8
|
+
};
|
|
9
|
+
} catch (error) {
|
|
10
|
+
console.log("Error while hashing the value:", error.message);
|
|
11
|
+
return {
|
|
12
|
+
error: true,
|
|
13
|
+
hashedItem: null
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @author Akif Mohammed
|
|
20
|
+
* @description Function to mask pid from object.
|
|
21
|
+
* @param {Object} messageObject
|
|
22
|
+
* @returns {Object}
|
|
23
|
+
*/
|
|
24
|
+
async function maskPid(messageObject, message_type) {
|
|
25
|
+
try {
|
|
26
|
+
if (!messageObject || typeof messageObject !== "object") {
|
|
27
|
+
return messageObject;
|
|
28
|
+
}
|
|
29
|
+
switch (message_type) {
|
|
30
|
+
case "purchase_details":
|
|
31
|
+
return await maskPurchaseDetails(messageObject);
|
|
32
|
+
default:
|
|
33
|
+
return {
|
|
34
|
+
success: false,
|
|
35
|
+
error: `Unknown message type: ${message_type}`,
|
|
36
|
+
message: messageObject
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
} catch (error) {
|
|
40
|
+
console.log("Error while scrubbing pid:", error.message);
|
|
41
|
+
return {
|
|
42
|
+
success: false,
|
|
43
|
+
error: `Error while scrubbing pid: ${error.message}`,
|
|
44
|
+
message: messageObject
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @author Akif Mohammed
|
|
51
|
+
* @description Function to mask purchase_details.
|
|
52
|
+
* @param {Object} purchase_details
|
|
53
|
+
* @returns {Object}
|
|
54
|
+
*/
|
|
55
|
+
async function maskPurchaseDetails(purchase_details) {
|
|
56
|
+
try {
|
|
57
|
+
const tenant_id = purchase_details?.tenant_id;
|
|
58
|
+
console.log(`[Central-MQ] [${new Date().toLocaleString()}] Masking purchase_details with _id: ${purchase_details._id}, tenant_id: ${tenant_id}.`);
|
|
59
|
+
const mrn = String(purchase_details?.mrn || "");
|
|
60
|
+
if (mrn !== "") {
|
|
61
|
+
const hashedMrn = await hashItem(mrn || "");
|
|
62
|
+
if (hashedMrn.error) {
|
|
63
|
+
console.log(`Error while hashing mrn for purchase_details: ${purchase_details?._id}`);
|
|
64
|
+
} else {
|
|
65
|
+
purchase_details["mrn"] = hashedMrn.hashedItem;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const visit_no = String(purchase_details?.visit_no || "");
|
|
69
|
+
if (visit_no !== "") {
|
|
70
|
+
const hashedVisitNo = await hashItem(visit_no || "");
|
|
71
|
+
if (hashedVisitNo.error) {
|
|
72
|
+
console.log(`Error while hashing visit_no for purchase_details: ${purchase_details?._id}`);
|
|
73
|
+
} else {
|
|
74
|
+
purchase_details["visit_no"] = hashedVisitNo.hashedItem;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (purchase_details?.client_report_upload_details?.blobURL) {
|
|
78
|
+
purchase_details.client_report_upload_details.blobURL = null;
|
|
79
|
+
}
|
|
80
|
+
if (purchase_details?.meta) {
|
|
81
|
+
purchase_details.meta.doc_url = null;
|
|
82
|
+
}
|
|
83
|
+
if (purchase_details.generated_reports_details) {
|
|
84
|
+
for (const key in purchase_details.generated_reports_details) {
|
|
85
|
+
if (purchase_details.generated_reports_details?.[key]?.["hv_report_url"]) {
|
|
86
|
+
purchase_details.generated_reports_details[key]["hv_report_url"] = null;
|
|
87
|
+
}
|
|
88
|
+
if (purchase_details.generated_reports_details?.[key]?.failure_data?.api?.message?.config) {
|
|
89
|
+
purchase_details.generated_reports_details[key].failure_data.api.message.config = null;
|
|
90
|
+
}
|
|
91
|
+
if (purchase_details.generated_reports_details?.[key]?.failure_data?.api?.err_obj?.config) {
|
|
92
|
+
purchase_details.generated_reports_details[key].failure_data.api.err_obj.config = null;
|
|
93
|
+
}
|
|
94
|
+
if (purchase_details.generated_reports_details?.[key]?.delivery_details) {
|
|
95
|
+
const channels = purchase_details.generated_reports_details[key]?.delivery_details?.channels;
|
|
96
|
+
if (channels?.api?.status === "success") {
|
|
97
|
+
if (channels?.api?.delivery_details?.client_response?.data?.clientCallResponse?.client_response_body) {
|
|
98
|
+
channels.api.delivery_details.client_response.data.clientCallResponse.client_response_body = "";
|
|
99
|
+
}
|
|
100
|
+
} else if (channels?.api?.status === "failed") {
|
|
101
|
+
if (channels?.api?.delivery_details?.client_response?.config) {
|
|
102
|
+
channels.api.delivery_details.client_response.config = null;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const purchaseAdditionalClientResponse = purchase_details?.additional_info?.client_response_meta;
|
|
108
|
+
if (purchaseAdditionalClientResponse) {
|
|
109
|
+
if (purchaseAdditionalClientResponse?.USER_CODE) {
|
|
110
|
+
const hashedUserCode = await hashItem(String(purchaseAdditionalClientResponse.USER_CODE || ""));
|
|
111
|
+
if (hashedUserCode.error) {
|
|
112
|
+
console.log(`Error while hashing user code for purchase_details: ${purchase_details?._id}`);
|
|
113
|
+
} else {
|
|
114
|
+
purchase_details.additional_info.client_response_meta.USER_CODE = hashedUserCode.hashedItem;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (purchaseAdditionalClientResponse?.VISIT_CODE) {
|
|
118
|
+
const hashedVisitCode = await hashItem(String(purchaseAdditionalClientResponse.VISIT_CODE || ""));
|
|
119
|
+
if (hashedVisitCode.error) {
|
|
120
|
+
console.log(`Error while hashing visit code for purchase_details: ${purchase_details?._id}`);
|
|
121
|
+
} else {
|
|
122
|
+
purchase_details.additional_info.client_response_meta.VISIT_CODE = hashedVisitCode.hashedItem;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (purchaseAdditionalClientResponse?.cug_code) {
|
|
126
|
+
const hashedCugCode = await hashItem(String(purchaseAdditionalClientResponse.cug_code || ""));
|
|
127
|
+
if (hashedCugCode.error) {
|
|
128
|
+
console.log(`Error while hashing cug code for purchase_details: ${purchase_details?._id}`);
|
|
129
|
+
} else {
|
|
130
|
+
purchase_details.additional_info.client_response_meta.cug_code = hashedCugCode.hashedItem;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
success: true,
|
|
137
|
+
message: purchase_details
|
|
138
|
+
};
|
|
139
|
+
} catch (error) {
|
|
140
|
+
console.error(`Error while masking the purchase_details for _id ${purchase_details?._id}.`, error.message);
|
|
141
|
+
return {
|
|
142
|
+
success: false,
|
|
143
|
+
error: `Error while masking the purchase_details for _id ${purchase_details?._id}: ${error.message}`
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
module.exports = maskPid;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "message-brokers",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.0-beta",
|
|
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",
|