Hey folks! I'm working on a webhook handler and aiming to respond quickly while handling heavy calculations in the background. Is this approach safe, or could Azure terminate my process after I send the response? Here's a snippet of my code:
function httpHandler() {
doExpensiveOperation() // runs on background
return {
status: 200
}
} Any insights would be appreciated!
5 Answers
You might want to check out durable functions for this. They’re designed to handle longer-running operations without risking a timeout.
Using Event Hub could be the right way to go. Queue all your requests to respond immediately, ideally with HTTP 202 Accepted, and let the Event Hub trigger your Function to handle processing afterward.
Consider posting to a queue and using a queue trigger for processing. HTTP triggers have a max timeout of around 230 seconds, so they're not ideal for long-running tasks.
A solid approach is to use an Azure queue. Send a message with a unique ID, and store that ID in a database with a status of 'in progress'. Then, your Azure Function can process that message later and update the status accordingly.
It's actually not safe to assume that heavy operations will run after responding. A good solution is to push requests to a storage queue. This way, processing can retry if it fails, and you avoid termination issues after sending a response.
Exactly! You could set up your HTTP trigger to create a queue item and return a task ID, then have a separate queue trigger function that processes it. You could also have a status HTTP trigger to check the task's state.
Just to clarify, Azure queues generally have a default timeout, and you will need to poll them for updates. But the SDK helps to smooth out those complexities!
Definitely! The HTTP async pattern works well here too. Just a heads up, if you're on the consumption plan, you have a max execution time of 10 minutes.