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.
This commit is contained in:
Davidson Gomes 2024-12-11 15:33:01 -03:00
parent ebd70fe454
commit ca474236b0
4 changed files with 14 additions and 2 deletions

View File

@ -45,6 +45,7 @@ export const getAMQP = (): amqp.Channel | null => {
export const initGlobalQueues = () => {
logger.info('Initializing global queues');
const events = configService.get<Rabbitmq>('RABBITMQ').EVENTS;
const prefixKey = configService.get<Rabbitmq>('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';

View File

@ -778,6 +778,7 @@ export class ChannelStartupService {
if (rabbitmqGlobal && rabbitmqEvents[we] && amqp) {
const exchangeName = 'evolution_exchange';
const prefixKey = this.configService.get<Rabbitmq>('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,

View File

@ -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',

View File

@ -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