|
| 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