Service Bus to Azure Function Deliver Again if Azure Function Fails
Using Azure Service Bus with Azure Functions is a great selection to trigger functions. If the function fails you'll be able to retry your function. If a part fails to execute you lot tin can either investigate your logging tool of pick or look at the fault provided past the error message in the dead-letter queue. The question is practise you get the information you await and is it enough to understand the error?
Default fault information setup
With Azure Service Bus using the latest Azure.Messaging.ServiceBus the types for messages and SDK has inverse a fleck. This is the version covered in this post.
We showtime by creating a standard Azure Function with a ServiceBusTrigger. Subsequently, ensure the package Microsoft.Azure.WebJobs.Extensions.ServiceBus is upgraded to five.2.0 (latest every bit of writing this postal service). Update the input trigger to be of type ServiceBusReceivedMessage. This is the new blazon for receiving messages.
The queue and connectedness values are both set in the configuration file, hence the syntax.
With this setup, we can simply throw an exception to illustrate the default error information of the function execution.
Testing default fault handling
Now when the function executes and the exception is thrown, the fault messages provided in the exception should show up, but does it? Let's attempt it.
To test the setup on your local machine requires 3 steps.
1. Run the function
Simply start the function on your local motorcar, debug or not-debug
2. Transport a message to the queue
Sending a message to the queue can be done either from the Azure Portal or a tool like Service Motorbus Explorer. From the Azure Portal and navigate to the Service Omnibus Namespace where the queue you lot're using is located. Select the queue and utilise the Service Bus Explorer (Preview) tool.
Create a message with content-blazon set to JSON and a UserId belongings that matches the type nosotros're expecting in the function. Finally, click ship
When the bulletin is sent, nosotros should see the function being triggered. Past default, the message is retried 10 times.
3. Monitor message treatment
If the function was deployed to Azure, we could utilise look at the execution at that place. As nosotros're running locally and we also don't want to await for the information in Azure, nosotros're using the Service Bus Explorer tool.
The tool requires a connectedness to the ServiceBus namespace. Within the tool we can meet our queues, here nosotros meet "web log", the name of the queue used in this example. The second number in the parentheses indicates the number of dead-letter messages. If we select Dead-letter from the bill of fare below, nosotros can investigate the dead-alphabetic character queue. Hither we can see the bulletin we sent and as well some Message Custom Properties, to the right.
DeadLetterReason and DeadLetterErrorDescription contain the error information, but does it provide us with enough information about what caused the error?
MaxDeliveryCountExceeded
Message could not be consumed subsequently 10 delivery attempts
No data about the bodily error that caused the not-treatment of the message.
Improved Error Data
To improve error handling, nosotros can make use of the additional property AutoCompleteMessages of the ServiceBusTrigger, and an extra input parameter ServiceBusMessageActions.
AutoCompleteMessages is by default true and ensures automatic complete and dead lettering of failed messages. If we set up it to false, we can take over the message handling and provide extra information, exactly what nosotros want.
When we hook into the treatment of letters, nosotros also demand ServiceBusMessageActions to actually consummate or dead-alphabetic character messages.
With a piffling rewrite, the improved Azure Function is now handling exceptions and sending failed letters to the dead-letter queue together with an mistake message. If the message is handled okay, it'south just completed. For analogy, nosotros're just using the UserId to determine whether or not the message handling should fail.
If we send a new message with a UserId of 666 what happens?
Again the bulletin handling fails, and we go some other dead-alphabetic character message. If we audit the Service Coach Explorer and Peek the expressionless-letter queue, we see a second message.
This time the DeadletterReason contains the description provided by the exception bulletin, exactly what nosotros wanted 👍
All source code is available on GitHub
bernacchithounater.blogspot.com
Source: https://blog.rasmustc.com/azure-servicebus-azurefunction-improved-error-handling/
0 Response to "Service Bus to Azure Function Deliver Again if Azure Function Fails"
Post a Comment