Skip to content

Commit

Permalink
hack/bin/upload-image: Retry despite set -e (#2953)
Browse files Browse the repository at this point in the history
Executing `"$@"` within first argument of `if` prevents `set -e` from
immediately failing the whole script.

It could also be written as `while ! "$@"; do ...`, but then getting status of
`"$@"` is more complicated as `! "$@"` has status=0 and overwrites the value of
`$?`.
  • Loading branch information
akshaymankar authored Dec 28, 2022
1 parent 7a96592 commit 3f9c17e
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions hack/bin/upload-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,24 @@ function retry {
local maxAttempts=$1
local secondsDelay=1
local attemptCount=1
local output=
shift 1

while [ $attemptCount -le "$maxAttempts" ]; do
output=$("$@")
local status=$?

if [ $status -eq 0 ]; then
if "$@"; then
break
fi

if [ $attemptCount -lt "$maxAttempts" ]; then
echo "Command [$*] failed after attempt $attemptCount of $maxAttempts. Retrying in $secondsDelay second(s)." >&2
sleep $secondsDelay
elif [ $attemptCount -eq "$maxAttempts" ]; then
echo "Command [$*] failed after $attemptCount attempt(s)" >&2
return $status
else
local status=$?
if [ $attemptCount -lt "$maxAttempts" ]; then
echo "Command [$*] failed after attempt $attemptCount of $maxAttempts. Retrying in $secondsDelay second(s)." >&2
sleep $secondsDelay
elif [ $attemptCount -eq "$maxAttempts" ]; then
echo "Command [$*] failed after $attemptCount attempt(s)" >&2
return $status
fi
fi
attemptCount=$((attemptCount + 1))
secondsDelay=$((secondsDelay * 2))
done

echo "$output"
}

tmp_link_store=$(mktemp -d)
Expand Down

0 comments on commit 3f9c17e

Please sign in to comment.