0
0
mirror of https://github.com/thegeeklab/retry.git synced 2024-11-24 22:30:39 +00:00

fixes some posix compatibility issues

This commit is contained in:
Robert Kaussow 2020-07-30 15:57:32 +02:00
parent 19ef4b5579
commit 8c440f34aa
No known key found for this signature in database
GPG Key ID: 65362AE74AF98B61

105
retry
View File

@ -1,5 +1,4 @@
#!/bin/sh #!/usr/bin/env sh
GETOPT_BIN=$IN_GETOPT_BIN GETOPT_BIN=$IN_GETOPT_BIN
GETOPT_BIN=${GETOPT_BIN:-getopt} GETOPT_BIN=${GETOPT_BIN:-getopt}
@ -9,7 +8,7 @@ __sleep_amount() {
else else
#TODO: check for awk #TODO: check for awk
#TODO: check if user would rather use one of the other possible dependencies: python, ruby, bc, dc #TODO: check if user would rather use one of the other possible dependencies: python, ruby, bc, dc
sleep_time=`awk "BEGIN {t = $min_sleep * $(( (1<<($attempts -1)) )); print (t > $max_sleep ? $max_sleep : t)}"` sleep_time=$(awk "BEGIN {t = $min_sleep * $(( (1<<(attempts -1)) )); print (t > $max_sleep ? $max_sleep : t)}")
fi fi
} }
@ -37,32 +36,32 @@ retry()
local return_code=1 local return_code=1
while [[ $return_code -ne 0 && $attempts -le $max_tries ]]; do while [ "$return_code" -ne 0 ] && [ "$attempts" -le "$max_tries" ]; do
if [ $attempts -gt 0 ]; then if [ $attempts -gt 0 ]; then
__sleep_amount __sleep_amount
__log_out "Before retry #$attempts: sleeping $sleep_time seconds" __log_out "Before retry #$attempts: sleeping $sleep_time seconds"
sleep $sleep_time sleep "$sleep_time"
fi fi
P="$1" P="$1"
for param in "${@:2}"; do P="$P '$param'"; done #for param in "${@:2}"; do P="$P '$param'"; done
#TODO: replace single quotes in each arg with '"'"' ? #TODO: replace single quotes in each arg with '"'"' ?
export RETRY_ATTEMPT=$attempts export RETRY_ATTEMPT=$attempts
bash -c "$P" sh -c "$P"
return_code=$? return_code=$?
#__log_out "Process returned $return_code on attempt $attempts" #__log_out "Process returned $return_code on attempt $attempts"
if [ $return_code -eq 127 ]; then if [ $return_code -eq 127 ]; then
# command not found # command not found
exit $return_code exit $return_code
elif [ $return_code -ne 0 ]; then elif [ $return_code -ne 0 ]; then
attempts=$[$attempts +1] attempts=$((attempts +1))
fi fi
done done
if [ $attempts -gt $max_tries ]; then if [ "$attempts" -gt "$max_tries" ]; then
if [ -n "$fail_script" ]; then if [ -n "$fail_script" ]; then
__log_out "Retries exhausted, running fail script" __log_out "Retries exhausted, running fail script"
eval $fail_script eval "$fail_script"
else else
__log_out "Retries exhausted" __log_out "Retries exhausted"
fi fi
@ -71,57 +70,55 @@ retry()
exit $return_code exit $return_code
} }
# If we're being sourced, don't worry about such things
if [ "$BASH_SOURCE" == "$0" ]; then help()
# Prints the help text {
help() local retry=$(basename "$0")
{ cat <<EOF
local retry=$(basename $0)
cat <<EOF
Usage: $retry [options] -- execute command Usage: $retry [options] -- execute command
-h, -?, --help -h, -?, --help
-v, --verbose Verbose output -v, --verbose Verbose output
-t, --tries=# Set max retries: Default 10 -t, --tries=# Set max retries: Default 10
-s, --sleep=secs Constant sleep amount (seconds) -s, --sleep=secs Constant sleep amount (seconds)
-m, --min=secs Exponential Backoff: minimum sleep amount (seconds): Default 0.3 -m, --min=secs Exponential Backoff: minimum sleep amount (seconds): Default 0.3
-x, --max=secs Exponential Backoff: maximum sleep amount (seconds): Default 60 -x, --max=secs Exponential Backoff: maximum sleep amount (seconds): Default 60
-f, --fail="script +cmds" Fail Script: run in case of final failure -f, --fail="script +cmds" Fail Script: run in case of final failure
EOF EOF
} }
# show help for no arguments if stdin is a terminal # show help for no arguments if stdin is a terminal
if { [ -z "$1" ] && [ -t 0 ] ; } || [ "$1" == '-h' ] || [ "$1" == '-?' ] || [ "$1" == '--help' ] if { [ -z "$1" ] && [ -t 0 ] ; } || [ "$1" = '-h' ] || [ "$1" = '-?' ] || [ "$1" = '--help' ]
then then
help help
exit 0 exit 0
fi fi
$GETOPT_BIN --test > /dev/null $GETOPT_BIN --test > /dev/null
if [[ $? -ne 4 ]]; then if [ $? -ne 4 ]; then
echo "Im sorry, 'getopt --test' failed in this environment. Please load GNU getopt." echo "Im sorry, 'getopt --test' failed in this environment. Please load GNU getopt."
exit 1 exit 1
fi fi
OPTIONS=vt:s:m:x:f: OPTIONS=vt:s:m:x:f:
LONGOPTIONS=verbose,tries:,sleep:,min:,max:,fail: LONGOPTIONS=verbose,tries:,sleep:,min:,max:,fail:
PARSED=$($GETOPT_BIN --options="$OPTIONS" --longoptions="$LONGOPTIONS" --name "$0" -- "$@") PARSED=$($GETOPT_BIN --options="$OPTIONS" --longoptions="$LONGOPTIONS" --name "$0" -- "$@")
if [[ $? -ne 0 ]]; then if [ $? -ne 0 ]; then
# e.g. $? == 1 # e.g. $? == 1
# then getopt has complained about wrong arguments to stdout # then getopt has complained about wrong arguments to stdout
exit 2 exit 2
fi fi
# read getopts output this way to handle the quoting right: # read getopts output this way to handle the quoting right:
eval set -- "$PARSED" eval set -- "$PARSED"
max_tries=10 max_tries=10
min_sleep=0.3 min_sleep=0.3
max_sleep=60.0 max_sleep=60.0
constant_sleep= constant_sleep=
fail_script= fail_script=
# now enjoy the options in order and nicely split until we see -- # now enjoy the options in order and nicely split until we see --
while true; do while true; do
case "$1" in case "$1" in
-v|--verbose) -v|--verbose)
VERBOSE=true VERBOSE=true
@ -156,8 +153,6 @@ EOF
exit 3 exit 3
;; ;;
esac esac
done done
retry "$max_tries" "$min_sleep" "$max_sleep" "$constant_sleep" "$fail_script" "$@" retry "$max_tries" "$min_sleep" "$max_sleep" "$constant_sleep" "$fail_script" "$@"
fi