简体   繁体   中英

Azure Function Python - serviceBusTrigger - How to deadletter a message

I have a plain simple Python function which should dead-letter a message if it does not match few constraint. Actually I'm raising an exception and everything works fine (I mean the message is being dead-lettered), but I would like to understand if there is a "clean" way to dead-letter the message without raising an exception.

async def function_handler(message: func.ServiceBusMessage, starter: str):
    for msg in [message]:
        client = df.DurableOrchestrationClient(starter)
        message_body = msg.get_body().decode("utf-8")

        msg = json.loads(message_body)

        if 'valid' in msg:    
           instance_id = await client.start_new('orchestrator', None, json.dumps(message_body))
        else:
           raise Exception(f'not found valid {msg["id"]}')

This is part of host.json , this should indicate I'm working with version 2.0 of Azure Functions

  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[2.*, 3.0.0)"
  },

Suggestions are welcome

At time of writing, in Python it is not possible interactively send a message in dead-letter.

I found out that autocomplete=false is only supported for C# .

This basically means that the only way to dead letter a message is raise an exception, just like I was doing in my code.

Thanks to @GauravMantri to pointing me the right way (ie have a look at how to use the autocomplete configuration parameter).

Azure Service Bus Queue has this Max Delivery Count property that you can make use of. Considering you only want to process a message exactly once and then deadletter the message in case Function is unable to process, what you can do is set the max delivery count to 1. That way the message will be automatically deadlettered after 1st delivery.

By default, Function runtime tries to auto-complete the message if there is no exception in processing the message. You do not want Function runtime to do that. For that what you would need to do is set auto complete setting to false. However if the message is processed successfully, you would want to delete that message thus you will need to call auto complete manually if the message processing is successful.

Something like:

if 'valid' in msg:    
    instance_id = await client.start_new('orchestrator', None, json.dumps(message_body))
    //auto complete the message here...
else:
    //do nothing and the message will be dead-lettered

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM