@platform-x/hep-message-broker-client 1.1.13 → 1.1.15

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.
@@ -360,6 +360,53 @@ class MessageBroker {
360
360
  return false;
361
361
  });
362
362
  }
363
+ /**
364
+ * Description: Publishes a message directly to a RabbitMQ queue with configurable retry attempts.
365
+ * On each failed attempt, waits using exponential backoff based on TTL config.
366
+ * After all retries are exhausted, sends the message to the dead letter queue.
367
+ * @param request - The request object containing the queue name and the message payload
368
+ * @returns true if the message was published successfully, false otherwise
369
+ */
370
+ publishMessage(request) {
371
+ return __awaiter(this, void 0, void 0, function* () {
372
+ var _a, _b;
373
+ logger_1.Logger.info('Reached to publishMessage', 'publishMessage');
374
+ const { queueName, message } = request;
375
+ const retryDelayConfig = ((_a = message === null || message === void 0 ? void 0 : message.message) === null || _a === void 0 ? void 0 : _a.feed_type) === constants_1.RABBITMQ_FEED_TYPE.OVR ? RABBITMQ.OVR_TTL : RABBITMQ.ETX_TTL;
376
+ const retryDelay = retryDelayConfig.split('|').map(Number);
377
+ let attempt = 0;
378
+ while (attempt < maxRetries) {
379
+ try {
380
+ yield this.checkConnectionAndChannel();
381
+ const messageBuffer = Buffer.from(JSON.stringify(message));
382
+ const sent = this.channel.sendToQueue(queueName, messageBuffer, { persistent: true });
383
+ //if(attempt < 1){ sent = false}else{sent = true} // --- IGNORE ---
384
+ if (sent && (RABBITMQ.PUBLISHER_RETRY_ERROR !== 'true' && RABBITMQ.PUBLISHER_RETRY_ERROR !== true)) {
385
+ logger_1.Logger.info(`Message published successfully on attempt ${attempt + 1}`, 'publishMessage');
386
+ return true;
387
+ }
388
+ else {
389
+ throw new Error('Channel publish returned false — buffer full');
390
+ }
391
+ }
392
+ catch (error) {
393
+ attempt++;
394
+ logger_1.Logger.error(`Attempt ${attempt} of ${maxRetries} failed in ${retryDelay[attempt - 1] / 1000}s...: ${error.message}`, 'publishMessage');
395
+ if (attempt >= maxRetries) {
396
+ logger_1.Logger.error('Max retries reached. Sending message to Dead Letter Queue...', 'publishMessage');
397
+ yield this.sendToDeadLetterQueue({ message, attempt });
398
+ logger_1.Logger.error(`Failed to publish message to queue ${queueName} after ${maxRetries} attempts`, 'publishMessage');
399
+ return false;
400
+ }
401
+ // Exponential backoff using TTL array
402
+ const delay = (_b = retryDelay[attempt - 1]) !== null && _b !== void 0 ? _b : retryDelay[retryDelay.length - 1];
403
+ logger_1.Logger.info(`Retrying in ${delay / 1000}s...`, 'publishMessage');
404
+ yield new Promise(res => setTimeout(res, delay));
405
+ }
406
+ }
407
+ return false;
408
+ });
409
+ }
363
410
  /**
364
411
  * Description: Sends a failed message to the dead letter queue after all retries are exhausted.
365
412
  * @param request - The request object containing the message payload and the number of attempts made
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platform-x/hep-message-broker-client",
3
- "version": "1.1.13",
3
+ "version": "1.1.15",
4
4
  "description": "platform-x hep-message-broker service",
5
5
  "main": "./dist/src/index.js",
6
6
  "scripts": {