@platform-x/hep-message-broker-client 1.0.1 → 1.0.2
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/dist/src/config/index.js
CHANGED
|
@@ -32,7 +32,7 @@ exports.default = {
|
|
|
32
32
|
CONNECTION_ERROR: (_q = process.env.RABBITMQ_CONNECTION_ERROR) !== null && _q !== void 0 ? _q : false,
|
|
33
33
|
PUBLISHER_ERROR: (_r = process.env.RABBITMQ_PUBLISHER_ERROR) !== null && _r !== void 0 ? _r : false,
|
|
34
34
|
CONSUMER_ERROR: (_s = process.env.RABBITMQ_CONSUMER_ERROR) !== null && _s !== void 0 ? _s : false,
|
|
35
|
-
CONFIG_PATH: (_t = process.env.CONFIG_PATH) !== null && _t !== void 0 ? _t :
|
|
35
|
+
CONFIG_PATH: (_t = process.env.CONFIG_PATH) !== null && _t !== void 0 ? _t : "C:/platform-X/rabbitMQConfig.json",
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
38
|
//# sourceMappingURL=index.js.map
|
|
@@ -32,7 +32,7 @@ export declare class MessageBrokerClient {
|
|
|
32
32
|
* @param messagePublisherErrorHandler
|
|
33
33
|
* @returns
|
|
34
34
|
*/
|
|
35
|
-
publishMessageToExchange(request: PublishMessageInputType): Promise<
|
|
35
|
+
publishMessageToExchange(request: PublishMessageInputType): Promise<boolean>;
|
|
36
36
|
/**
|
|
37
37
|
* Funciton to message handler
|
|
38
38
|
*/
|
|
@@ -62,7 +62,7 @@ export declare class MessageBrokerClient {
|
|
|
62
62
|
* @param queueName
|
|
63
63
|
* @retruns
|
|
64
64
|
*/
|
|
65
|
-
sendMessageToDeadLatter(attempt: number, data: any): Promise<boolean
|
|
65
|
+
sendMessageToDeadLatter(attempt: number, data: any): Promise<boolean>;
|
|
66
66
|
/***
|
|
67
67
|
* Function to send message to retry queue
|
|
68
68
|
* @param attempt
|
|
@@ -79,6 +79,14 @@ export declare class MessageBrokerClient {
|
|
|
79
79
|
* @returns
|
|
80
80
|
*/
|
|
81
81
|
findQueueAndExchange(queueName: string, exchangeName: string): Promise<void>;
|
|
82
|
+
/**
|
|
83
|
+
* Function to check connection and channel
|
|
84
|
+
*/
|
|
85
|
+
private checkConnectionAndChannel;
|
|
86
|
+
/**
|
|
87
|
+
* Function to recreate channel
|
|
88
|
+
*/
|
|
89
|
+
private recreateChannel;
|
|
82
90
|
/**
|
|
83
91
|
* Function to setup the queues and exchanges
|
|
84
92
|
* @params array
|
|
@@ -199,67 +199,55 @@ class MessageBrokerClient {
|
|
|
199
199
|
return __awaiter(this, void 0, void 0, function* () {
|
|
200
200
|
var _a;
|
|
201
201
|
logger_1.Logger.info('Reached:publishMessageToExchange', 'publishMessageToExchange');
|
|
202
|
-
|
|
203
|
-
|
|
202
|
+
let attempt = 0;
|
|
203
|
+
const exchangeName = (_a = request === null || request === void 0 ? void 0 : request.exchangeName) !== null && _a !== void 0 ? _a : '';
|
|
204
|
+
const queueName = request.queueName;
|
|
205
|
+
const data = request === null || request === void 0 ? void 0 : request.message;
|
|
206
|
+
const messageBuffer = Buffer.from(JSON.stringify(data.message));
|
|
204
207
|
try {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
// let retries: number = typeof data.message === 'object' && data.message !== null ? (data.message as { retries: number }).retries ?? 0 : 0;
|
|
214
|
-
// while (attempt < maxRetries) {
|
|
215
|
-
const isSent = yield new Promise((resolve, reject) => {
|
|
216
|
-
const sent = channel.publish(exchangeName, queueName, messageBuffer, {
|
|
217
|
-
persistent: true, // Ensures the message is persisted
|
|
218
|
-
// correlationId: this.correlationId,
|
|
208
|
+
// 1. Ensure connection and channel exists
|
|
209
|
+
yield this.checkConnectionAndChannel();
|
|
210
|
+
// 2. Retry loop
|
|
211
|
+
while (attempt < maxRetries) {
|
|
212
|
+
try {
|
|
213
|
+
logger_1.Logger.info(`Attempting to publish message`, 'publishMessageToExchange');
|
|
214
|
+
let sent = channel.publish(exchangeName, queueName, messageBuffer, {
|
|
215
|
+
persistent: true,
|
|
219
216
|
});
|
|
220
|
-
|
|
221
|
-
if (sent) {
|
|
222
|
-
logger_1.Logger.
|
|
223
|
-
|
|
217
|
+
// if(attempt <1){ sent = false}else{sent = true} // Simulate failure for testing retry logic
|
|
218
|
+
if (sent && (RABBITMQ.PUBLISHER_ERROR !== 'true' && RABBITMQ.PUBLISHER_ERROR !== true)) {
|
|
219
|
+
logger_1.Logger.info(`Message published successfully on attempt ${attempt + 1}`, 'publishMessageToExchange');
|
|
220
|
+
logger_1.Logger.info(`✅ Message sent successfully`, 'publishMessageToExchange');
|
|
221
|
+
break;
|
|
224
222
|
}
|
|
225
223
|
else {
|
|
226
|
-
|
|
227
|
-
|
|
224
|
+
throw new Error('Publish returned false');
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
catch (error) {
|
|
228
|
+
attempt++;
|
|
229
|
+
logger_1.Logger.error(`Failed attempt ${attempt} to publish message`, 'publishMessageToExchange', error);
|
|
230
|
+
if (attempt < maxRetries) {
|
|
231
|
+
logger_1.Logger.info(`Retrying in ${retry_delay}ms...`, 'publishMessageToExchange');
|
|
232
|
+
yield new Promise(res => setTimeout(res, retry_delay));
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
logger_1.Logger.error(`Error in publishMessageToExchange: ${error}`, 'publishMessageToExchange');
|
|
236
|
+
throw new Error('Max retries reached');
|
|
228
237
|
}
|
|
229
|
-
});
|
|
230
|
-
logger_1.Logger.info(`Message publish attempt completed status ${isSent}`, 'publishMessageToExchange');
|
|
231
|
-
if ((isSent) && (RABBITMQ.PUBLISHER_ERROR !== 'true' && RABBITMQ.PUBLISHER_ERROR !== true)) {
|
|
232
|
-
logger_1.Logger.info(`Message published successfully`, 'publishMessageToExchange');
|
|
233
|
-
return isSent;
|
|
234
238
|
}
|
|
235
|
-
// else{
|
|
236
|
-
// let queueName = (request as PublishMessageInputType)?.queueName
|
|
237
|
-
// if (attempt >= maxRetries) {
|
|
238
|
-
// Logger.error('Max retries reached. Failed to publish message.', 'publishMessageToExchange', request);
|
|
239
|
-
// // Optionally: Log or persist the failed message to a DB, file, or other storage for manual intervention
|
|
240
|
-
// this.sendMessageToDeadLatter(attempt, data, queueName);
|
|
241
|
-
// this.messagePublisherErrorHandler(request); // we need to comment this line
|
|
242
|
-
// if (typeof classInstance[messagePublisherErrorHandler] === 'function') {
|
|
243
|
-
// classInstance[messagePublisherErrorHandler](data); // Dynamically call the method
|
|
244
|
-
// } else {
|
|
245
|
-
// Logger.info(`Method ${messagePublisherErrorHandler} not found in classInstance.`, 'publishMessageToExchange');
|
|
246
|
-
// }
|
|
247
|
-
// return false;
|
|
248
|
-
// }else{
|
|
249
|
-
// await this.publishMessageRetryQueue(request, (attempt+1));
|
|
250
|
-
// attempt++;
|
|
251
|
-
// }
|
|
252
|
-
// }
|
|
253
|
-
// }
|
|
254
239
|
}
|
|
240
|
+
return true;
|
|
255
241
|
}
|
|
256
242
|
catch (error) {
|
|
257
|
-
logger_1.Logger.error(
|
|
258
|
-
|
|
243
|
+
logger_1.Logger.error('Max retries reached. Sending message to Dead Letter Queue...', 'publishMessageToExchange');
|
|
244
|
+
yield this.sendMessageToDeadLatter(attempt, data);
|
|
245
|
+
const customError = new Error('Failed to publish message to exchange');
|
|
246
|
+
customError.originalError = error; // optional
|
|
247
|
+
throw customError;
|
|
259
248
|
}
|
|
260
249
|
});
|
|
261
250
|
}
|
|
262
|
-
;
|
|
263
251
|
/**
|
|
264
252
|
* Funciton to message handler
|
|
265
253
|
*/
|
|
@@ -444,19 +432,31 @@ class MessageBrokerClient {
|
|
|
444
432
|
*/
|
|
445
433
|
sendMessageToDeadLatter(attempt, data) {
|
|
446
434
|
return __awaiter(this, void 0, void 0, function* () {
|
|
447
|
-
logger_1.Logger.info("Reached: send message on dead
|
|
435
|
+
logger_1.Logger.info("Reached: send message on dead letter queue", 'sendMessageToDeadLater');
|
|
448
436
|
try {
|
|
449
437
|
if (isConnectionOpen) {
|
|
450
|
-
|
|
438
|
+
logger_1.Logger.info(`Attempting to send message to DLX queue: ${dlx_queue} (attempt: ${attempt}, retry-count: ${attempt - 1}, size: ${JSON.stringify(data).length} bytes)`, 'sendMessageToDeadLater');
|
|
439
|
+
const sent = channel.sendToQueue(dlx_queue, Buffer.from(JSON.stringify(data)), {
|
|
451
440
|
persistent: true,
|
|
452
441
|
headers: {
|
|
453
442
|
'x-retry-count': attempt - 1
|
|
454
443
|
},
|
|
455
444
|
});
|
|
445
|
+
if (sent) {
|
|
446
|
+
logger_1.Logger.info(`Message successfully published to DLX queue: ${dlx_queue} (attempt: ${attempt}, retry-count: ${attempt - 1})`, 'sendMessageToDeadLater');
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
logger_1.Logger.error(`Failed to publish message to DLX queue: ${dlx_queue} - Channel buffer full (attempt: ${attempt}, retry-count: ${attempt - 1})`, 'sendMessageToDeadLater');
|
|
450
|
+
}
|
|
451
|
+
return sent;
|
|
452
|
+
}
|
|
453
|
+
else {
|
|
454
|
+
logger_1.Logger.error(`Cannot send message to DLX - Connection is not open (queue: ${dlx_queue}, attempt: ${attempt})`, 'sendMessageToDeadLater');
|
|
455
|
+
return false;
|
|
456
456
|
}
|
|
457
457
|
}
|
|
458
458
|
catch (error) {
|
|
459
|
-
logger_1.Logger.error(
|
|
459
|
+
logger_1.Logger.error(`Error sending message to dead letter queue: ${error.message} (queue: ${dlx_queue}, attempt: ${attempt})`, 'sendMessageToDeadLater');
|
|
460
460
|
throw error;
|
|
461
461
|
}
|
|
462
462
|
});
|
|
@@ -544,6 +544,40 @@ class MessageBrokerClient {
|
|
|
544
544
|
}
|
|
545
545
|
});
|
|
546
546
|
}
|
|
547
|
+
/**
|
|
548
|
+
* Function to check connection and channel
|
|
549
|
+
*/
|
|
550
|
+
checkConnectionAndChannel() {
|
|
551
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
552
|
+
logger_1.Logger.info('Reached: checkConnectionAndChannel', 'checkConnectionAndChannel');
|
|
553
|
+
// 🧪 1. Ensure connection exists
|
|
554
|
+
if (!isConnectionOpen) {
|
|
555
|
+
logger_1.Logger.warn('No connection found. Creating new connection...', 'publishMessageToExchange');
|
|
556
|
+
yield this.createConnection();
|
|
557
|
+
}
|
|
558
|
+
// 🧪 2. Ensure channel is alive
|
|
559
|
+
const channelConnection = channel === null || channel === void 0 ? void 0 : channel.connection;
|
|
560
|
+
if (!channel || !channelConnection || channelConnection.stream.destroyed) {
|
|
561
|
+
logger_1.Logger.warn('Channel seems dead, recreating before publish...', 'publishMessageToExchange');
|
|
562
|
+
yield this.recreateChannel();
|
|
563
|
+
}
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* Function to recreate channel
|
|
568
|
+
*/
|
|
569
|
+
recreateChannel() {
|
|
570
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
571
|
+
try {
|
|
572
|
+
channel = yield connection.createConfirmChannel();
|
|
573
|
+
channel.prefetch(RABBITMQ === null || RABBITMQ === void 0 ? void 0 : RABBITMQ.PREFETCH_COUNT);
|
|
574
|
+
logger_1.Logger.info(' Channel recreated successfully.', 'recreateChannel');
|
|
575
|
+
}
|
|
576
|
+
catch (err) {
|
|
577
|
+
logger_1.Logger.error(' Failed to recreate channel:', err);
|
|
578
|
+
}
|
|
579
|
+
});
|
|
580
|
+
}
|
|
547
581
|
/**
|
|
548
582
|
* Function to setup the queues and exchanges
|
|
549
583
|
* @params array
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platform-x/hep-message-broker-client",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "@platform-x/hep-message-broker-client",
|
|
5
5
|
"main": "./dist/src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -37,4 +37,4 @@
|
|
|
37
37
|
"publishConfig": {
|
|
38
38
|
"access": "public"
|
|
39
39
|
}
|
|
40
|
-
}
|
|
40
|
+
}
|