Skip to content

Commit

Permalink
Merge pull request #2 from mimi1vx/poo_138491
Browse files Browse the repository at this point in the history
Add check for return code of retried command
  • Loading branch information
mergify[bot] authored Nov 22, 2023
2 parents 0230dc5 + b104510 commit fbcdfe5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 6 additions & 1 deletion retry
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ opts=$(getopt \
--options h,r:,s:,e:: \
--longoptions help,retries:,sleep:,exponential:: \
--name "$(basename "$0")" -- "$@") || usage 1

eval set -- "$opts"

while [ $# -gt 0 ]; do
case "$1" in
-h | --help ) usage 0; shift ;;
Expand All @@ -35,11 +37,14 @@ done
retries="${retries:-3}"
sleep="${sleep:-3}"

ret=0
until "$@"; do
ret=$?
[ "$retries" -gt 0 ] || break
echo "Retrying up to $retries more times after sleeping ${sleep}s …" >&2
retries=$((retries-1))
sleep "$sleep"
[ -n "$exponential" ] && sleep=$((sleep*exponential))
done
[ $retries -gt 0 ]

[ $retries -gt 0 ] || [ $ret -eq 0 ]
6 changes: 5 additions & 1 deletion test/01-retry.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ PATH=$BASHLIB$PATH

source bash+ :std
use Test::More
plan tests 12
plan tests 14

PATH=$dir/..:$PATH

Expand All @@ -36,3 +36,7 @@ like "$out" 'sleeping 1s.*sleeping 2s' 'sleep amount doubles'
is $rc 1 'failing command returns no success'
set +e; out=$(retry -r 1 -- sh -c 'echo -n .; false' 2>/dev/null); set -e
is "$out" '..' 'specified number of tries (1+retries)'
set +e; out=$(retry -r 0 false 2>&1); rc=$?; set -e
is $rc 1 'failing command without retry returns no success'
set +e; out=$(retry -r 0 true 2>&1); rc=$?; set -e
is $rc 0 'passing command without retry returns no error'

0 comments on commit fbcdfe5

Please sign in to comment.