Search code examples
azurepowershellazure-functionsazure-logic-apps

How to Handle Long-Running Azure Function Callbacks in a Logic App Without Timing Out?


I am a bit lost on this and hope someone can point me in the right direction.

We have an Azure Logic App workflow that triggers several functions, some of which take 30 minutes to 5 hours to run. However, after 10 to 15 minutes, it will timeout because the functions are running too long and throw the following error message:

Http request failed: the server did not respond within the timeout limit. Please see logic app limits at https://aka.ms/logic-apps-limits-and-config#http-limits.

Now, as a workaround, I am trying to use a webhook to invoke the Azure Function via an HTTP request. Instead of continuously polling, it should remain idle until my function is done running and triggers a call to the callback URL.

What I did was create an HTTP webhook that triggers my Azure Function. This works since the function is being triggered, and when executing Write-Host, both tenantId and callbackUri are shown in the output.

Webhook

The tenantId and callbackUru are shown in the output:

Output

I tried following this link, but unfortunately, it's behind a Medium paywall: https://blog.devgenius.io/invoking-long-running-functions-from-logic-apps-powershell-edition-6caa96b95981

But after retrieving the callbackUri within the Function app, the next step according to the blog is to:

$response = $client.GetAsync($callbackUri)

However, this kept giving me the following error message:

ERROR: You cannot call a method on a null-valued expression.

So I tried somethings like:

Add-Type -AssemblyName System.Net.Http
$httpClient = New-Object System.Net.Http.HttpClient
$response = $httpClient.GetAsync($callbackUri)

When printing the response variable I get the following:

INFORMATION: System.Runtime.CompilerServices.AsyncTaskMethodBuilder1+AsyncStateMachineBox1[System.Net.Http.HttpResponseMessage,System.Net.Http.HttpClient+<<SendAsync>g__Core|83_0>d]

But since the Logic App is still timing out after 10-15 minutes, I guess it's still not working correctly.

At the end of my code, I have the following, which will send a response back to the callback URL. However, it never gets there since the Azure Logic App has already timed out before it reaches this statement.

Invoke-WebRequest -Method Post -Uri $callbackUri -ContentType 'application/json'

After a few days of struggling, I'm a bit lost on what else to try, so I was wondering if someone could point me in the right direction or if there is a better alternative for doing this.


Solution

  • How to Handle Long-Running Azure Function Callbacks in a Logic App Without Timing Out?

    Alternative of http webhook is making http request asynchronous as below:

    enter image description here

    enter image description here

    This will wait until it receives a 202 response.

    One other way is using 2 Logic Apps workflows, That is in 1st Workflow call the function app. Let it fail or succeed due to timeout. In Azure function add code to send the Data( the response of the function app or the output of function app) to service bus queue. In 2nd Logic app use the trigger When a message is received in a queue (auto-complete) . After this you can use the message that is the response of the Function app. So because function app is called the response is sent to queue and in 2nd logic app the response is taken as message from queue. Other than this Http Webhook is only the best way for Long running tasks.