Skip to content

Commit c7439dd

Browse files
committed
(tasks) Fix with_retries_if() for failed commands
The previous incarnation of with_retries_if() had a bug in that set -e would cause it to exit immediately when the exec_and_capture call returned non-zero, so it would never go on to retry. Added a global variable for the last run status and performing the exec_and_capture in the if clause now so the return status informs the branching instead, and we capture the status value separately. Also did a bit of reworking to not trail a 'Retrying...' message and delay after a final failed execution. And loops on 1 to retry now, since that's what we need for output.
1 parent d224f66 commit c7439dd

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

files/common.sh

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,25 @@ exists() {
4545
# Log and execute a command in a subshell, capturing and echoing its
4646
# output before returning its exit status.
4747
#
48-
# Output is also captured in the variable EXEC_AND_CAPTURE_RESULT
49-
# for other functions to inspect.
48+
# Also captures output and status in the global variables:
49+
#
50+
# LAST_EXEC_AND_CAPTURE_OUTPUT
51+
# LAST_EXEC_AND_CAPTURE_STATUS
52+
#
53+
# so that the caller can inspect them as well.
5054
exec_and_capture() {
5155
local _cmd="$*"
5256

5357
info "Executing: ${_cmd}"
5458

5559
set +e
56-
EXEC_AND_CAPTURE_RESULT=$(${_cmd} 2>&1)
57-
local _status=$?
60+
LAST_EXEC_AND_CAPTURE_OUTPUT=$(${_cmd} 2>&1)
61+
LAST_EXEC_AND_CAPTURE_STATUS=$?
5862
set -e
5963

60-
echo "${EXEC_AND_CAPTURE_RESULT}"
61-
info "Status: ${_status}"
62-
return $_status
64+
echo "${LAST_EXEC_AND_CAPTURE_OUTPUT}"
65+
info "Status: ${LAST_EXEC_AND_CAPTURE_STATUS}"
66+
return $LAST_EXEC_AND_CAPTURE_STATUS
6367
}
6468

6569
# If the passed command fails with output matching the given regex,
@@ -82,19 +86,24 @@ with_retries_if() {
8286
shift 3
8387

8488
local _cmd="$*"
89+
local _result
8590
local _status
8691

87-
for ((i = 0; i < _retries; i++)); do
88-
info "Attempt $((i + 1)) of $_retries: ${_cmd}"
89-
exec_and_capture "${_cmd}"
90-
_status=$?
91-
if [[ "${_status}" == 0 ]]; then
92+
for ((i = 1; i <= _retries; i++)); do
93+
if [[ $i -gt 1 ]]; then
94+
info "Retrying in ${_delay} seconds..."
95+
sleep "${_delay}"
96+
fi
97+
98+
info "Attempt ${i} of $_retries: ${_cmd}"
99+
100+
if exec_and_capture "${_cmd}"; then
101+
_status=$LAST_EXEC_AND_CAPTURE_STATUS
92102
break # command succeeded
93103
else
94-
if [[ "${EXEC_AND_CAPTURE_OUTPUT}" =~ ${_error_regex} ]]; then
95-
info "Retrying in ${_delay} seconds..."
96-
sleep "${_delay}"
97-
else
104+
_result="${LAST_EXEC_AND_CAPTURE_OUTPUT}"
105+
_status=$LAST_EXEC_AND_CAPTURE_STATUS
106+
if ! [[ "${_result}" =~ ${_error_regex} ]]; then
98107
info "Command failed but output did not match /${_error_regex}/. Aborting retries."
99108
break
100109
fi

0 commit comments

Comments
 (0)