Skip to content

Instantly share code, notes, and snippets.

@igorpopovio
Created August 17, 2015 12:55
Show Gist options
  • Save igorpopovio/5398f9f5a9926cd36dd9 to your computer and use it in GitHub Desktop.
Save igorpopovio/5398f9f5a9926cd36dd9 to your computer and use it in GitHub Desktop.
function Main() {
RetryCommand `
-maxRetries 5 `
-delayInSeconds 0 `
-command { cmd /c $SIGNTOOL sign /v /debug /sm /t $TIMESTAMP_URL $files }
RetryCommand `
-maxRetries 5 `
-delayInSeconds 3 `
-command { . $INSTALLSHIELD $arguments }
}
function RetryCommand($maxRetries, $delayInSeconds, $command) {
$textCommand = $ExecutionContext.InvokeCommand.ExpandString($command)
LogImportant "Calling `n`t $textCommand"
$failedTimes = 0
do {
& $command
$commandExitCode = $lastExitCode
$successful = $commandExitCode -eq 0
if (-not $successful) { $failedTimes++ }
$shouldRetry = -not $successful -and $failedTimes -le $maxRetries
if ($shouldRetry) {
$retriesLeft = $maxRetries - $failedTimes + 1
LogImportant "FAILED!!! Command: $textCommand"
LogImportant "We'll retry again after $delayInSeconds seconds ($retriesLeft/$maxRetries retries left)..."
Start-Sleep -Seconds $delayInSeconds
}
} while ($shouldRetry)
if (-not $successful) { Write-Error "FAILED!!! Command: $textCommand (exit code = $commandExitCode)" }
}
Main
@igorpopovio
Copy link
Author

Stuff to note: forcing string interpolation (variable substitution):

$ExecutionContext.InvokeCommand.ExpandString($command)

@igorpopovio
Copy link
Author

Other stuff: only $lastExitCode works in this case. $? won't work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment