
1) Previous commit removed a shell function by mistake, that is being referenced by "clone-source". Restore this function. 2) Make sure "with_retries" works correctly when "set -e" is in effect Story: 2010226 Task: 48064 TESTS ========================== Run "clone-source" and make sure it works Signed-off-by: Davlet Panech <davlet.panech@windriver.com> Change-Id: I1bbf4bf041305860d2c8db83f5130f424084255c
105 lines
2.5 KiB
Bash
105 lines
2.5 KiB
Bash
# bash
|
|
|
|
#
|
|
# Copyright (c) 2022 Wind River Systems, Inc.
|
|
#
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
#
|
|
|
|
#
|
|
# Function to call a command, with support for retries
|
|
#
|
|
# with_retries [<options>] <retries> <cmd> [<cmd_args>...]
|
|
#
|
|
# Options:
|
|
# -d <secs> | --delay <secs>
|
|
# Wait given number of seconds between retries
|
|
# -t <secs> | --timeout <secs>
|
|
# Each iteration of the command runs under a timeout
|
|
# -k <secs> | --kill-timeout <secs>
|
|
# Each iteration of the command is killed violently
|
|
# if it doesn't exit voluntarily within the set time
|
|
# after the initial timeout signal.
|
|
#
|
|
function with_retries {
|
|
local delay=5
|
|
local max_time=0
|
|
local kill_time=0
|
|
local to_cmd=""
|
|
|
|
while [ $1 != "" ]; do
|
|
case "$1" in
|
|
-d | --delay)
|
|
delay=$2
|
|
shift 2
|
|
;;
|
|
-t | --timeout)
|
|
max_time=$2
|
|
shift 2
|
|
;;
|
|
-k | --kill-timeout)
|
|
kill_time=$2
|
|
shift 2
|
|
;;
|
|
*)
|
|
break
|
|
;;
|
|
esac
|
|
done
|
|
|
|
local max_attempts=$1
|
|
local cmd=$2
|
|
shift 2
|
|
|
|
if [ ${max_time} -gt 0 ]; then
|
|
to_cmd="timeout "
|
|
if [ ${kill_time} -gt 0 ]; then
|
|
to_cmd+="--kill-after=${kill_time} "
|
|
fi
|
|
to_cmd+="${max_time} "
|
|
fi
|
|
|
|
# Pop the first two arguments off the list,
|
|
# so we can pass additional args to the command safely
|
|
|
|
local -i attempt=0
|
|
local rc=0
|
|
|
|
while :; do
|
|
let ++attempt
|
|
|
|
echo "Running: ${cmd} $@" >&2
|
|
if ${to_cmd} ${cmd} "$@" ; then
|
|
return 0
|
|
else
|
|
rc=$?
|
|
fi
|
|
|
|
if [ $rc -eq 124 ]; then
|
|
echo "Command (${cmd}) timed out, attempt ${attempt} of ${max_attempts}." >&2
|
|
elif [ $rc -eq 137 ]; then
|
|
echo "Command (${cmd}) timed out and killed, attempt ${attempt} of ${max_attempts}." >&2
|
|
else
|
|
echo "Command (${cmd}) failed, attempt ${attempt} of ${max_attempts}." >&2
|
|
fi
|
|
|
|
if [ ${attempt} -lt ${max_attempts} ]; then
|
|
echo "Waiting ${delay} seconds before retrying..." >&2
|
|
sleep ${delay}
|
|
continue
|
|
else
|
|
echo "Max command attempts reached. Aborting..." >&2
|
|
return 1
|
|
fi
|
|
done
|
|
|
|
# unreachable
|
|
return 1
|
|
}
|
|
|
|
function with_default_retries {
|
|
local cmd=$1
|
|
shift 1
|
|
with_retries -d "${RETRY_INTERVAL_SEC:-1}" "${RETRIES:-1}" "${cmd}" "$@"
|
|
}
|