nestjs icon indicating copy to clipboard operation
nestjs copied to clipboard

Message won't ack and keep looping the consumer handler

Open toriqahmads opened this issue 1 year ago • 1 comments

Hi, I'm trying to create a delayed message, but the message won't ack caused infinite loop in consuming the message even no error happen.

Here is my consumer code

@RabbitSubscribe({
    exchange: "delayed",
    queue: QUEUE_NAME,
    routingKey: "delayed.key",
    allowNonJsonMessages: true,
  })
  async getDelayToken(message: any) {
    try {
      console.log(message);
      return Promise.resolve();
    } catch (err) {
      this.logger.error(err.message, err.stack);
      return new Nack(true);
    }
  }

this is the setup module code

RabbitMQModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => {
        return {
          name: RABBIT_MQ_CLIENT_NAME,
          uri: [configService.get<string>("RABBIT_MQ_URL")],
          exchanges: [{
            name: 'delayed',
            type: 'x-delayed-message',
            options: {
              durable: true,
              arguments: { 
                'x-delayed-type': 'direct' 
              },
            },
          }],
          queues: [{
            name: QUEUE_NAME,
            exchange: 'delayed',
            createQueueIfNotExists: true,
            options: {
              durable: true,
            }
          }],
          defaultSubscribeErrorBehavior: MessageHandlerErrorBehavior.REQUEUE,
          enableControllerDiscovery: true,
        }
      }
    })

The message keep redelivered

Image

toriqahmads avatar Feb 21 '25 03:02 toriqahmads

SOLVED! So the issue is the response will be forwarded through interceptor first. We need to add handle if the response context type is 'rmq' then pass it

toriqahmads avatar Feb 21 '25 04:02 toriqahmads