Browse Source

retry $RETRIES times before reporting "offline" state

Fabian Peter Hammerle 2 years ago
parent
commit
3db6b8f2a8
5 changed files with 17 additions and 4 deletions
  1. 3 0
      CHANGELOG.md
  2. 1 0
      Dockerfile
  3. 1 0
      README.md
  4. 1 0
      docker-compose.yml
  5. 11 4
      monitor.sh

+ 3 - 0
CHANGELOG.md

@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
 ## [Unreleased]
+### Added
+- configure number of retries before reporting "offline" state
+  via environment variable `RETRIES` (default: `0`)
 
 ## [2.0.0] - 2021-04-10
 ### Added

+ 1 - 0
Dockerfile

@@ -19,6 +19,7 @@ ENV TOR_HOST= \
     ONION_SERVICE_PORT= \
     TIMEOUT_SECONDS=4 \
     SLEEP_DURATION=16s \
+    RETRIES=0 \
     MAIL_TO= \
     VERBOSE=
 

+ 1 - 0
README.md

@@ -19,6 +19,7 @@ $ sudo docker run -d --network tor \
     -e TOR_HOST=tor_proxy -e TOR_PORT=9050 \
     -e ONION_SERVICE_HOST=change-me.onion \
     -e ONION_SERVICE_PORT=80 \
+    -e RETRIES=2 \
     -e MAIL_TO=me@example.com,someone@else.com \
     --name onion_service_monitor  \
     fphammerle/onion-service-status-mail

+ 1 - 0
docker-compose.yml

@@ -23,6 +23,7 @@ services:
       ONION_SERVICE_PORT: 80
       # TIMEOUT_SECONDS: 8
       # SLEEP_DURATION: 1m
+      # RETRIES: 4
       MAIL_TO: change.me@domain.tld, another@recipient.com
       #VERBOSE: 1
     volumes:

+ 11 - 4
monitor.sh

@@ -38,6 +38,7 @@ EOF
 )}
 
 last_state=""
+left_retries="$RETRIES"
 while : ; do
     if ! nc -z -w "$TIMEOUT_SECONDS" "$TOR_HOST" "$TOR_PORT"; then
         echo "failed to connect to tor proxy at $TOR_HOST:$TOR_PORT"
@@ -48,12 +49,18 @@ while : ; do
                 send_report online
             fi
             last_state="online"
+            left_retries=$RETRIES
         else
-            if [ "$last_state" == "online" ]; then
-                echo went offline
-                send_report offline
+            if [ $left_retries -le 0 ]; then
+                if [ "$last_state" == "online" ]; then
+                    echo went offline
+                    send_report offline
+                fi
+                last_state="offline"
+            else
+                left_retries=$((left_retries-1))
+                [ ! -z "$VERBOSE" ] && echo "$left_retries retry/ies left"
             fi
-            last_state="offline"
         fi
     fi
     # flush queue