golevelup / nestjs

A collection of badass modules and utilities to help you level up your NestJS applications 🚀
MIT License
2.16k stars 244 forks source link

Single Channel Configuration Across Multiple @RabbitSubscribe Decorators Not Working as Expected #717

Closed xfd1387 closed 3 months ago

xfd1387 commented 3 months ago

I'm encountering an issue with the @golevelup/nestjs-rabbitmq package where multiple channels are being created despite explicitly configuring a single channel to ensure ordered processing of events. This behavior is contrary to my expectation based on the configuration provided, where a single channel named 'user-service-channel-1' is defined to preserve the order of event processing.

Configuration Details

I have declared a channel 'user-service-channel-1' in the RabbitMQModule.forRootAsync method and referenced this channel in the queueOptions for each @RabbitSubscribe decorator. Despite this setup, the RabbitMQ Management UI indicates the creation of 3 separate channels instead of utilizing the single configured channel.

Here is a snippet of the module configuration:

    RabbitMQModule.forRootAsync(RabbitMQModule, {
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        exchanges: [
          {
            name: 'event-bus.exchange',
            type: 'topic',
          },
        ],
        channels: {
          'user-service-channel-1': {
            prefetchCount: 1,
            default: true,
          }
        },
        uri: configService.get<string>('RABBITMQ_CONNECTION_URI', 'amqp://guest:guest@localhost:5672'),
        connectionInitOptions: { wait: true },
      }),
      inject: [ConfigService],
    }),

And the event handlers:

  @RabbitSubscribe({
    exchange: 'event-bus.exchange',
    routingKey: 'tenant.created',
    queue: 'user-service',
    queueOptions: { channel: 'user-service-channel-1' },
    createQueueIfNotExists: false
  })
  public async tenantCreatedEventHandler(msg: any, amqpMsg: ConsumeMessage) {}

  @RabbitSubscribe({
    exchange: 'event-bus.exchange',
    routingKey: 'tenant.updated',
    queue: 'user-service',
    queueOptions: { channel: 'user-service-channel-1' },
    createQueueIfNotExists: false
  })
  public async tenantUpdatedEventHandler(msg: any, amqpMsg: ConsumeMessage) {}

  @RabbitSubscribe({
    exchange: 'event-bus.exchange',
    routingKey: 'tenant.deleted',
    queue: 'user-service',
    queueOptions: { channel: 'user-service-channel-1' },
    createQueueIfNotExists: false
  })
  public async tenantDeletedEventHandler(msg: any, amqpMsg: ConsumeMessage) {}

Expected Behavior

A single channel ('user-service-channel-1') is utilized for all @RabbitSubscribe decorators to ensure the order of events is preserved.

Observed Behavior

Multiple channels are created, leading to potential ordering issues, as evidenced by the RabbitMQ Management UI screenshot:

Capture

Request

I seek guidance or a fix to ensure that the specified channel configuration is respected across multiple event handlers to preserve event order.