-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathentrypoint.sh
More file actions
90 lines (75 loc) · 2.31 KB
/
entrypoint.sh
File metadata and controls
90 lines (75 loc) · 2.31 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
#!/bin/bash
set -e
CONFIG="${ZERDA_CONFIG:-/root/.zerda/zerda.toml}"
BACKUP="${CONFIG}.bak"
PACKAGES_FILE="/root/.zerda/packages.txt"
CRASH_THRESHOLD=15
MAX_BACKOFF=60
MAX_RESTARTS=20
STABLE_RUNTIME=300
backoff=1
restart_count=0
if [ -f "$PACKAGES_FILE" ] && [ -s "$PACKAGES_FILE" ]; then
echo "[supervisor] Restoring packages from $PACKAGES_FILE ..."
sudo -u builder paru -S --needed --noconfirm $(cat "$PACKAGES_FILE" | tr '\n' ' ') || true
echo "[supervisor] Package restoration complete."
fi
BUNDLED_DOCS="/usr/local/share/zerda/docs/zerda"
USER_DOCS="/root/.zerda/docs/zerda"
if [ -d "$BUNDLED_DOCS" ] && [ ! -d "$USER_DOCS" ]; then
mkdir -p "$(dirname "$USER_DOCS")"
cp -r "$BUNDLED_DOCS" "$USER_DOCS"
echo "[supervisor] Installed bundled docs: zerda"
fi
child_pid=0
forward_signal() {
if [ "$child_pid" -ne 0 ]; then
kill -TERM "$child_pid" 2>/dev/null
wait "$child_pid" 2>/dev/null
fi
exit 0
}
trap forward_signal SIGTERM SIGINT
while true; do
if [ -f "$CONFIG" ]; then
cp "$CONFIG" "$BACKUP"
echo "[supervisor] Backed up config to ${BACKUP}"
fi
start_time=$(date +%s)
echo "[supervisor] Starting zerda $@ ..."
zerda "$@" &
child_pid=$!
wait "$child_pid" 2>/dev/null
exit_code=$?
child_pid=0
end_time=$(date +%s)
runtime=$((end_time - start_time))
if [ "$exit_code" -eq 0 ]; then
echo "[supervisor] zerda exited normally."
exit 0
fi
if [ "$runtime" -ge "$STABLE_RUNTIME" ]; then
restart_count=0
fi
restart_count=$((restart_count + 1))
if [ "$restart_count" -ge "$MAX_RESTARTS" ]; then
echo "[supervisor] Reached max restarts ($MAX_RESTARTS). Giving up."
exit 1
fi
if [ "$runtime" -lt "$CRASH_THRESHOLD" ]; then
echo "[supervisor] zerda crashed after ${runtime}s (exit ${exit_code}). Likely bad config."
if [ -f "$BACKUP" ]; then
cp "$BACKUP" "$CONFIG"
echo "[supervisor] Rolled back config from ${BACKUP}"
fi
sleep 2
backoff=1
continue
fi
echo "[supervisor] zerda exited with code ${exit_code} after ${runtime}s. Restarting in ${backoff}s..."
sleep "$backoff"
backoff=$((backoff * 2))
if [ "$backoff" -gt "$MAX_BACKOFF" ]; then
backoff=$MAX_BACKOFF
fi
done