Commit 22775d8e authored by Matt Kotsenas's avatar Matt Kotsenas Committed by LUCI CQ

Retry CIPD client download on Windows

Transient network conditions can cause the CIPD client download to fail,
so add a retry to the download.

As part of adding the retry, also extracted the download code to its own
function to ensure the WebClient is disposed promptly.

Bug: 1145741
Change-Id: I66bb5cde7ec4f637dd1161252c2b096af6abb8a8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2519730Reviewed-by: 's avatarVadim Shtayura <vadimsh@chromium.org>
Reviewed-by: 's avatarEdward Lesmes <ehmaldonado@chromium.org>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
parent b503f5ff
......@@ -86,6 +86,77 @@ function Get-Actual-SHA256($path) {
}
# Download a file to a particular path.
function Download-File {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]
$UserAgent,
[Parameter(Mandatory = $true)]
[string]
$Url,
[Parameter(Mandatory = $true)]
[string]
$Path
)
$wc = (New-Object System.Net.WebClient)
$wc.Headers.Add("User-Agent", $UserAgent)
try {
# Download failures were reported on Windows 8.1 without this line.
[System.Net.ServicePointManager]::SecurityProtocol = `
[System.Net.SecurityProtocolType]::Tls12
$wc.DownloadFile($Url, $Path)
}
catch {
$err = $_.Exception.Message
throw "Failed to download the file, check your network connection, $err"
}
finally {
$wc.Dispose()
}
}
# Retry a command with a delay between each.
function Retry-Command {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[scriptblock]
$Command,
[int]
$MaxAttempts = 3,
[timespan]
$Delay = (New-TimeSpan -Seconds 5)
)
$attempt = 0
while ($attempt -lt $MaxAttempts) {
try {
$Command.Invoke()
return
}
catch {
$attempt += 1
$exception = $_.Exception.InnerException
if ($attempt -lt $MaxAttempts) {
echo $exception.Message
echo "Retrying after a short nap..."
Start-Sleep -Seconds $Delay.TotalSeconds
} else {
throw $exception
}
}
}
}
$ExpectedSHA256 = Get-Expected-SHA256 $Platform
$Version = (Get-Content $VersionFile).Trim()
$URL = "$BackendURL/client?platform=$Platform&version=$Version"
......@@ -108,16 +179,8 @@ while ($CipdLockFile -eq $null) {
$TmpPath = $CipdBinary + ".tmp"
try {
echo "Downloading CIPD client for $Platform from $URL..."
$wc = (New-Object System.Net.WebClient)
$wc.Headers.Add("User-Agent", $UserAgent)
try {
# Download failures were reported on Windows 8.1 without this line.
[System.Net.ServicePointManager]::SecurityProtocol = `
[System.Net.SecurityProtocolType]::Tls12
$wc.DownloadFile($URL, $TmpPath)
} catch {
$err = $_.Exception.Message
throw "Failed to download the file, check your network connection, $err"
Retry-Command -Command {
Download-File -UserAgent $UserAgent -Url $URL -Path $TmpPath
}
$ActualSHA256 = Get-Actual-SHA256 $TmpPath
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment