-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathbackoff_retry.sh
More file actions
executable file
·177 lines (158 loc) · 5.36 KB
/
backoff_retry.sh
File metadata and controls
executable file
·177 lines (158 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/bash
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
#
# == Description ==
#
# This script acts as a wrapper to run a specified command. If the command
# exits with a non-zero status (indicating failure), the script will wait
# for a calculated period and then retry the command.
#
# The delay between retries increases exponentially to avoid overwhelming
# a failing service (e.g., a database, web API) that might be
# temporarily unavailable.
#
# == Backoff Logic ==
#
# The delay is calculated using the formula:
# DELAY = BASE_DELAY * (2 ^ (current_attempt_number - 1))
#
# With default settings (BASE_DELAY=60s, ATTEMPTS=3):
# - Attempt 1 fails: Waits 60 * (2^0) = 60 seconds
# - Attempt 2 fails: Waits 60 * (2^1) = 120 seconds
# - Attempt 3 fails: Script gives up and exits with the command's final error code.
#
# == Usage ==
#
# ./backoff_retry.sh -c "<command_to_run>" [-a <num_attempts>] [-d <base_delay_sec>]
#
# == Options ==
#
# -c, --command The command string to execute (required).
# **Must be quoted** if it contains spaces, pipes, or
# other special characters.
#
# -a, --attempts The total number of times to try the command.
# Must be a positive integer. (Default: 3)
#
# -d, --base-delay The initial delay in seconds before the first retry.
# Must be a non-negative integer. (Default: 60)
#
# -h, --help Show this help message and exit.
#
# == Examples ==
#
# 1. Run a simple 'false' command (will fail 3 times by default):
# ./backoff_retry.sh -c "false"
#
# 2. Attempt to curl a potentially flaky API 5 times, starting with a 10s delay:
# ./backoff_retry.sh -c "curl -f http://localhost/status" -a 5 -d 10
#
# 3. Run a complex command with pipes and ensure it's properly quoted:
# ./backoff_retry.sh -c "ps aux | grep 'xpk' | grep -v 'grep'"
#
# ==============================================================================
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'
usage() {
echo -e "Usage: $0 -c \"<command>\" [-a <attempts>] [-d <base_delay>]" >&2
echo -e "\nOptions:" >&2
echo -e " -c, --command The command string to execute (required)." >&2
echo -e " -a, --attempts Number of attempts (default: 3)." >&2
echo -e " -d, --base-delay Base delay in seconds for backoff (default: 60)." >&2
echo -e " -h, --help Show this help message." >&2
}
ATTEMPTS=3
BASE_DELAY=60
COMMAND_STRING=""
if [ "$#" -eq 0 ]; then
echo -e "${RED}Error: No arguments provided.${NC}" >&2
usage
exit 1
fi
while [[ "$#" -gt 0 ]]; do
case "$1" in
-c|--command)
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
COMMAND_STRING="$2"
shift 2
else
echo -e "${RED}Error: --command requires an argument.${NC}" >&2
usage
exit 1
fi
;;
-a|--attempts)
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
ATTEMPTS="$2"
shift 2
else
echo -e "${RED}Error: --attempts requires an argument.${NC}" >&2
usage
exit 1
fi
;;
-d|--base-delay)
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
BASE_DELAY="$2"
shift 2
else
echo -e "${RED}Error: --base-delay requires an argument.${NC}" >&2
usage
exit 1
fi
;;
-h|--help)
usage
exit 0
;;
*)
echo -e "${RED}Error: Unknown option: $1${NC}" >&2
usage
exit 1
;;
esac
done
if [[ -z "$COMMAND_STRING" ]]; then
echo -e "${RED}Error: You must provide a command string with -c or --command.${NC}" >&2
usage
exit 1
fi
if ! [[ "$ATTEMPTS" =~ ^[1-9][0-9]*$ ]]; then
echo -e "${RED}Error: ATTEMPTS (-a) must be a positive integer.${NC}" >&2
exit 1
fi
if ! [[ "$BASE_DELAY" =~ ^[0-9]+$ ]]; then
echo -e "${RED}Error: BASE_DELAY (-d) must be a non-negative integer.${NC}" >&2
exit 1
fi
for (( i=1; i<=ATTEMPTS; i++ )); do
echo -e "${YELLOW}--- Attempt $i of $ATTEMPTS ---${NC}"
/bin/bash -c "$COMMAND_STRING"
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
echo -e "${GREEN}Command succeeded on attempt $i. Exiting.${NC}"
exit 0
fi
if [ $i -eq $ATTEMPTS ]; then
echo -e "${RED}Command failed after $ATTEMPTS attempts. Exiting with status $EXIT_CODE.${NC}"
exit $EXIT_CODE
fi
DELAY=$(( BASE_DELAY * (2 ** (i - 1)) ))
echo -e "${YELLOW}Command failed with status $EXIT_CODE. Waiting for $DELAY seconds before next attempt...${NC}"
sleep "$DELAY"
done