Skip to content

Commit

Permalink
Extract retry function in a file
Browse files Browse the repository at this point in the history
  • Loading branch information
pquentin committed Sep 7, 2023
1 parent 49ddac8 commit 4f5e50f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 93 deletions.
33 changes: 2 additions & 31 deletions .buildkite/it/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

set -eo pipefail

source .buildkite/retry.sh

export TERM=dumb
export LC_ALL=en_US.UTF-8
export TZ=Etc/UTC
Expand All @@ -10,37 +12,6 @@ export DEBIAN_FRONTEND=noninteractive
sudo mkdir -p /etc/needrestart
echo "\$nrconf{restart} = 'a';" | sudo tee -a /etc/needrestart/needrestart.conf > /dev/null

# based on https://gist.github.com/sj26/88e1c6584397bb7c13bd11108a579746?permalink_comment_id=4155247#gistcomment-4155247
function retry {
local retries=$1
shift
local cmd=($@)
local cmd_string="${@}"
local count=0

# be lenient with non-zero exit codes, to allow retries
set +o errexit
set +o pipefail
until "${cmd[@]}"; do
retcode=$?
wait=$(( 2 ** count ))
count=$(( count + 1))
if [[ $count -le $retries ]]; then
printf "Command [%s] failed. Retry [%d/%d] in [%d] seconds.\n" "$cmd_string" $count $retries $wait
sleep $wait
else
printf "Exhausted all [%s] retries for command [%s]. Exiting.\n" "$cmd_string" $retries
# restore settings to fail immediately on error
set -o errexit
set -o pipefail
return $retcode
fi
done
# restore settings to fail immediately on error
set -o errexit
set -o pipefail
return 0
}
echo "--- System dependencies"

PYTHON_VERSION="$1"
Expand Down
33 changes: 2 additions & 31 deletions .buildkite/it/run_serverless.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

set -eo pipefail

source .buildkite/retry.sh

export TERM=dumb
export LC_ALL=en_US.UTF-8
export TZ=Etc/UTC
Expand All @@ -10,37 +12,6 @@ export DEBIAN_FRONTEND=noninteractive
sudo mkdir -p /etc/needrestart
echo "\$nrconf{restart} = 'a';" | sudo tee -a /etc/needrestart/needrestart.conf > /dev/null

# based on https://gist.github.com/sj26/88e1c6584397bb7c13bd11108a579746?permalink_comment_id=4155247#gistcomment-4155247
function retry {
local retries=$1
shift
local cmd=($@)
local cmd_string="${@}"
local count=0

# be lenient with non-zero exit codes, to allow retries
set +o errexit
set +o pipefail
until "${cmd[@]}"; do
retcode=$?
wait=$(( 2 ** count ))
count=$(( count + 1))
if [[ $count -le $retries ]]; then
printf "Command [%s] failed. Retry [%d/%d] in [%d] seconds.\n" "$cmd_string" $count $retries $wait
sleep $wait
else
printf "Exhausted all [%s] retries for command [%s]. Exiting.\n" "$cmd_string" $retries
# restore settings to fail immediately on error
set -o errexit
set -o pipefail
return $retcode
fi
done
# restore settings to fail immediately on error
set -o errexit
set -o pipefail
return 0
}
echo "--- System dependencies"

PYTHON_VERSION="$1"
Expand Down
32 changes: 1 addition & 31 deletions .buildkite/release-docker/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,7 @@

set -eo pipefail

# based on https://gist.github.com/sj26/88e1c6584397bb7c13bd11108a579746?permalink_comment_id=4155247#gistcomment-4155247
function retry {
local retries=$1
shift
local cmd=($@)
local cmd_string="${@}"
local count=0

# be lenient with non-zero exit codes, to allow retries
set +o errexit
set +o pipefail
until "${cmd[@]}"; do
retcode=$?
wait=$(( 2 ** count ))
count=$(( count + 1))
if [[ $count -le $retries ]]; then
printf "Command [%s] failed. Retry [%d/%d] in [%d] seconds.\n" "$cmd_string" $count $retries $wait
sleep $wait
else
printf "Exhausted all [%s] retries for command [%s]. Exiting.\n" "$cmd_string" $retries
# restore settings to fail immediately on error
set -o errexit
set -o pipefail
return $retcode
fi
done
# restore settings to fail immediately on error
set -o errexit
set -o pipefail
return 0
}
source .buildkite/retry.sh

set +x
RELEASE_VERSION=$(buildkite-agent meta-data get RELEASE_VERSION)
Expand Down
31 changes: 31 additions & 0 deletions .buildkite/retry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# based on https://gist.github.com/sj26/88e1c6584397bb7c13bd11108a579746?permalink_comment_id=4155247#gistcomment-4155247
function retry {
local retries=$1
shift
local cmd=($@)
local cmd_string="${@}"
local count=0

# be lenient with non-zero exit codes, to allow retries
set +o errexit
set +o pipefail
until "${cmd[@]}"; do
retcode=$?
wait=$(( 2 ** count ))
count=$(( count + 1))
if [[ $count -le $retries ]]; then
printf "Command [%s] failed. Retry [%d/%d] in [%d] seconds.\n" "$cmd_string" $count $retries $wait
sleep $wait
else
printf "Exhausted all [%s] retries for command [%s]. Exiting.\n" "$cmd_string" $retries
# restore settings to fail immediately on error
set -o errexit
set -o pipefail
return $retcode
fi
done
# restore settings to fail immediately on error
set -o errexit
set -o pipefail
return 0
}

0 comments on commit 4f5e50f

Please sign in to comment.