Skip to content

Commit 1c96401

Browse files
authored
Merge pull request #153 from martius-lab/fkloss/autorestart
Add autorestart.sh to example client
2 parents 44ffd0d + 1a2cb10 commit 1c96401

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

comprl-hockey-agent/README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,31 @@ export COMPRL_ACCESS_TOKEN=<YOUR ACCESS TOKEN>
2727
```
2828
Then just call
2929
```
30-
python3 ./run_client.p --args --agent=strong
30+
python3 ./run_client.py --args --agent=strong
3131
```
32+
33+
34+
## The Auto-Restart Wrapper Script
35+
36+
37+
`autostart.sh` is a wrapper script around `run_client.py`, that is meant to
38+
automatically restart the client in case of infrequent issues like connection
39+
hiccups or server restarts. To use it simply replace `python3 ./run_client.py`
40+
with `bash ./autorestart.sh` in the examples above. Example:
41+
```
42+
bash ./autorestart.sh --server-url <URL> --server-port <PORT> \
43+
--token <YOUR ACCESS TOKEN> \
44+
--args --agent=strong
45+
```
46+
47+
It keeps track of the number of restarts within a certain time window. It will
48+
stop if there are too many restarts (e.g. because an issue in the client
49+
itself). The length of the time window and the restart limit can be configured
50+
via variables at the top of the script.
51+
52+
To stop the script, you need to press Ctrl+C twice. Once to stop the client
53+
and then again to stop the wrapper script.
54+
55+
You can enable notifications about restarts on ntfy.sh by setting a topic name
56+
to `NTFY_TOPIC` at the top of the script. Once set, open https://ntfy.sh/TOPIC
57+
in your browser (or use the app on your phone) to get notified.

comprl-hockey-agent/autorestart.sh

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/bin/bash
2+
# Wrapper script for run_client.py that automatically restarts the client if it
3+
# terminates.
4+
# It keeps track of the number of restarts and aborts if there are too many
5+
# within a certain time window (see variables below).
6+
# Comprl server information can either be set as environment variables below or
7+
# be passed as arguments to this script (any arguments will simply be forwarded
8+
# to run_client.py).
9+
#
10+
# NOTE: If you want to stop the script, you need to press Ctrl+C twice. Once
11+
# to stop the client and then again to stop the wrapper script.
12+
13+
14+
# Keep track of restarts within this time window
15+
THRESHOLD_TIME_WINDOW=600 # 10 min
16+
# Threshold for the number of restarts in the time window
17+
THRESHOLD=10
18+
19+
# If you set a topic name here, restart notifications will be sent to
20+
# ntfy.sh/$NFTY_TOPIC (so you can more easily monitor if there are problems).
21+
NTFY_TOPIC=
22+
23+
# You may set the server information here, then you don't need to pass it as
24+
# argument.
25+
# export COMPRL_SERVER_URL=<URL>
26+
# export COMPRL_SERVER_PORT=<PORT>
27+
# export COMPRL_ACCESS_TOKEN=<YOUR ACCESS TOKEN>
28+
29+
# Array to hold timestamps of terminations
30+
termination_times=()
31+
32+
while true; do
33+
# Run the command foobar
34+
python3 ./run_client.py "$@"
35+
36+
# Get the current timestamp
37+
current_time=$(date +%s)
38+
39+
# Add the current timestamp to the termination times array
40+
termination_times+=("$current_time")
41+
42+
# Remove timestamps outside of the time window
43+
termination_times=($(for time in "${termination_times[@]}"; do
44+
if (( current_time - time <= ${THRESHOLD_TIME_WINDOW} )); then
45+
echo "$time"
46+
fi
47+
done))
48+
49+
# Check if the number of terminations exceeds the threshold
50+
if (( ${#termination_times[@]} > ${THRESHOLD} )); then
51+
echo
52+
echo "##############################################"
53+
echo "Restarted too many times within the last ${THRESHOLD_TIME_WINDOW} seconds."
54+
55+
if [ -n "${NTFY_TOPIC}" ]; then
56+
curl -H "Priority: urgent" -H "Tags: warning" \
57+
-d "Too many restarts within the last ${THRESHOLD_TIME_WINDOW}" \
58+
ntfy.sh/${NTFY_TOPIC}
59+
fi
60+
61+
exit 1
62+
fi
63+
64+
# Wait a bit before restarting. In case the client terminated due to a
65+
# server restart, it will take some time before the server is ready again.
66+
sleep 20
67+
68+
echo
69+
echo "##############################################"
70+
echo "# Restarting #"
71+
echo "##############################################"
72+
echo
73+
74+
if [ -n "${NTFY_TOPIC}" ]; then
75+
curl -d "Restarting" ntfy.sh/${NTFY_TOPIC}
76+
fi
77+
done
78+

0 commit comments

Comments
 (0)