Skip to content

Commit a6004e3

Browse files
Gabriel Ionescualindima
Gabriel Ionescu
authored andcommitted
devtool: add retry loop for docker pull
Added a mechanism of retrying commands and wrapped the "docker pull" command in it. The reason for doing this is that occasionally a docker pull command may be rate limited by ECR. Signed-off-by: Gabriel Ionescu <[email protected]>
1 parent b4376a9 commit a6004e3

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

tools/devtool

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,38 @@ ensure_docker() {
192192
"https://docs.docker.com/install/linux/linux-postinstall/"
193193
}
194194

195+
# Run a command and retry multiple times if it fails. Once it stops
196+
# failing return to normal execution. If there are "retry count"
197+
# failures, set the last error code.
198+
# $1 - command
199+
# $2 - retry count
200+
# $3 - sleep interval between retries
201+
retry_cmd() {
202+
command=$1
203+
retry_cnt=$2
204+
sleep_int=$3
205+
206+
{
207+
$command
208+
} || {
209+
# Save error code
210+
err_code=$?
211+
212+
# Command failed, substract one from retry_cnt
213+
retry_cnt=$((retry_cnt - 1))
214+
215+
# If retry_cnt is larger than 0, sleep and call again
216+
if [ "$retry_cnt" -gt 0 ]; then
217+
echo "$command failed, retrying..."
218+
sleep "$sleep_int"
219+
retry_cmd "$command" "$retry_cnt" "$sleep_int"
220+
fi
221+
222+
# Set what error code docker returned
223+
$(exit "$err_code")
224+
}
225+
}
226+
195227
# Attempt to download our Docker image. Exit if that fails.
196228
# Upon returning from this call, the caller can be certain our Docker image is
197229
# available on this system.
@@ -207,7 +239,9 @@ ensure_devctr() {
207239
say "About to pull docker image $DEVCTR_IMAGE"
208240
get_user_confirmation || die "Aborted."
209241

210-
docker pull "${DEVCTR_IMAGE}"
242+
# Run docker pull 5 times in case it fails - sleep 3 seconds
243+
# between attempts
244+
retry_cmd "docker pull $DEVCTR_IMAGE" 5 3
211245

212246
ok_or_die "Error pulling docker image. Aborting."
213247
}

0 commit comments

Comments
 (0)