Skip to content

Commit 56b9b2c

Browse files
authored
fix download timeout (#3514)
Downloading artifacts would hang forever some times. Co-authored-by: freddydk <[email protected]>
1 parent 5ff7909 commit 56b9b2c

File tree

4 files changed

+36
-115
lines changed

4 files changed

+36
-115
lines changed

Artifacts/Download-Artifacts.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function Download-Artifacts {
3030
[switch] $force,
3131
[switch] $forceRedirection,
3232
[string] $basePath = "",
33-
[int] $timeout = 300
33+
[int] $timeout = $bccontainerHelperConfig.artifactDownloadTimeout
3434
)
3535

3636
function DownloadPackage {

BC.HelperFunctions.ps1

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ function Get-ContainerHelperConfig {
3535
"useSharedEncryptionKeys" = $true
3636
"DOCKER_SCAN_SUGGEST" = $false
3737
"psSessionTimeout" = 0
38+
"artifactDownloadTimeout" = 300
39+
"defaultDownloadTimeout" = 100
3840
"baseUrl" = "https://businesscentral.dynamics.com"
3941
"apiBaseUrl" = "https://api.businesscentral.dynamics.com"
4042
"mapCountryCode" = [PSCustomObject]@{

Common/Download-File.ps1

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function Download-File {
2727
[string] $description = '',
2828
[hashtable] $headers = @{"UserAgent" = "BcContainerHelper $bcContainerHelperVersion" },
2929
[switch] $dontOverwrite,
30-
[int] $timeout = 100
30+
[int] $timeout = $bccontainerHelperConfig.defaultDownloadTimeout
3131
)
3232

3333
$replaceUrls = @{
@@ -86,6 +86,7 @@ function Download-File {
8686
}
8787
else {
8888
Write-Host "Could not download from CDN..., retrying from blob storage in $waittime seconds..."
89+
$timeout += $timeout
8990
}
9091
Start-Sleep -Seconds $waittime
9192
DownloadFileLow -sourceUrl $newSourceUrl -destinationFile $destinationFile -dontOverwrite:$dontOverwrite -timeout $timeout -headers $headers

HelperFunctions.ps1

+31-113
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,4 @@
1-
$useTimeOutWebClient = $false
2-
if ($PSVersionTable.PSVersion -lt "6.0.0" -or $useTimeOutWebClient) {
3-
$timeoutWebClientCode = @"
4-
using System.Net;
5-
6-
public class TimeoutWebClient : WebClient
7-
{
8-
int theTimeout;
9-
10-
public TimeoutWebClient(int timeout)
11-
{
12-
theTimeout = timeout;
13-
}
14-
15-
protected override WebRequest GetWebRequest(System.Uri address)
16-
{
17-
WebRequest request = base.GetWebRequest(address);
18-
if (request != null)
19-
{
20-
request.Timeout = theTimeout;
21-
}
22-
return request;
23-
}
24-
}
25-
"@;
26-
if (-not ([System.Management.Automation.PSTypeName]"TimeoutWebClient").Type) {
27-
Add-Type -TypeDefinition $timeoutWebClientCode -Language CSharp -WarningAction SilentlyContinue | Out-Null
28-
$useTimeOutWebClient = $true
29-
}
30-
}
31-
32-
$sslCallbackCode = @"
1+
$sslCallbackCode = @"
332
using System.Net.Security;
343
using System.Security.Cryptography.X509Certificates;
354
@@ -967,27 +936,6 @@ function GetHash {
967936
(Get-FileHash -InputStream $stream -Algorithm SHA256).Hash
968937
}
969938

970-
function Wait-Task {
971-
param(
972-
[Parameter(Mandatory, ValueFromPipeline)]
973-
[System.Threading.Tasks.Task[]]$Task
974-
)
975-
976-
Begin {
977-
$Tasks = @()
978-
}
979-
980-
Process {
981-
$Tasks += $Task
982-
}
983-
984-
End {
985-
While (-not [System.Threading.Tasks.Task]::WaitAll($Tasks, 200)) {}
986-
$Tasks.ForEach( { $_.GetAwaiter().GetResult() })
987-
}
988-
}
989-
Set-Alias -Name await -Value Wait-Task -Force
990-
991939
function DownloadFileLow {
992940
Param(
993941
[string] $sourceUrl,
@@ -999,71 +947,41 @@ function DownloadFileLow {
999947
[int] $timeout = 100
1000948
)
1001949

1002-
if ($useTimeOutWebClient) {
1003-
Write-Host "Downloading using WebClient"
1004-
if ($skipCertificateCheck) {
1005-
Write-Host "Disabling SSL Verification"
1006-
[SslVerification]::Disable()
1007-
}
1008-
$webClient = New-Object TimeoutWebClient -ArgumentList (1000 * $timeout)
1009-
$headers.Keys | ForEach-Object {
1010-
$webClient.Headers.Add($_, $headers."$_")
1011-
}
1012-
$webClient.UseDefaultCredentials = $useDefaultCredentials
1013-
if (Test-Path $destinationFile -PathType Leaf) {
1014-
if ($dontOverwrite) {
1015-
return
1016-
}
1017-
Remove-Item -Path $destinationFile -Force
1018-
}
1019-
try {
1020-
$webClient.DownloadFile($sourceUrl, $destinationFile)
1021-
}
1022-
finally {
1023-
$webClient.Dispose()
1024-
if ($skipCertificateCheck) {
1025-
Write-Host "Restoring SSL Verification"
1026-
[SslVerification]::Enable()
1027-
}
1028-
}
950+
$handler = New-Object System.Net.Http.HttpClientHandler
951+
if ($skipCertificateCheck) {
952+
Write-Host "Disabling SSL Verification on HttpClient"
953+
[SslVerification]::DisableSsl($handler)
954+
}
955+
if ($useDefaultCredentials) {
956+
$handler.UseDefaultCredentials = $true
957+
}
958+
$httpClient = New-Object System.Net.Http.HttpClient -ArgumentList $handler
959+
$httpClient.Timeout = [Timespan]::FromSeconds($timeout)
960+
$headers.Keys | ForEach-Object {
961+
$httpClient.DefaultRequestHeaders.Add($_, $headers."$_")
962+
}
963+
$stream = $null
964+
$fileStream = $null
965+
if ($dontOverwrite) {
966+
$fileMode = [System.IO.FileMode]::CreateNew
1029967
}
1030968
else {
1031-
Write-Host "Downloading using HttpClient"
1032-
1033-
$handler = New-Object System.Net.Http.HttpClientHandler
1034-
if ($skipCertificateCheck) {
1035-
Write-Host "Disabling SSL Verification on HttpClient"
1036-
[SslVerification]::DisableSsl($handler)
1037-
}
1038-
if ($useDefaultCredentials) {
1039-
$handler.UseDefaultCredentials = $true
1040-
}
1041-
$httpClient = New-Object System.Net.Http.HttpClient -ArgumentList $handler
1042-
$httpClient.Timeout = [Timespan]::FromSeconds($timeout)
1043-
$headers.Keys | ForEach-Object {
1044-
$httpClient.DefaultRequestHeaders.Add($_, $headers."$_")
1045-
}
1046-
$stream = $null
1047-
$fileStream = $null
1048-
if ($dontOverwrite) {
1049-
$fileMode = [System.IO.FileMode]::CreateNew
1050-
}
1051-
else {
1052-
$fileMode = [System.IO.FileMode]::Create
969+
$fileMode = [System.IO.FileMode]::Create
970+
}
971+
try {
972+
$stream = $httpClient.GetStreamAsync($sourceUrl).GetAwaiter().GetResult()
973+
$fileStream = New-Object System.IO.Filestream($destinationFile, $fileMode)
974+
if (-not $stream.CopyToAsync($fileStream).Wait($timeout * 1000)) {
975+
throw "Timeout downloading file"
1053976
}
1054-
try {
1055-
$stream = $httpClient.GetStreamAsync($sourceUrl).GetAwaiter().GetResult()
1056-
$fileStream = New-Object System.IO.Filestream($destinationFile, $fileMode)
1057-
$stream.CopyToAsync($fileStream).GetAwaiter().GetResult() | Out-Null
977+
}
978+
finally {
979+
if ($fileStream) {
1058980
$fileStream.Close()
981+
$fileStream.Dispose()
1059982
}
1060-
finally {
1061-
if ($fileStream) {
1062-
$fileStream.Dispose()
1063-
}
1064-
if ($stream) {
1065-
$stream.Dispose()
1066-
}
983+
if ($stream) {
984+
$stream.Dispose()
1067985
}
1068986
}
1069987
}

0 commit comments

Comments
 (0)