-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathapt-retry
More file actions
executable file
·42 lines (35 loc) · 1.05 KB
/
apt-retry
File metadata and controls
executable file
·42 lines (35 loc) · 1.05 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
#!/usr/bin/env bash
set -e
# Retry wrapper for apt commands to handle transient mirror failures.
# Uses decorrelated jitter: delay = rand(base, 3 * prev_delay), capped at max_delay
# Usage: apt-retry apt-get update && apt-retry apt-get install -y <packages>
RED='\033[0;31m'
NC='\033[0m' # No Color
max_attempts=5
base_delay=2
max_delay=20
# Decorrelated jitter: random value between base_delay and 3 * previous delay, capped
next_delay() {
local prev=$1
local range=$((3 * prev - base_delay + 1))
local next=$((base_delay + RANDOM % range))
if [ $next -gt $max_delay ]; then
next=$max_delay
fi
echo $next
}
delay=$base_delay
attempt=1
while [ $attempt -le $max_attempts ]; do
if "$@"; then
exit 0
fi
if [ $attempt -lt $max_attempts ]; then
echo -e "${RED}apt command failed (attempt $attempt/$max_attempts), retrying in ${delay}s...${NC}"
sleep $delay
fi
attempt=$((attempt + 1))
delay=$(next_delay $delay)
done
echo -e "${RED}apt command failed after $max_attempts attempts${NC}"
exit 1