1#!/bin/bash 2# Copyright 2020 Google LLC 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16function retry_with_backoff { 17 attempts_left=$1 18 sleep_seconds=$2 19 shift 2 20 command=$@ 21 22 23 # store current flag state 24 flags=$- 25 26 # allow a failures to continue 27 set +e 28 ${command} 29 exit_code=$? 30 31 # restore "e" flag 32 if [[ ${flags} =~ e ]] 33 then set -e 34 else set +e 35 fi 36 37 if [[ $exit_code == 0 ]] 38 then 39 return 0 40 fi 41 42 # failure 43 if [[ ${attempts_left} > 0 ]] 44 then 45 echo "failure (${exit_code}), sleeping ${sleep_seconds}..." 46 sleep ${sleep_seconds} 47 new_attempts=$((${attempts_left} - 1)) 48 new_sleep=$((${sleep_seconds} * 2)) 49 retry_with_backoff ${new_attempts} ${new_sleep} ${command} 50 fi 51 52 return $exit_code 53} 54 55## Helper functionss 56function now() { date +"%Y-%m-%d %H:%M:%S" | tr -d '\n'; } 57function msg() { println "$*" >&2; } 58function println() { printf '%s\n' "$(now) $*"; } 59 60## Helper comment to trigger updated repo dependency release