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

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.
@@ -52,7 +52,7 @@ const config_1 = __importDefault(require("../../config"));
52
52
  const ConfigManager_1 = __importDefault(require("../../config/ConfigManager"));
53
53
  const logger_1 = require("../../Util/logger");
54
54
  const constants_1 = require("../../Util/constants");
55
- const __1 = require("../..");
55
+ //import { getIAMSecrets } from '../..';
56
56
  const { RABBITMQ } = config_1.default;
57
57
  let configManager = ConfigManager_1.default.getInstance();
58
58
  let maxRetries = RABBITMQ.MAX_RETRIES;
@@ -152,10 +152,10 @@ class MessageBroker {
152
152
  try {
153
153
  logger_1.Logger.info('Attempting to connect to RabbitMQ', 'createConnection');
154
154
  configManager.loadConfig(RABBITMQ === null || RABBITMQ === void 0 ? void 0 : RABBITMQ.CONFIG_PATH);
155
- const secret = yield (0, __1.getIAMSecrets)();
156
- let url = `amqp://${secret === null || secret === void 0 ? void 0 : secret[constants_1.SECRET_KEYS.RABBITMQ_USER]}:${secret === null || secret === void 0 ? void 0 : secret[constants_1.SECRET_KEYS.RABBITMQ_PASS]}@${RABBITMQ === null || RABBITMQ === void 0 ? void 0 : RABBITMQ.HOST}`;
157
- // console.log(SECRET_KEYS.RABBITMQ_USER, SECRET_KEYS.RABBITMQ_PASS);
158
- // const url = `amqp://${RABBITMQ.USER}:${RABBITMQ.PASS}@${RABBITMQ.HOST}:${RABBITMQ.PORT}`;
155
+ // const secret = await getIAMSecrets();
156
+ // let url: string = `amqp://${secret?.[SECRET_KEYS.RABBITMQ_USER]}:${secret?.[SECRET_KEYS.RABBITMQ_PASS]}@${RABBITMQ?.HOST}`;
157
+ console.log(constants_1.SECRET_KEYS.RABBITMQ_USER, constants_1.SECRET_KEYS.RABBITMQ_PASS);
158
+ const url = `amqp://${RABBITMQ.USER}:${RABBITMQ.PASS}@${RABBITMQ.HOST}:${RABBITMQ.PORT}`;
159
159
  this.connection = yield amqp.connect(url, { heartbeat: RABBITMQ.HEARTBEAT });
160
160
  this.channel = yield this.connection.createConfirmChannel();
161
161
  this.connectionEventHandler(); // Event handlers for connection 'error' and 'close'
@@ -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.14",
4
4
  "description": "platform-x hep-message-broker service",
5
5
  "main": "./dist/src/index.js",
6
6
  "scripts": {