From ca474236b069b3d506e61423a7886db506b773b0 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Wed, 11 Dec 2024 15:33:01 -0300 Subject: [PATCH] feat: Add PREFIX_KEY configuration for RabbitMQ integration - Introduced a new optional PREFIX_KEY in the RabbitMQ configuration to allow custom queue naming. - Updated the AMQP server and channel service to utilize PREFIX_KEY when initializing queues. - Modified the dev environment configuration to include PREFIX_KEY. This enhancement improves the flexibility of queue naming in RabbitMQ, facilitating better organization and management of queues. --- src/api/integrations/rabbitmq/libs/amqp.server.ts | 7 ++++++- src/api/services/channel.service.ts | 6 +++++- src/config/env.config.ts | 2 ++ src/dev-env.yml | 1 + 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/api/integrations/rabbitmq/libs/amqp.server.ts b/src/api/integrations/rabbitmq/libs/amqp.server.ts index 99c10f66..695657da 100644 --- a/src/api/integrations/rabbitmq/libs/amqp.server.ts +++ b/src/api/integrations/rabbitmq/libs/amqp.server.ts @@ -45,6 +45,7 @@ export const getAMQP = (): amqp.Channel | null => { export const initGlobalQueues = () => { logger.info('Initializing global queues'); const events = configService.get('RABBITMQ').EVENTS; + const prefixKey = configService.get('RABBITMQ').PREFIX_KEY; if (!events) { logger.warn('No events to initialize on AMQP'); @@ -56,7 +57,11 @@ export const initGlobalQueues = () => { eventKeys.forEach((event) => { if (events[event] === false) return; - const queueName = `${event.replace(/_/g, '.').toLowerCase()}`; + const queueName = + prefixKey !== '' + ? `${prefixKey}.${event.replace(/_/g, '.').toLowerCase()}` + : `${event.replace(/_/g, '.').toLowerCase()}`; + const amqp = getAMQP(); const exchangeName = 'evolution_exchange'; diff --git a/src/api/services/channel.service.ts b/src/api/services/channel.service.ts index ea3a57c1..12b2556d 100644 --- a/src/api/services/channel.service.ts +++ b/src/api/services/channel.service.ts @@ -778,6 +778,7 @@ export class ChannelStartupService { if (rabbitmqGlobal && rabbitmqEvents[we] && amqp) { const exchangeName = 'evolution_exchange'; + const prefixKey = this.configService.get('RABBITMQ').PREFIX_KEY; let retry = 0; @@ -788,7 +789,10 @@ export class ChannelStartupService { autoDelete: false, }); - const queueName = event; + const queueName = + prefixKey !== '' + ? `${prefixKey}.${event.replace(/_/g, '.').toLowerCase()}` + : `${event.replace(/_/g, '.').toLowerCase()}`; await amqp.assertQueue(queueName, { durable: true, diff --git a/src/config/env.config.ts b/src/config/env.config.ts index f1ab0335..659858fd 100644 --- a/src/config/env.config.ts +++ b/src/config/env.config.ts @@ -104,6 +104,7 @@ export type Rabbitmq = { ENABLED: boolean; URI: string; EXCHANGE_NAME: string; + PREFIX_KEY?: string; GLOBAL_ENABLED: boolean; EVENTS: EventsRabbitmq; }; @@ -323,6 +324,7 @@ export class ConfigService { ENABLED: process.env?.RABBITMQ_ENABLED === 'true', GLOBAL_ENABLED: process.env?.RABBITMQ_GLOBAL_ENABLED === 'true', EXCHANGE_NAME: process.env?.RABBITMQ_EXCHANGE_NAME || 'evolution_exchange', + PREFIX_KEY: process.env?.RABBITMQ_PREFIX_KEY || '', URI: process.env.RABBITMQ_URI || '', EVENTS: { APPLICATION_STARTUP: process.env?.RABBITMQ_EVENTS_APPLICATION_STARTUP === 'true', diff --git a/src/dev-env.yml b/src/dev-env.yml index 42573ef3..03d5e84c 100644 --- a/src/dev-env.yml +++ b/src/dev-env.yml @@ -89,6 +89,7 @@ RABBITMQ: ENABLED: false URI: "amqp://guest:guest@localhost:5672" EXCHANGE_NAME: evolution_exchange + PREFIX_KEY: evolution GLOBAL_ENABLED: true EVENTS: APPLICATION_STARTUP: false