diff options
author | Jakob Botsch Nielsen <Jakob.botsch.nielsen@gmail.com> | 2021-09-30 20:14:24 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-30 20:14:24 +0300 |
commit | 5e4bd4051b4872403c3138ad991141a627987620 (patch) | |
tree | 440a58fa2f962da4d13ae151be269574532f4db8 /eng/formatting | |
parent | 909cddfcd99e357189015432ac3c0b5a0d69306b (diff) |
Fix retry when downloading clang tools (#59806)
Invoke-WebRequest throws an exception when the download fails, so we
should use try-catch instead of status code to check failure. We can use
the generic Retry function from tools.ps1 to do this.
In addition pass -PassThru to avoid leaving a corrupted download file in
case the download fails. This will buffer the download and write it once
at the end.
Fix #57196
Diffstat (limited to 'eng/formatting')
-rw-r--r-- | eng/formatting/download-tools.ps1 | 34 |
1 files changed, 10 insertions, 24 deletions
diff --git a/eng/formatting/download-tools.ps1 b/eng/formatting/download-tools.ps1 index 36959c784f5..603a015c5e5 100644 --- a/eng/formatting/download-tools.ps1 +++ b/eng/formatting/download-tools.ps1 @@ -1,6 +1,6 @@ +. "$PSScriptRoot/../common/tools.ps1" # for Retry function -function DownloadClangTool -{ +function DownloadClangTool { param ( [string] $toolName, @@ -10,28 +10,14 @@ function DownloadClangTool $baseUri = "https://clrjit.blob.core.windows.net/clang-tools/windows" - if (-not $(ls $downloadOutputPath | Where-Object {$_.Name -eq "$toolName.exe"})) - { - $baseBackoffSeconds = 2; - - $success = $false - for ($i = 0; $i -lt 5; $i++) { - echo "Attempting download of '$baseUri/$toolName.exe'" - $status = Invoke-WebRequest -Uri "$baseUri/$toolName.exe" -OutFile $(Join-Path $downloadOutputPath -ChildPath "$toolName.exe") - if ($status.StatusCode -lt 400) - { - $success = $true - break - } else { - echo "Download attempt $($i+1) failed. Trying again in $($baseBackoffSeconds + $baseBackoffSeconds * $i) seconds" - Start-Sleep -Seconds $($baseBackoffSeconds + $baseBackoffSeconds * $i) - } - } - if (-not $success) - { - Write-Output "Failed to download clang-format" - return 1 - } + if (-not $(ls $downloadOutputPath | Where-Object { $_.Name -eq "$toolName.exe" })) { + + Retry({ + Write-Output "Downloading '$baseUri/$toolName.exe'" + # Pass -PassThru as otherwise Invoke-WebRequest leaves a corrupted file if the download fails. With -PassThru the download is buffered first. + # -UseBasicParsing is necessary for older PowerShells when Internet Explorer might not be installed/configured + $null = Invoke-WebRequest -Uri "$baseUri/$toolName.exe" -OutFile $(Join-Path $downloadOutputPath -ChildPath "$toolName.exe") -PassThru -UseBasicParsing + }) } } |