pika add headers to nack response

Viewed 908

I am modifying pika headers using

properties.headers = {
     'myheader': myheader
}

But I am acking and nacking with the delivery_tag

channel.basic_nack(delivery_tag=delivery_tag, requeue=False)

How can I pass the update properties with the headers to the ack and nack response functions? Or what is the pika way of doing this?

2 Answers

It is correct that basic_nack cannot change the headers.

The way to do this is not to use NACK at all but to generate and return a 'new' message (which is simply the current message you are handling, but adding new headers to it).

It appears that a NACK is basically doing this anyway according to the AMQP spec.

So my logic is using basic_ack on success and message generation with updated headers on failure. And in my case I 'redirect' the new message to a dead letter exchange which a dead letter queue is binded to.

An extract from pika docs say that you misspell basic_nck... is just a question error or is your actual issue?

def basic_nack(self, delivery_tag=None, multiple=False, requeue=True):
        """This method allows a client to reject one or more incoming messages.
        It can be used to interrupt and cancel large incoming messages, or
        return untreatable messages to their original queue.

        :param integer delivery-tag: int/long The server-assigned delivery tag
        :param bool multiple: If set to True, the delivery tag is treated as
                              "up to and including", so that multiple messages
                              can be acknowledged with a single method. If set
                              to False, the delivery tag refers to a single
                              message. If the multiple field is 1, and the
                              delivery tag is zero, this indicates
                              acknowledgement of all outstanding messages.
        :param bool requeue: If requeue is true, the server will attempt to
                             requeue the message. If requeue is false or the
                             requeue attempt fails the messages are discarded or
                             dead-lettered.

        """
        self._raise_if_not_open()
        return self._send_method(
            spec.Basic.Nack(delivery_tag, multiple, requeue))

Sorry for that but as far as I know is not possible for a basic_nack (or basic_ack) modify the headers. The problem is that the modified message will be put in the dead letter queue and the new one has a newer ID.

L-

Related