Created
January 19, 2016 14:14
-
-
Save vaijab/570546d7516affbd44fc to your computer and use it in GitHub Desktop.
aws cli: retry on RequestLimitExceeded error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/bash | |
function retry() { | |
tries=0 | |
max_tries=3 | |
sleep_sec=1 | |
exit_code=256 | |
error='' | |
until { error=$(${@} 2>&1 1>&${stdout}); } {stdout}>&1; do | |
exit_code=$? | |
tries=$(( ${tries} + 1 )) | |
if [[ ${tries} -gt ${max_tries} ]]; then | |
exit ${exit_code} | |
fi | |
if [[ ${exit_code} == 255 ]] && (echo "${error}" | grep -q 'RequestLimitExceeded'); then | |
if [[ ${tries} != 1 ]]; then | |
sleep ${sleep_sec} | |
fi | |
sleep_sec=$((${sleep_sec} * 2)) | |
echo "${error}" >&2 | |
echo 'Being throttled. Retrying..' >&2 | |
else | |
echo "${error}" >&2 | |
exit ${exit_code} | |
fi | |
done | |
} | |
retry $@ |
Author
vaijab
commented
Jan 19, 2016
Nice, a couple of potential improvements though:
- quote the ${@} so that arguments with spaces in (e.g. aws ec2 create-tags --tags Key=type,Value="my type" ... ) will work
- when calling from a script, might not want to exit that script (i.e. let the caller deal with the aftermath of failure) - putting a subshell in here (i.e. wrap in parentheses) would help
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment