cody-greene / node-rabbitmq-client

RabbitMQ (0-9-1) client library with auto-reconnect & zero dependencies
MIT License
116 stars 8 forks source link

how nack in consumer #24

Closed primemb closed 1 year ago

primemb commented 1 year ago

Hi, i want to ask how to nack work on createConsumer in docs you said if you throw an error in handler it automatically return nack but if I throw an error my app crash this is my code :

this._consumer = this.rabbit.createConsumer(
      {
        queue: process.env.SERVICE_NAME as string,
        queueOptions: {
          durable: true,
          arguments: {
            "x-queue-type": "quorum",
            "x-delivery-limit": 3,
          },
        },
        qos: { prefetchCount: 1 },
        exchanges: [
          { exchange: "router", type: "topic", durable: true },
          { exchange: "x-dead-letter-exchange", type: "direct", durable: true },
        ],
        queueBindings: [{ exchange: "router", routingKey: "transcoder" }],
      },
      async (msg) => {
        await this.transcoderConsumer.handleConsume(
          msg,
          this.sendMessage.bind(this)
        );
      }
    );

so if i throw an exception on handleConsume my app gets crashes. and in general is it not better to use a boolean here? for example if this function returns true ack message and if it return false nack message

cody-greene commented 1 year ago

The consumer is emitting an error event. Attach an event listener with .on("error", cb)

And regarding your suggestion, yes see PR #21

-------- Original Message -------- On Jun 24, 2023, 2:44 PM, PRIME wrote:

Hi, i want to ask how to nack work on createConsumer in docs you said if you throw an error in handler it automatically return nack but if I throw an error my app crash this is my code :

this._consumer = this.rabbit.createConsumer( { queue: process.env.SERVICE_NAME as string, queueOptions: { durable: true, arguments: { "x-queue-type": "quorum", "x-delivery-limit": 3, }, }, qos: { prefetchCount: 1 }, exchanges: [ { exchange: "router", type: "topic", durable: true }, { exchange: "x-dead-letter-exchange", type: "direct", durable: true }, ], queueBindings: [{ exchange: "router", routingKey: "transcoder" }], }, async (msg) => { await this.transcoderConsumer.handleConsume( msg, this.sendMessage.bind(this) ); } );

so if i throw an exception on handleConsume my app gets crashes. and in general is it not better to use a boolean here? for example if this function returns true ack message and if it return false nack message

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

primemb commented 1 year ago

I think i am not explaining it well on consumer i have function that can throw an error for many reason so if i not place try catch on that the app get crashed and need fully restart and if i place try catch then handler in createConsumer thinks everything is fine and send ack .and about that PR it looks great

cody-greene commented 1 year ago

If your consumer handler throws an error, then the message is rejected (BasicNack) and an error event is emitted. Register an event handler to prevent the crash. Most people just log the errors.

-------- Original Message -------- On Jun 24, 2023, 4:00 PM, PRIME wrote:

I think i am not explaining it well on consumer i have function that can throw an error for many reason so if i not place try catch on that the app get crashed and need fully restart and if i place try catch then handler in createConsumer thinks everything is fine and send ack .and about that PR it looks great

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

primemb commented 1 year ago

thank you so much