message-brokers 1.10.0-beta → 1.10.0-beta.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 +9 -0
- package/lib/configs/mb.configs.js +0 -1
- package/lib/configs/mqConfigs.js +12 -0
- package/lib/controllers/amqp.controller.js +0 -109
- package/lib/message-brokers.js +22 -11
- package/lib/package-loader.js +0 -11
- package/lib/rabbitmq/rabbitmq.helper.js +3 -5
- package/lib/services/deadqueue.service.js +6 -6
- package/lib/types +3 -39
- package/lib/utils/log.util.js +6 -6
- package/package.json +1 -1
- package/lib/configs/centralMqConfigs.js +0 -17
- package/lib/helpers/centralRabbitmq/centralRabbitmq.helper.js +0 -156
- package/lib/helpers/centralRabbitmq/centralRabbitmqConnection.js +0 -38
- package/lib/utils/maskPid.js +0 -147
package/README.md
CHANGED
|
@@ -180,6 +180,15 @@ messageBrokers.publishNotificationMessagesLogs(notification_messages_logs);
|
|
|
180
180
|
// Publish amqp_lab_api_response
|
|
181
181
|
messageBrokers.publishAmqpLabApiResponse(amqp_lab_api_response);
|
|
182
182
|
|
|
183
|
+
// Publish merged_lab_api_response
|
|
184
|
+
messageBrokers.publishAmqpMergedLabApiResponse(merged_lab_api_response);
|
|
185
|
+
|
|
183
186
|
// Publish billing_events
|
|
184
187
|
messageBrokers.publishBillingEvents(billing_events);
|
|
188
|
+
|
|
189
|
+
// Publish past data reminder request (hvconnect → LLM reminder app)
|
|
190
|
+
messageBrokers.publishPastDataReminderRequest(reminder_request);
|
|
191
|
+
|
|
192
|
+
// Publish prepared past data reminder message (LLM reminder app → hv-connect)
|
|
193
|
+
messageBrokers.publishPastDataReminderMessage(reminder_message);
|
|
185
194
|
```
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
const mbConfigs = {
|
|
2
2
|
rabbitmqUrl: process.env.RABBITMQ_URL,
|
|
3
|
-
centralRabbitmqUrl: process.env.CENTRAL_RABBITMQ_URL,
|
|
4
3
|
activeMessageBroker: process.env.ACTIVE_MESSAGE_BROKER || "rabbitmq",
|
|
5
4
|
redisPort: process.env.REDIS_PORT || 6260,
|
|
6
5
|
cachingEnabled: process.env.CACHING_ENABLED || false,
|
package/lib/configs/mqConfigs.js
CHANGED
|
@@ -20,10 +20,22 @@ const mqConfigs = [{
|
|
|
20
20
|
queueName: "v2.lab-data.queue",
|
|
21
21
|
routingKeyList: ["lab_api_response.amqp"],
|
|
22
22
|
priorityQueue: false
|
|
23
|
+
}, {
|
|
24
|
+
queueName: "v2.merged-lab-data.queue",
|
|
25
|
+
routingKeyList: ["merged_lab_api_response.amqp"],
|
|
26
|
+
priorityQueue: false
|
|
23
27
|
}, {
|
|
24
28
|
queueName: "billing.events",
|
|
25
29
|
routingKeyList: ["billing"],
|
|
26
30
|
priorityQueue: false
|
|
31
|
+
}, {
|
|
32
|
+
queueName: "llm-reminder.past-data.queue",
|
|
33
|
+
routingKeyList: ["past_data.reminder_request"],
|
|
34
|
+
priorityQueue: false
|
|
35
|
+
}, {
|
|
36
|
+
queueName: "messaging.past-data.reminder-message.queue",
|
|
37
|
+
routingKeyList: ["past_data.reminder_message"],
|
|
38
|
+
priorityQueue: false
|
|
27
39
|
}
|
|
28
40
|
// Example for priority queue.
|
|
29
41
|
// {
|
|
@@ -2,8 +2,6 @@ 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");
|
|
7
5
|
const AmqpController = {};
|
|
8
6
|
|
|
9
7
|
/**
|
|
@@ -127,111 +125,4 @@ AmqpController.publishPendingMessageInCache = async () => {
|
|
|
127
125
|
};
|
|
128
126
|
}
|
|
129
127
|
};
|
|
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
|
-
};
|
|
237
128
|
module.exports = AmqpController;
|
package/lib/message-brokers.js
CHANGED
|
@@ -6,18 +6,12 @@ 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";
|
|
10
9
|
class MessageBrokers {
|
|
11
10
|
constructor(mongoConnectionHolder, applicationName) {
|
|
12
11
|
this.mongoConnectionHolder = mongoConnectionHolder;
|
|
13
12
|
this.applicationName = applicationName || "mb_app";
|
|
14
13
|
this._initialized = false;
|
|
15
14
|
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Initializes the AMQP connection and MongoDB connection.
|
|
19
|
-
* @returns A promise that resolves to a response object.
|
|
20
|
-
*/
|
|
21
15
|
async init() {
|
|
22
16
|
if (this._initialized) return;
|
|
23
17
|
try {
|
|
@@ -152,6 +146,16 @@ class MessageBrokers {
|
|
|
152
146
|
return response;
|
|
153
147
|
}
|
|
154
148
|
|
|
149
|
+
/**
|
|
150
|
+
* This function is used publish merged_lab_api_response into the exchange.
|
|
151
|
+
* @param {Object} merged_lab_api_response
|
|
152
|
+
* @returns
|
|
153
|
+
*/
|
|
154
|
+
async publishAmqpMergedLabApiResponse(merged_lab_api_response) {
|
|
155
|
+
const response = await AmqpController.publishMessage(hv_exchange, "merged_lab_api_response.amqp", merged_lab_api_response);
|
|
156
|
+
return response;
|
|
157
|
+
}
|
|
158
|
+
|
|
155
159
|
/**
|
|
156
160
|
* This function is used publish billing events into the exchange.
|
|
157
161
|
* @param {Object} amqp_lab_api_response
|
|
@@ -163,19 +167,26 @@ class MessageBrokers {
|
|
|
163
167
|
}
|
|
164
168
|
|
|
165
169
|
/**
|
|
166
|
-
* This function is used publish
|
|
167
|
-
*
|
|
170
|
+
* This function is used publish a past data reminder request into the exchange.
|
|
171
|
+
* Consumed by the LLM reminder app (llm-reminder.past-data.queue).
|
|
172
|
+
* @param {Object} reminder_request
|
|
168
173
|
* @returns
|
|
169
174
|
*/
|
|
170
|
-
async
|
|
171
|
-
const response = await AmqpController.
|
|
175
|
+
async publishPastDataReminderRequest(reminder_request) {
|
|
176
|
+
const response = await AmqpController.publishMessage(hv_exchange, "past_data.reminder_request", reminder_request);
|
|
172
177
|
return response;
|
|
173
178
|
}
|
|
174
179
|
|
|
175
180
|
/**
|
|
176
|
-
* This function is used publish
|
|
181
|
+
* This function is used publish a prepared past data reminder message into the exchange.
|
|
182
|
+
* Consumed by hv-connect (messaging.past-data.reminder-message.queue).
|
|
183
|
+
* @param {Object} reminder_message
|
|
177
184
|
* @returns
|
|
178
185
|
*/
|
|
186
|
+
async publishPastDataReminderMessage(reminder_message) {
|
|
187
|
+
const response = await AmqpController.publishMessage(hv_exchange, "past_data.reminder_message", reminder_message);
|
|
188
|
+
return response;
|
|
189
|
+
}
|
|
179
190
|
async publishPendingMessageInCache() {
|
|
180
191
|
return await AmqpController.publishPendingMessageInCache();
|
|
181
192
|
}
|
package/lib/package-loader.js
CHANGED
|
@@ -4,8 +4,6 @@ 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");
|
|
9
7
|
|
|
10
8
|
/**
|
|
11
9
|
* This function is used to load all the db connections,
|
|
@@ -19,15 +17,6 @@ const package_loader = async (mongoConnectionHolder, rabbitMqEnable = true) => {
|
|
|
19
17
|
await MessageBroker.connect();
|
|
20
18
|
await MessageBroker.initializePubSub();
|
|
21
19
|
}
|
|
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
|
-
}
|
|
31
20
|
if (mongoConnectionHolder.connection) {
|
|
32
21
|
await mongoDB(mongoConnectionHolder.connection);
|
|
33
22
|
}
|
|
@@ -16,9 +16,7 @@ const {
|
|
|
16
16
|
applyExchangeName,
|
|
17
17
|
applyRoutingKeyIsolationForPublish
|
|
18
18
|
} = require("../utils/rabbitmq.utils");
|
|
19
|
-
|
|
20
|
-
// const offlinePubQueue = [];
|
|
21
|
-
|
|
19
|
+
const offlinePubQueue = [];
|
|
22
20
|
const RabbitMQHelper = {};
|
|
23
21
|
const CachingEnabled = mbConfigs.cachingEnabled === "true";
|
|
24
22
|
const RABBITMQ_DEPLOYMENT_ENV = process.env.RABBITMQ_DEPLOYMENT_ENV;
|
|
@@ -114,7 +112,7 @@ RabbitMQHelper.publish = async (exchangeName, routingKey, message, priority = 0,
|
|
|
114
112
|
if (!publishResponse) {
|
|
115
113
|
const message = `Error while publishing to the ${exchange_name} with the routing key ${routing_key}`;
|
|
116
114
|
console.log(message);
|
|
117
|
-
|
|
115
|
+
offlinePubQueue.push([exchange_name, routing_key, message]);
|
|
118
116
|
return {
|
|
119
117
|
error: true,
|
|
120
118
|
message: message,
|
|
@@ -136,7 +134,7 @@ RabbitMQHelper.publish = async (exchangeName, routingKey, message, priority = 0,
|
|
|
136
134
|
let routing_key = applyRoutingKeyIsolationForPublish(routingKey);
|
|
137
135
|
const message = `Error while publishing to the ${exchange_name} with the routing key ${routing_key}`;
|
|
138
136
|
console.log(message, error.message);
|
|
139
|
-
|
|
137
|
+
offlinePubQueue.push([exchange_name, routing_key, message]);
|
|
140
138
|
return {
|
|
141
139
|
error: true,
|
|
142
140
|
message: message,
|
|
@@ -3,10 +3,10 @@ const {
|
|
|
3
3
|
getActiveApplication
|
|
4
4
|
} = require("../helpers/app.helper");
|
|
5
5
|
const DeadQueueService = {};
|
|
6
|
-
DeadQueueService.storeFailureMessage = async
|
|
6
|
+
DeadQueueService.storeFailureMessage = async failedMessage => {
|
|
7
7
|
try {
|
|
8
8
|
const db = await mongoDB();
|
|
9
|
-
const collection = await db.collection(
|
|
9
|
+
const collection = await db.collection("deadqueue");
|
|
10
10
|
const applicationName = getActiveApplication();
|
|
11
11
|
const new_failed_message = {
|
|
12
12
|
...failedMessage,
|
|
@@ -14,17 +14,17 @@ DeadQueueService.storeFailureMessage = async (failedMessage, deadqueueCollection
|
|
|
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()}] [MQ]: ${failedMessage?.routingKey} added to dead_queue.`);
|
|
18
18
|
return {
|
|
19
19
|
success: true,
|
|
20
|
-
message:
|
|
20
|
+
message: "Successfully stored failed message in the deadqueue 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()}] [MQ]: Error while saving ${failedMessage?.routingKey} to dead_queue.`, error.message);
|
|
25
25
|
return {
|
|
26
26
|
success: false,
|
|
27
|
-
message:
|
|
27
|
+
message: "Error storing failed message in the deadqueue collection.",
|
|
28
28
|
data: error
|
|
29
29
|
};
|
|
30
30
|
}
|
package/lib/types
CHANGED
|
@@ -39,12 +39,6 @@ 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
|
-
|
|
48
42
|
/**
|
|
49
43
|
* Publishes a HAR object into the exchange.
|
|
50
44
|
* @param har - The HAR object to be published.
|
|
@@ -109,41 +103,11 @@ declare module "message-brokers" {
|
|
|
109
103
|
publishTrendingData(trending_data: object): Promise<PublishResponse>;
|
|
110
104
|
|
|
111
105
|
/**
|
|
112
|
-
* Publishes
|
|
113
|
-
* @param
|
|
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.
|
|
106
|
+
* Publishes a merged lab API response into the exchange.
|
|
107
|
+
* @param merged_lab_api_response - The merged lab API response object to be published.
|
|
144
108
|
* @returns A promise that resolves to a response object.
|
|
145
109
|
*/
|
|
146
|
-
|
|
110
|
+
publishAmqpMergedLabApiResponse(merged_lab_api_response: object): Promise<PublishResponse>;
|
|
147
111
|
|
|
148
112
|
/**
|
|
149
113
|
* Publishes all pending messages stored in the cache.
|
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
|
|
10
|
+
const exchangeLogger = async exchangeName => {
|
|
11
11
|
try {
|
|
12
12
|
const _chalk = await chalk;
|
|
13
|
-
console.log(_chalk.bgHex("5d4331").bold(
|
|
13
|
+
console.log(_chalk.bgHex("5d4331").bold("[*amqp*] Exchange creation: " + _chalk.bgGreen(` ${exchangeName}`)));
|
|
14
14
|
} catch (error) {
|
|
15
15
|
console.log(error);
|
|
16
16
|
}
|
|
17
17
|
};
|
|
18
|
-
const queueLogger = async
|
|
18
|
+
const queueLogger = async message => {
|
|
19
19
|
try {
|
|
20
20
|
const _chalk = await chalk;
|
|
21
|
-
console.log(_chalk.black.bgWhite.bold(
|
|
21
|
+
console.log(_chalk.black.bgWhite.bold(" [*amqp*]") + " " + _chalk.green(_chalk.italic(message)));
|
|
22
22
|
} catch (error) {
|
|
23
23
|
console.log(error);
|
|
24
24
|
}
|
|
25
25
|
};
|
|
26
|
-
const bindingLogger = async
|
|
26
|
+
const bindingLogger = async text => {
|
|
27
27
|
try {
|
|
28
28
|
const _chalk = await chalk;
|
|
29
|
-
console.log(_chalk.black.bgWhite.bold(
|
|
29
|
+
console.log(_chalk.black.bgWhite.bold(" [*amqp*]") + " " + _chalk.yellow(_chalk.italic(text[0]) + " -> " + _chalk.italic(text[1])));
|
|
30
30
|
} catch (error) {
|
|
31
31
|
console.log(error);
|
|
32
32
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "message-brokers",
|
|
3
|
-
"version": "1.10.0-beta",
|
|
3
|
+
"version": "1.10.0-beta.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",
|
|
@@ -1,17 +0,0 @@
|
|
|
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,156 +0,0 @@
|
|
|
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;
|
|
@@ -1,38 +0,0 @@
|
|
|
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/utils/maskPid.js
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
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;
|