[macOS] Added HTTP status code check to download_with_retries (#3716)

* Disable exit on error temporary to implement retry logic based on exit code
* Check HTTP response code and retry if it's not 200
* Make variables local to not interfere with other scripts

Co-authored-by: Mikhail Timofeev <[email protected]>
This commit is contained in:
Darleev
2021-07-19 11:29:23 +03:00
committed by GitHub
co-authored by Mikhail Timofeev
parent 11a951ee60
commit 6576bc7f78
+14 -9
View File
@@ -10,24 +10,29 @@ download_with_retries() {
local COMPRESSED="$4" local COMPRESSED="$4"
if [[ $COMPRESSED == "compressed" ]]; then if [[ $COMPRESSED == "compressed" ]]; then
COMMAND="curl $URL -4 -sL --compressed -o '$DEST/$NAME'" local COMMAND="curl $URL -4 -sL --compressed -o '$DEST/$NAME' -w '%{http_code}'"
else else
COMMAND="curl $URL -4 -sL -o '$DEST/$NAME'" local COMMAND="curl $URL -4 -sL -o '$DEST/$NAME' -w '%{http_code}'"
fi fi
echo "Downloading $URL..." echo "Downloading '$URL' to '${DEST}/${NAME}'..."
retries=20 retries=20
interval=30 interval=30
while [ $retries -gt 0 ]; do while [ $retries -gt 0 ]; do
((retries--)) ((retries--))
eval $COMMAND # Temporary disable exit on error to retry on non-zero exit code
if [ $? != 0 ]; then set +e
echo "Unable to download $URL, next attempt in $interval sec, $retries attempts left" http_code=$(eval $COMMAND)
sleep $interval exit_code=$?
else if [ $http_code -eq 200 ] && [ $exit_code -eq 0 ]; then
echo "$URL was downloaded successfully to $DEST/$NAME" echo "Download completed"
return 0 return 0
else
echo "Error — Either HTTP response code for '$URL' is wrong - '$http_code' or exit code is not 0 - '$exit_code'. Waiting $interval seconds before the next attempt, $retries attempts left"
sleep 30
fi fi
# Enable exit on error back
set -e
done done
echo "Could not download $URL" echo "Could not download $URL"