Azure Function Queue Trigger V2 not reading the message

Pedro Junqueira 20 Reputation points
2023-09-25T20:56:30.9166667+00:00

I am trying to read a message from a queue storage.

I am following exactly the tutorial code and running locally using azurite (tested also with a storage).

The the strange thing is that when a new message arrive at the queue the function gets triggered and then moved to poison queue but nothing happens in between.

The logs do not log, therefore I cannot do anything with the message.

@app.queue_trigger(arg_name="azqueue", queue_name="hyper-tyre-to-process",
                               connection="AzureStorageAccount") 
def queue_trigger(azqueue: func.QueueMessage):
   
   logging.info('Python queue trigger function processed a queue item: %s',
                 azqueue.get_body().decode('utf-8'))


User's image

It works but but really does not do the most important thing. Read the message.

What I am doing wrong?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,284 questions
Azure Queue Storage
Azure Queue Storage
An Azure service that provides messaging queues in the cloud.
97 questions
{count} votes

Accepted answer
  1. navba-MSFT 17,125 Reputation points Microsoft Employee
    2023-09-27T10:35:57.96+00:00

    @Pedro Junqueira I'm glad to see you were able to resolve your issue.

    Thanks for posting your solution so that others experiencing the same thing can easily reference this. Since the Microsoft Q&A community has a policy that the question author cannot accept their own answer, they can only accept answers by others, I'll repost your solution in case you'd like to Accept the answer.

    Issue:
    You have a Python QueueTrigger Function app which is not able to read the new message in the queue and you are getting the error Message has reached MaxDequeueCount of 5. Moving message to queue.

    Resolution:

    The above error message is completely misleading and has nothing to do with the encoding issue. The same has been already been reported to MSFT as a bug.

    You got the right code from this stackoverflow post.

    Here is the code that you have used and it is working when sending the message to be then able to get the message using function trigger.

    from azure.storage.queue import (
            QueueClient,
            BinaryBase64EncodePolicy,
            BinaryBase64DecodePolicy
    )
    queue_client = QueueClient.from_connection_string(
        connection_string,
        queue_name
        )
    
    # Setup Base64 encoding and decoding functions
    queue_client.message_encode_policy = BinaryBase64EncodePolicy()
    queue_client.message_decode_policy = BinaryBase64DecodePolicy()
    
    for i in range(10):
    
        print(f'Sending message {i}')
        message = {'id': i, 'message' :'Hello World'}
        message_string = json.dumps(message)
        message_bytes =  message_string.encode('utf-8')
        queue_client.send_message(
            queue_client.message_encode_policy.encode(content=message_bytes)
            )
    
    

1 additional answer

Sort by: Most helpful
  1. Pedro Junqueira 20 Reputation points
    2023-09-27T01:57:35.87+00:00

    Hi @navba-MSFT ,

    Thanks a lot for taking the time to look into my problem.

    I am using Azure Function V2 and below is my host.json file

    There is nothing saying about it queues. It is like this.

    {
      "version": "2.0",
      "logging": {
        "applicationInsights": {
          "samplingSettings": {
            "isEnabled": true,
            "excludedTypes": "Request"
          }
        }
      },
      "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[3.*, 4.0.0)"
      }
    }
    
    
    

    That was not the problem.

    The issue is that the code in python I used to send the message to the queue was incorrect when it comes to the encoding.

    The error message of Message has reached MaxDequeueCount of 5. Moving message to queue.

    is completely misleading and has nothing to do with the encoding issue. By the way it has already been reported to MSFT as a bug.

    I realized that if I manually add a message to the queue, using the correct encoding, then my original code just work fine.

    Then I changed my code to send the message in the right encoding which is Base64 . It is a bit more tricky than just using the base64 python package.

    The write code I got was in stack overflow post.

    Here is the code that works when sending the message to be then able to get the message using function trigger.

    from azure.storage.queue import (
            QueueClient,
            BinaryBase64EncodePolicy,
            BinaryBase64DecodePolicy
    )
    queue_client = QueueClient.from_connection_string(
        connection_string,
        queue_name
        )
    
    # Setup Base64 encoding and decoding functions
    queue_client.message_encode_policy = BinaryBase64EncodePolicy()
    queue_client.message_decode_policy = BinaryBase64DecodePolicy()
    
    for i in range(10):
    
        print(f'Sending message {i}')
        message = {'id': i, 'message' :'Hello World'}
        message_string = json.dumps(message)
        message_bytes =  message_string.encode('utf-8')
        queue_client.send_message(
            queue_client.message_encode_policy.encode(content=message_bytes)
            )