A follow-up to this older post. There I had an Azure Function that was calling another Azure Function like so:
1 |
Invoke-WebRequest -Uri $functionUrl -Headers $headers -Body ($Request.Body | ConvertTo-Json -Depth 5) -ContentType $contentType -UseBasicParsing -ErrorAction Stop |
Let’s call these Function A and Function B. Function A is called by a Logic App using a webhook. Function A then calls Function B via Invoke-WebRequest
after which it is supposed to exit. Function B will then call the Logic App’s callback URL once its done.
Occassionally though, Function A wouldn’t exit. Instead it would sort of hang around, causing the Logic App to call it again because it’s like “I haven’t heard from Function A, maybe something went wrong…” and so Function A calls Function B again. This would happen some 5-6 times.
I figured what I really want is for Function A to call Function B via Invoke-WebRequest
, but not wait for any sort of response. Not even a confirmation etc. How to do that though?
Enter Start-Job
. Or in the case of Azure Functions, Start-ThreadJob
.
I replaced the above code with the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$invokeWebParams = @{ "Uri" = $functionUrl "Headers" = $headers "Body" = ($RequestObj | ConvertTo-Json -Depth 5) "ContentType" = $contentType } $job = Start-ThreadJob -ScriptBlock { Invoke-WebRequest @Using:invokeWebParams -UseBasicParsing } Write-Host "Successfully invoked $functionUrl" while ($job.State -eq "NotStarted") { Write-Host "Waiting 10 seconds for the job to start" Start-Sleep -Seconds 10 } |
Now Function A triggers the Invoke-WebRequest. It waits until it is running, and then it exists as before via the following:
1 2 3 4 |
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ StatusCode = [HttpStatusCode]::OK Body = "Successfully invoked $functionUrl" }) |
That should do the trick I think. If not, you’ll find an update below when I learn more. :)