From 21cb90c04ac8759500afb0f68d24016922b35ea1 Mon Sep 17 00:00:00 2001 From: --unset <--unset> Date: Tue, 26 Nov 2024 10:21:48 +0100 Subject: [PATCH 1/4] Check file from local main --- build/scripts/GuardingV2ExtensionsHelper.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/scripts/GuardingV2ExtensionsHelper.psm1 b/build/scripts/GuardingV2ExtensionsHelper.psm1 index fc3b233cfd..5da0a5c2cf 100644 --- a/build/scripts/GuardingV2ExtensionsHelper.psm1 +++ b/build/scripts/GuardingV2ExtensionsHelper.psm1 @@ -259,7 +259,7 @@ function Test-IsStrictModeEnabled function Get-MaxAllowedObsoleteVersion() { git fetch origin main - $alGoSettings = $(git show origin/main:.github/AL-Go-Settings.json) | ConvertFrom-Json + $alGoSettings = $(git show main:.github/AL-Go-Settings.json) | ConvertFrom-Json if (-not $alGoSettings.repoVersion) { throw "Unable to find repoVersion in AL-Go-Settings.json" } From 597c2219f657e4f5ecdaf542dd9908779ed440ff Mon Sep 17 00:00:00 2001 From: Maria Zhelezova Date: Tue, 26 Nov 2024 16:21:11 +0100 Subject: [PATCH 2/4] Revert "Check file from local main" This reverts commit 21cb90c04ac8759500afb0f68d24016922b35ea1. --- build/scripts/GuardingV2ExtensionsHelper.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/scripts/GuardingV2ExtensionsHelper.psm1 b/build/scripts/GuardingV2ExtensionsHelper.psm1 index 5da0a5c2cf..fc3b233cfd 100644 --- a/build/scripts/GuardingV2ExtensionsHelper.psm1 +++ b/build/scripts/GuardingV2ExtensionsHelper.psm1 @@ -259,7 +259,7 @@ function Test-IsStrictModeEnabled function Get-MaxAllowedObsoleteVersion() { git fetch origin main - $alGoSettings = $(git show main:.github/AL-Go-Settings.json) | ConvertFrom-Json + $alGoSettings = $(git show origin/main:.github/AL-Go-Settings.json) | ConvertFrom-Json if (-not $alGoSettings.repoVersion) { throw "Unable to find repoVersion in AL-Go-Settings.json" } From 700b8cd33f67e2af98e30968a821d351500035f6 Mon Sep 17 00:00:00 2001 From: Maria Zhelezova Date: Tue, 26 Nov 2024 16:41:24 +0100 Subject: [PATCH 3/4] Add retries when running git fetch --- build/scripts/EnlistmentHelperFunctions.psm1 | 69 +++++++++++++++++++ build/scripts/GuardingV2ExtensionsHelper.psm1 | 6 +- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/build/scripts/EnlistmentHelperFunctions.psm1 b/build/scripts/EnlistmentHelperFunctions.psm1 index 3015c32a5a..2969606eb6 100644 --- a/build/scripts/EnlistmentHelperFunctions.psm1 +++ b/build/scripts/EnlistmentHelperFunctions.psm1 @@ -448,5 +448,74 @@ function RunAndCheck { } } +<# +.SYNOPSIS + Invokes a command with retry logic. +.DESCRIPTION + This function will invoke a command and retry it up to a specified number of times if it fails. + The function will sleep for an increasing amount of time between each retry. + The function will stop retrying if the maximum wait time is reached. +.PARAMETER ScriptBlock + The script block to invoke. +.PARAMETER RetryCount + The number of times to retry the command. +.PARAMETER MaxWaitTimeBeforeLastAttempt + The maximum time in seconds to wait before +.PARAMETER FirstDelay + The time in seconds to wait before the first retry. +.PARAMETER MaxWaitBetweenRetries + The maximum time in seconds to wait between retries. +#> +function Invoke-CommandWithRetry { + [CmdletBinding()] + param ( + [parameter(Mandatory = $true)] + [System.Management.Automation.ScriptBlock] $ScriptBlock, + [parameter(Mandatory = $false)] + [int] $RetryCount = 3, + [parameter(Mandatory = $false)] + [int] $MaxWaitTimeBeforeLastAttempt = 2 * 60 * 60, + [parameter(Mandatory = $false)] + [int] $FirstDelay = 60, + [parameter(Mandatory = $false)] + [ValidateRange(0, 60 * 60)] + [int] $MaxWaitBetweenRetries = 60 * 60 + ) + # Initialize the variables that will tell us when we should stop trying + $startTime = Get-Date + $retryNo = 0 + # Start trying... + $nextSleepTime = $FirstDelay + while ($true) { + $retryNo++ + try { + Invoke-Command -ScriptBlock $ScriptBlock -OutVariable output | Out-Null + return $output # Success! + } + catch [System.Exception] { + $exceptionMessage = $_.Exception.Message + $secondsSinceStart = ((Get-Date) - $startTime).TotalSeconds + + # Determine if we should keep trying + $tryAgain = $retryNo -lt $RetryCount -and $secondsSinceStart -lt $MaxWaitTimeBeforeLastAttempt + # Try again, or stop? + if ($tryAgain) { + # Sleep + $sleepTime = [System.Math]::Min($nextSleepTime, $MaxWaitTimeBeforeLastAttempt - $secondsSinceStart) # don't sleep beyond the max time + $sleepTime = [System.Math]::Min($sleepTime, $MaxWaitBetweenRetries) # don't sleep for more than one hour (and don't go above what Start-Sleep can handle (2147483)) + Write-Warning "Command failed with error '$exceptionMessage' in attempt no $retryNo after $secondsSinceStart seconds. Will retry up to $RetryCount times. Sleeping for $sleepTime seconds before trying again..." + Start-Sleep -Seconds $sleepTime + $nextSleepTime = 2 * $nextSleepTime # Next time sleep for longer + # Now try again + } + else { + # Failed! + $output | Write-Host + throw + } + } + } +} + Export-ModuleMember -Function *-* Export-ModuleMember -Function RunAndCheck diff --git a/build/scripts/GuardingV2ExtensionsHelper.psm1 b/build/scripts/GuardingV2ExtensionsHelper.psm1 index fc3b233cfd..417398edef 100644 --- a/build/scripts/GuardingV2ExtensionsHelper.psm1 +++ b/build/scripts/GuardingV2ExtensionsHelper.psm1 @@ -258,8 +258,10 @@ function Test-IsStrictModeEnabled } function Get-MaxAllowedObsoleteVersion() { - git fetch origin main - $alGoSettings = $(git show origin/main:.github/AL-Go-Settings.json) | ConvertFrom-Json + Import-Module -Name $PSScriptRoot\EnlistmentHelperFunctions.psm1 + + Invoke-CommandWithRetry -ScriptBlock { RunAndCheck git fetch origin main } + $alGoSettings = $(RunAndCheck git show origin/main:.github/AL-Go-Settings.json) | ConvertFrom-Json if (-not $alGoSettings.repoVersion) { throw "Unable to find repoVersion in AL-Go-Settings.json" } From 856d5929c76449643d5ce55507c4346771ed9429 Mon Sep 17 00:00:00 2001 From: Maria Zhelezova Date: Wed, 27 Nov 2024 09:55:24 +0100 Subject: [PATCH 4/4] Remove whitespace --- build/scripts/GuardingV2ExtensionsHelper.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/scripts/GuardingV2ExtensionsHelper.psm1 b/build/scripts/GuardingV2ExtensionsHelper.psm1 index 417398edef..0810b9149f 100644 --- a/build/scripts/GuardingV2ExtensionsHelper.psm1 +++ b/build/scripts/GuardingV2ExtensionsHelper.psm1 @@ -259,7 +259,7 @@ function Test-IsStrictModeEnabled function Get-MaxAllowedObsoleteVersion() { Import-Module -Name $PSScriptRoot\EnlistmentHelperFunctions.psm1 - + Invoke-CommandWithRetry -ScriptBlock { RunAndCheck git fetch origin main } $alGoSettings = $(RunAndCheck git show origin/main:.github/AL-Go-Settings.json) | ConvertFrom-Json if (-not $alGoSettings.repoVersion) {