forked from Snipa22/nodejs-pool
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathfix_daemon.sh
More file actions
executable file
·240 lines (215 loc) · 5.3 KB
/
fix_daemon.sh
File metadata and controls
executable file
·240 lines (215 loc) · 5.3 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env bash
set -euo pipefail
dry_run=0
reason=""
port=""
xmr_height=""
expected_xmr_height=""
xtm_height=""
expected_xtm_height=""
lock_file="${FIX_DAEMON_LOCK:-/tmp/fix_daemon.lock}"
usage() {
cat <<'EOF'
Usage: fix_daemon.sh [--dry-run] <reason> [options]
Reasons:
xmr-lag restart monerod, relay-pool if present, and xtm_mm if present
proxy-unhealthy restart monerod, relay-pool if present, and xtm_mm if present
xtm-lag restart local xtm if present/enabled, relay-pool if present, and xtm_mm if present
template-stuck restart monerod, local xtm if present/enabled, relay-pool if present, and xtm_mm if present
Options:
--port <port>
--xmr-height <height>
--expected-xmr-height <height>
--xtm-height <height>
--expected-xtm-height <height>
EOF
}
while [ "$#" -gt 0 ]; do
case "$1" in
--dry-run)
dry_run=1
shift
;;
--port)
port="${2:-}"
shift 2
;;
--xmr-height)
xmr_height="${2:-}"
shift 2
;;
--expected-xmr-height)
expected_xmr_height="${2:-}"
shift 2
;;
--xtm-height)
xtm_height="${2:-}"
shift 2
;;
--expected-xtm-height)
expected_xtm_height="${2:-}"
shift 2
;;
-h|--help)
usage
exit 0
;;
--*)
echo "Unknown option: $1" >&2
usage >&2
exit 2
;;
*)
if [ -z "$reason" ]; then
if [[ "$1" =~ ^[0-9]+$ ]]; then
reason="template-stuck"
port="$1"
else
reason="$1"
fi
shift
else
echo "Unexpected argument: $1" >&2
usage >&2
exit 2
fi
;;
esac
done
reason="${reason:-template-stuck}"
exec 9>"$lock_file"
if ! flock -n 9; then
logger -t fix_daemon "skipping $reason recovery because another fix is running" 2>/dev/null || true
echo "fix_daemon: another recovery is already running"
exit 0
fi
log() {
logger -t fix_daemon "$*" 2>/dev/null || true
echo "fix_daemon: $*"
}
systemctl_cmd() {
if [ "$(id -u)" -eq 0 ]; then
systemctl "$@"
else
sudo -n systemctl "$@"
fi
}
service_exists() {
systemctl cat "$1" >/dev/null 2>&1
}
service_enabled() {
systemctl is-enabled "$1" >/dev/null 2>&1
}
run_service() {
local action="$1"
local unit="$2"
if [ "$dry_run" -eq 1 ]; then
log "DRY-RUN: systemctl $action $unit"
return 0
fi
log "systemctl $action $unit"
systemctl_cmd "$action" "$unit"
}
run_optional_service() {
local action="$1"
local unit="$2"
if [ "$dry_run" -eq 1 ]; then
log "DRY-RUN: systemctl $action $unit (if present)"
return 0
fi
if service_exists "$unit"; then
if [ "$action" != "stop" ] && ! service_enabled "$unit"; then
log "skipping $action $unit because the unit is disabled"
return 0
fi
run_service "$action" "$unit"
else
log "skipping $action $unit because the unit is not present"
fi
}
restart_relay_pool() {
run_optional_service restart relay-pool.service
}
run_xtm_mm_service() {
run_optional_service "$1" xtm_mm.service
}
wait_json_rpc() {
local name="$1"
local url="$2"
local payload="$3"
local pattern="$4"
local limit="${5:-30}"
if [ "$dry_run" -eq 1 ]; then
log "DRY-RUN: wait for $name RPC at $url"
return 0
fi
for _ in $(seq 1 "$limit"); do
local response
response="$(curl -m 2 -fsS "$url" -H "Content-Type: application/json" -d "$payload" 2>/dev/null || true)"
if grep -q "$pattern" <<<"$response"; then
log "$name RPC is reachable"
return 0
fi
sleep 1
done
log "$name RPC did not become reachable within ${limit}s"
return 1
}
wait_monero_rpc() {
wait_json_rpc \
"monerod" \
"http://127.0.0.1:18083/json_rpc" \
'{"jsonrpc":"2.0","id":"0","method":"get_info"}' \
'"status"[[:space:]]*:[[:space:]]*"OK"' \
30
}
wait_tari_rpc() {
wait_json_rpc \
"tari" \
"http://127.0.0.1:18146/json_rpc" \
'{"jsonrpc":"2.0","id":"0","method":"GetTipInfo","params":{}}' \
'"result"[[:space:]]*:' \
30
}
describe_context() {
local parts=()
[ -n "$port" ] && parts+=("port=$port")
[ -n "$xmr_height" ] && parts+=("xmr_height=$xmr_height")
[ -n "$expected_xmr_height" ] && parts+=("expected_xmr_height=$expected_xmr_height")
[ -n "$xtm_height" ] && parts+=("xtm_height=$xtm_height")
[ -n "$expected_xtm_height" ] && parts+=("expected_xtm_height=$expected_xtm_height")
if [ "${#parts[@]}" -gt 0 ]; then
printf ' (%s)' "${parts[*]}"
fi
}
log "starting $reason recovery$(describe_context)"
case "$reason" in
xmr-lag|proxy-unhealthy)
run_xtm_mm_service stop || true
run_service restart monero.service
restart_relay_pool
wait_monero_rpc || true
run_xtm_mm_service start
;;
xtm-lag)
run_xtm_mm_service stop || true
run_optional_service restart xtm.service
restart_relay_pool
if service_exists xtm.service || [ "$dry_run" -eq 1 ]; then
wait_tari_rpc || true
fi
run_xtm_mm_service start
;;
template-stuck|unknown|*)
run_xtm_mm_service stop || true
run_service restart monero.service
run_optional_service restart xtm.service
restart_relay_pool
wait_monero_rpc || true
if service_exists xtm.service || [ "$dry_run" -eq 1 ]; then
wait_tari_rpc || true
fi
run_xtm_mm_service start
;;
esac
log "completed $reason recovery"