Skip to content

Commit bae4c9b

Browse files
committed
csProjeHelper with retry
1 parent 8178deb commit bae4c9b

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

tools/CSProjHelper.ps1

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

4+
function Invoke-WithRetry {
5+
param(
6+
[Parameter(Mandatory = $true)]
7+
[scriptblock]$ScriptBlock,
8+
9+
[Parameter(Mandatory = $false)]
10+
[int]$MaxRetries = 5,
11+
12+
[Parameter(Mandatory = $false)]
13+
[int]$InitialDelayMs = 100,
14+
15+
[Parameter(Mandatory = $false)]
16+
[string]$OperationName = "Operation"
17+
)
18+
19+
for ($attempt = 1; $attempt -le $MaxRetries; $attempt++) {
20+
try {
21+
return & $ScriptBlock
22+
}
23+
catch {
24+
if ($attempt -eq $MaxRetries) {
25+
Write-Error "$OperationName failed after $MaxRetries attempts. Last error: $_"
26+
throw
27+
}
28+
29+
$delayMs = $InitialDelayMs * [Math]::Pow(2, $attempt - 1)
30+
Write-Warning "$OperationName failed (attempt $attempt/$MaxRetries). Retrying in $delayMs ms. Error: $_"
31+
Start-Sleep -Milliseconds $delayMs
32+
}
33+
}
34+
}
35+
436
function Set-CSProjValues(
537
[parameter(Mandatory = $true)][string] $ModuleCsProj,
638
[parameter(Mandatory = $true)][string] $ModuleVersion,

0 commit comments

Comments
 (0)