Skip to content

Commit e64e7a8

Browse files
committed
Move retry logic out to a utility
1 parent 01ea27e commit e64e7a8

File tree

3 files changed

+83
-46
lines changed

3 files changed

+83
-46
lines changed

.github/workflows/scripts/windows/install-vsb.ps1

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,19 @@
99
## See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
##
1111
##===----------------------------------------------------------------------===##
12+
13+
Import-Module $PSScriptRoot\web-request-utils.psm1
14+
1215
$VSB='https://download.visualstudio.microsoft.com/download/pr/5536698c-711c-4834-876f-2817d31a2ef2/c792bdb0fd46155de19955269cac85d52c4c63c23db2cf43d96b9390146f9390/vs_BuildTools.exe'
1316
$VSB_SHA256='C792BDB0FD46155DE19955269CAC85D52C4C63C23DB2CF43D96B9390146F9390'
1417
Set-Variable ErrorActionPreference Stop
1518
Set-Variable ProgressPreference SilentlyContinue
1619
Write-Host -NoNewLine ('Downloading {0} ... ' -f ${VSB})
17-
$MaxRetries = 10
18-
$BaseDelay = 1
19-
$Attempt = 0
20-
$Success = $false
21-
22-
while (-not $Success -and $Attempt -lt $MaxRetries) {
23-
$Attempt++
24-
try {
25-
Invoke-WebRequest -Uri $VSB -OutFile $env:TEMP\vs_buildtools.exe
26-
$Success = $true
27-
Write-Host 'SUCCESS'
28-
}
29-
catch {
30-
if ($Attempt -eq $MaxRetries) {
31-
Write-Host "FAILED after $MaxRetries attempts: $($_.Exception.Message)"
32-
exit 1
33-
}
34-
35-
# Calculate exponential backoff delay (2^attempt * base delay)
36-
$Delay = $BaseDelay * [Math]::Pow(2, $Attempt - 1)
37-
Write-Host "Attempt $Attempt failed, retrying in $Delay seconds..."
38-
Start-Sleep -Seconds $Delay
39-
}
20+
try {
21+
Invoke-WebRequestWithRetry -Uri $VSB -OutFile $env:TEMP\vs_buildtools.exe
22+
}
23+
catch {
24+
exit 1
4025
}
4126
Write-Host -NoNewLine ('Verifying SHA256 ({0}) ... ' -f $VSB_SHA256)
4227
$Hash = Get-FileHash $env:TEMP\vs_buildtools.exe -Algorithm sha256

.github/workflows/scripts/windows/swift/install-swift.ps1

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
## See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
##
1111
##===----------------------------------------------------------------------===##
12+
13+
Import-Module $PSScriptRoot\..\web-request-utils.psm1
14+
1215
function Install-Swift {
1316
param (
1417
[string]$Url,
@@ -17,29 +20,11 @@ function Install-Swift {
1720
Set-Variable ErrorActionPreference Stop
1821
Set-Variable ProgressPreference SilentlyContinue
1922
Write-Host -NoNewLine ('Downloading {0} ... ' -f $url)
20-
$MaxRetries = 10
21-
$BaseDelay = 1
22-
$Attempt = 0
23-
$Success = $false
24-
25-
while (-not $Success -and $Attempt -lt $MaxRetries) {
26-
$Attempt++
27-
try {
28-
Invoke-WebRequest -Uri $url -OutFile installer.exe
29-
$Success = $true
30-
Write-Host 'SUCCESS'
31-
}
32-
catch {
33-
if ($Attempt -eq $MaxRetries) {
34-
Write-Host "FAILED after $MaxRetries attempts: $($_.Exception.Message)"
35-
exit 1
36-
}
37-
38-
# Calculate exponential backoff delay (2^attempt * base delay)
39-
$Delay = $BaseDelay * [Math]::Pow(2, $Attempt - 1)
40-
Write-Host "Attempt $Attempt failed, retrying in $Delay seconds..."
41-
Start-Sleep -Seconds $Delay
42-
}
23+
try {
24+
Invoke-WebRequestWithRetry -Uri $url -OutFile installer.exe
25+
}
26+
catch {
27+
exit 1
4328
}
4429
Write-Host -NoNewLine ('Verifying SHA256 ({0}) ... ' -f $Sha256)
4530
$Hash = Get-FileHash installer.exe -Algorithm sha256
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# WebRequestUtils.psm1
2+
# Shared utilities for web requests with retry logic
3+
4+
<#
5+
.SYNOPSIS
6+
Invokes a web request with retry logic and exponential backoff.
7+
8+
.DESCRIPTION
9+
Attempts to download a file from a URL with automatic retry on failure.
10+
Uses exponential backoff to handle transient network failures.
11+
12+
.PARAMETER Uri
13+
The URL to download from.
14+
15+
.PARAMETER OutFile
16+
The destination file path for the download.
17+
18+
.PARAMETER MaxRetries
19+
Maximum number of retry attempts (default: 10).
20+
21+
.PARAMETER BaseDelay
22+
Base delay in seconds for exponential backoff (default: 1).
23+
24+
.EXAMPLE
25+
Invoke-WebRequestWithRetry -Uri "https://example.com/file.exe" -OutFile "file.exe"
26+
27+
.EXAMPLE
28+
Invoke-WebRequestWithRetry -Uri "https://example.com/file.exe" -OutFile "file.exe" -MaxRetries 5 -BaseDelay 2
29+
#>
30+
function Invoke-WebRequestWithRetry {
31+
param(
32+
[Parameter(Mandatory=$true)]
33+
[string]$Uri,
34+
35+
[Parameter(Mandatory=$true)]
36+
[string]$OutFile,
37+
38+
[int]$MaxRetries = 10,
39+
40+
[int]$BaseDelay = 1
41+
)
42+
43+
$Attempt = 0
44+
$Success = $false
45+
46+
while (-not $Success -and $Attempt -lt $MaxRetries) {
47+
$Attempt++
48+
try {
49+
Invoke-WebRequest -Uri $Uri -OutFile $OutFile
50+
$Success = $true
51+
Write-Host 'SUCCESS'
52+
}
53+
catch {
54+
if ($Attempt -eq $MaxRetries) {
55+
Write-Host "FAILED after $MaxRetries attempts: $($_.Exception.Message)"
56+
throw
57+
}
58+
59+
# Calculate exponential backoff delay (2^attempt * base delay)
60+
$Delay = $BaseDelay * [Math]::Pow(2, $Attempt - 1)
61+
Write-Host "Attempt $Attempt failed, retrying in $Delay seconds..."
62+
Start-Sleep -Seconds $Delay
63+
}
64+
}
65+
}
66+
67+
Export-ModuleMember -Function Invoke-WebRequestWithRetry

0 commit comments

Comments
 (0)