add retry logic to Cloud Backup download
This commit is contained in:
@@ -916,16 +916,54 @@ Function Get-EmberkomCloudBackupRelease {
|
||||
|
||||
Function Save-EmberkomCloudBackupInstaller {
|
||||
param([Parameter(Mandatory=$true)]$Release, [Parameter(Mandatory=$true)][string]$Path)
|
||||
$client = New-Object Net.WebClient
|
||||
$ProgressPreference = 'SilentlyContinue'
|
||||
$sourceHost = ([uri]$Release.Url).Host
|
||||
$maxAttempts = 2
|
||||
$timeoutSeconds = 240
|
||||
Write-Host "MSP360 $($Release.Version) installer target: $Path (expected SHA-256 $($Release.SHA256))."
|
||||
try {
|
||||
$client.DownloadFile($Release.Url, $Path)
|
||||
for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) {
|
||||
$elapsed = [Diagnostics.Stopwatch]::StartNew()
|
||||
$utc = [datetime]::UtcNow.ToString('u')
|
||||
Write-Host "$utc $env:COMPUTERNAME - MSP360 installer download from $sourceHost (attempt $attempt of $maxAttempts, request timeout $timeoutSeconds seconds)."
|
||||
try {
|
||||
Invoke-WebRequest -UseBasicParsing -Uri $Release.Url -OutFile $Path -TimeoutSec $timeoutSeconds -ErrorAction Stop
|
||||
break
|
||||
} catch {
|
||||
$failure = $_.Exception
|
||||
while ($failure.InnerException -and $failure -isnot [Net.WebException]) { $failure = $failure.InnerException }
|
||||
$failureHost = $sourceHost
|
||||
if ($failure -is [Net.WebException]) {
|
||||
$reason = [string]$failure.Status
|
||||
$retry = $reason -in @('Timeout','NameResolutionFailure','ProxyNameResolutionFailure','ConnectFailure',
|
||||
'ConnectionClosed','KeepAliveFailure','SendFailure','ReceiveFailure','PipelineFailure')
|
||||
if ($failure.Response -is [Net.HttpWebResponse]) {
|
||||
$status = [int]$failure.Response.StatusCode
|
||||
$failureHost = $failure.Response.ResponseUri.Host
|
||||
$failure.Response.Close()
|
||||
$reason = "HTTP $status"
|
||||
$retry = $status -in @(408,429,500,502,503,504)
|
||||
}
|
||||
} elseif ($failure -is [Net.Sockets.SocketException]) {
|
||||
$reason = [string]$failure.SocketErrorCode
|
||||
$retry = $true
|
||||
} else { throw }
|
||||
$bytes = if (Test-Path -LiteralPath $Path) { (Get-Item -LiteralPath $Path).Length } else { 0 }
|
||||
$detail = "MSP360 download attempt $attempt of $maxAttempts from $failureHost failed ($reason) after $([int]$elapsed.Elapsed.TotalSeconds)s, $bytes bytes saved."
|
||||
if (!$retry -or $attempt -eq $maxAttempts) { throw $detail }
|
||||
Write-Host "$detail Retrying in 3 seconds."
|
||||
if (Test-Path -LiteralPath $Path) { Remove-Item -LiteralPath $Path -Force -ErrorAction Stop }
|
||||
Start-Sleep -Seconds 3
|
||||
}
|
||||
}
|
||||
if ((Get-FileHash -LiteralPath $Path -Algorithm SHA256 -ErrorAction Stop).Hash -ne $Release.SHA256) {
|
||||
throw 'The installer SHA-256 does not match the published release.'
|
||||
}
|
||||
Write-Host 'MSP360 installer download complete. SHA-256 verified.'
|
||||
} catch {
|
||||
if (Test-Path -LiteralPath $Path) { Remove-Item -LiteralPath $Path -Force -ErrorAction Stop }
|
||||
throw
|
||||
} finally { $client.Dispose() }
|
||||
}
|
||||
}
|
||||
|
||||
# Function to install the MSP360 Powershell module
|
||||
|
||||
Reference in New Issue
Block a user