[CI][VSTS] When webservers are rude, retry (#10411)

Perseverance is failing 19 times and succeeding the 20th.

fix: https://github.com/xamarin/maccore/issues/2361
This commit is contained in:
Manuel de la Pena 2021-01-14 12:34:42 -05:00 коммит произвёл GitHub
Родитель 05e28c3713
Коммит 4f3b942a78
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 43 добавлений и 4 удалений

Просмотреть файл

@ -1,3 +1,42 @@
<#
.SYNOPSIS
Simple retry block to workaround certain issues with the webservices that cannot handle the load.
.PARAMETER Request
The request to be performed and retried if failed.
.PARAMETER Retries
The number of times the we will retry to perform the request.
#>
function Invoke-Request {
param (
[scriptblock]
$Request,
[int]
$Retries=5
)
$count = 0
do {
try {
# that & is important, tells pwsh to execute the script block, else you simple returns the block itself
return & $Request
} catch {
if ($count -gt $Retries) {
# notify and throw
Write-Host "Could not perform request after $Retries attempts."
throw $_.Exception
} else {
$count = $count + 1
$seconds = 5 * $count
Write-Host "Error performing request trying in $seconds seconds"
Start-Sleep -Seconds $seconds
}
}
} while ($true)
}
<#
.SYNOPSIS
Returns the target url to be used when setting the status. The target url allows users to get back to the CI event that updated the status.
@ -114,7 +153,7 @@ function Set-GitHubStatus {
Authorization = ("token {0}" -f $Env:GITHUB_TOKEN)
}
return Invoke-RestMethod -Uri $url -Headers $headers -Method "POST" -Body ($payload | ConvertTo-json) -ContentType 'application/json'
return Invoke-Request -Request { Invoke-RestMethod -Uri $url -Headers $headers -Method "POST" -Body ($payload | ConvertTo-json) -ContentType 'application/json' }
}
<#
@ -213,7 +252,7 @@ function New-GitHubComment {
Authorization = ("token {0}" -f $Env:GITHUB_TOKEN)
}
$request = Invoke-RestMethod -Uri $url -Headers $headers -Method "POST" -Body ($payload | ConvertTo-Json) -ContentType 'application/json'
$request = Invoke-Request -Request { Invoke-RestMethod -Uri $url -Headers $headers -Method "POST" -Body ($payload | ConvertTo-Json) -ContentType 'application/json' }
Write-Host $request
return $request
}
@ -433,7 +472,7 @@ function Get-GitHubPRInfo {
Authorization = ("token {0}" -f $Env:GITHUB_TOKEN)
}
$request = Invoke-RestMethod -Uri $url -Headers $headers -Method "POST" -ContentType 'application/json'
$request = Invoke-Request -Request { Invoke-RestMethod -Uri $url -Headers $headers -Method "POST" -ContentType 'application/json' }
Write-Host $request
return $request
}
@ -562,7 +601,7 @@ function New-GistWithFiles {
Authorization = ("token {0}" -f $Env:GITHUB_TOKEN);
}
$request = Invoke-RestMethod -Uri $url -Headers $headers -Method "POST" -Body $payloadJson -ContentType 'application/json'
$request = Invoke-Request -Request { Invoke-RestMethod -Uri $url -Headers $headers -Method "POST" -Body $payloadJson -ContentType 'application/json' }
Write-Host $request
return $request.html_url
}