As a continuation to a previous post where I was using Invoke-RestMethod to send some output to a Logic App’s callback URL I stumbled into an edge case wherein occassionally the callback would fail and rather than retry (because I was an idiot who forgot to put a try/ catch around it) the Function would fail.
Here’s an example error I get when calling the callback URL (this is not the entire message):
1 |
EXCEPTION: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (management.azure.com:443) |
I could add a try/ catch, but more importantly I want to keep retrying until it succeeds. Turns out Invoke-RestMethod
doesn’t give status code output so I had to resort to Invoke-WebRequest
instead and do the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Update the callback Url if present # Using Invoke-WebRequest here so I can get the status code and keep retrying until this succeeds. if ($RequestObj.callbackUrl.Length -ne 0) { $response = $null do { Write-Host "Pushing to the callback URL" try { $response = Invoke-WebRequest -Uri $RequestObj.callbackUrl -UseBasicParsing -Body ($functionOutput | ConvertTo-Json -Depth 5) -Method POST -ContentType $contentType } catch {} Start-Sleep -Seconds 60 } until ($response -and ($response.StatusCode -eq 200)) } |
Now the Function keeps trying until it gets a 200 OK.
I should probably add some code/ counter to stop trying after a while. Doesn’t matter in this case as the Function has a timeout of 30 mins, while the webhook action is set to timeout after 60 mins, so the Function will timeout before that.