Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async programming and Azure functions

In the Azure functions "Performance considerations" part, Functions Best Practices, under "Use async code but avoid blocking calls", async programming is the suggested practice for performance improvement. However, what is the best way to use it? For example, in my scenario, I have the following Service Bus Trigger:

public static void Run(     [ServiceBusTrigger("topicname", "subname", AccessRights.Manage,      Connection = "TopicConnection")]string message, TraceWriter log) {     try {         log.Info($"C# ServiceBus topic trigger function processed message: {message}");          Task.Run(() => PushToDb(message, log));     }     catch(Exception ex)     {         log.Info($"Exception found {ex.Message}");     } } 

In the above code, I call PushToDb method async. However, since it runs in the background, Function runtime assumes that the messages are consumed successfully and completes it. What if the PushToDb method throws an exception? How can I make sure runtime knows that it's not complete, but rather should be abandoned?

Looking to use async as much as possible for performance.

like image 296
Mandar Jogalekar Avatar asked Jan 19 '18 11:01

Mandar Jogalekar


People also ask

In which situation should you use an Azure function app?

Azure Functions are best suited for smaller apps have events that can work independently of other websites. Some of the common azure functions are sending emails, starting backup, order processing, task scheduling such as database cleanup, sending notifications, messages, and IoT data processing.

Which of the following languages is not supported by durable functions?

Durable entities are currently not supported in Java. Entity functions are available in Durable Functions 2.0 and above for C#, JavaScript, and Python.

Can a function app have multiple functions Azure?

Organize your functions As part of your solution, you likely develop and publish multiple functions. These functions are often combined into a single function app, but they can also run in separate function apps.


1 Answers

You can make the function async:

public static async Task Run(     [ServiceBusTrigger("topicname", "subname", AccessRights.Manage, Connection = "TopicConnection")]string message,     TraceWriter log) {     try     {         log.Info($"C# ServiceBus topic trigger function processed message: {message}");         await PushToDb(message, log);     }     catch(Exception ex)     {         log.Info($"Exception found {ex.Message}");     } } 

The Functions runtime allows you to make your function async and return a Task.

In this case we can just await the call so we can handle exceptions normally.

like image 142
juunas Avatar answered Sep 23 '22 14:09

juunas