Andreas Geissler | e9f6762 | 2024-04-24 15:38:24 +0200 | [diff] [blame^] | 1 | #!/bin/sh |
| 2 | |
| 3 | RETRY_INTERVAL=5 # Interval between retries in seconds |
| 4 | elapsed=0 # Elapsed time |
| 5 | |
| 6 | check_redis() { |
| 7 | host=$1 |
| 8 | port=$2 |
| 9 | while [ $elapsed -lt $TOTAL_RETRY_TIME ]; do |
| 10 | echo "Checking Redis at $host:$port... Elapsed time: ${elapsed}s" |
| 11 | if nc -z -w1 $TIMEOUT $host $port > /dev/null 2>&1; then |
| 12 | echo "Redis is up at $host:$port!" |
| 13 | return 0 |
| 14 | else |
| 15 | echo "Redis is down at $host:$port. Retrying in $RETRY_INTERVAL seconds." |
| 16 | sleep $RETRY_INTERVAL |
| 17 | elapsed=$((elapsed + RETRY_INTERVAL)) |
| 18 | fi |
| 19 | done |
| 20 | echo "Failed to connect to Redis at $host:$port after $TOTAL_RETRY_TIME seconds." |
| 21 | return 1 |
| 22 | } |
| 23 | |
| 24 | # For parsing and checking connections |
| 25 | parse_and_check() { |
| 26 | url=$1 |
| 27 | clean_url=${url#redis://} |
| 28 | host=$(echo $clean_url | cut -d':' -f1) |
| 29 | port=$(echo $clean_url | cut -d':' -f2) |
| 30 | check_redis $host $port |
| 31 | } |
| 32 | |
| 33 | # Main |
| 34 | if [ -n "$OAUTH2_PROXY_REDIS_CLUSTER_CONNECTION_URLS" ]; then |
| 35 | echo "Checking Redis in cluster mode..." |
| 36 | echo "$OAUTH2_PROXY_REDIS_CLUSTER_CONNECTION_URLS" | tr ',' '\n' | while read -r addr; do |
| 37 | parse_and_check $addr || exit 1 |
| 38 | done |
| 39 | elif [ -n "$OAUTH2_PROXY_REDIS_SENTINEL_CONNECTION_URLS" ]; then |
| 40 | echo "Checking Redis in sentinel mode..." |
| 41 | echo "$OAUTH2_PROXY_REDIS_SENTINEL_CONNECTION_URLS" | tr ',' '\n' | while read -r addr; do |
| 42 | parse_and_check $addr || exit 1 |
| 43 | done |
| 44 | elif [ -n "$OAUTH2_PROXY_REDIS_CONNECTION_URL" ]; then |
| 45 | echo "Checking standalone Redis..." |
| 46 | parse_and_check "$OAUTH2_PROXY_REDIS_CONNECTION_URL" || exit 1 |
| 47 | else |
| 48 | echo "Redis configuration not specified." |
| 49 | exit 1 |
| 50 | fi |
| 51 | |
| 52 | echo "Redis check completed." |