github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/build/shlib.sh (about) 1 # Shell support functions and variables. Bash-only. 2 3 # Terminal color codes. 4 term_reset=$(tput sgr0 2>/dev/null || true) 5 term_red=$(tput setaf 1 2>/dev/null || true) 6 term_yellow=$(tput setaf 3 2>/dev/null || true) 7 8 # `retry COMMAND [ARGS...]` invokes COMMAND with the specified ARGS, retrying it 9 # if it fails with exponential backoff up to three times. 10 retry() { 11 n=3 12 for (( i = 0; i < $n; i++ )); do 13 if (( i > 0 )); then 14 wait=$((5 * 2**(i-1))) # 5s, 10s 15 echo "$term_yellow[$i/$n] $1 failed: retrying in ${wait}s$term_reset" 16 sleep "$wait" 17 fi 18 if "$@"; then 19 return 0 20 fi 21 done 22 echo "$term_red[$n/$n] $1 failed: giving up$term_reset" 23 return 1 24 } 25 26 # `die [ARGS...]` outputs its arguments to stderr, then exits the program with 27 # a failing exit code. 28 die() { 29 echo "$@" >&2 30 exit 1 31 }