#!/usr/bin/env bash set -euo pipefail : "${AIGW_READY_URL:?AIGW_READY_URL is required}" : "${AIGW_REDIS_CONTAINER:?AIGW_REDIS_CONTAINER is required}" timeout_seconds="${AIGW_REDIS_DRILL_TIMEOUT_SECONDS:-60}" redis_stopped=false cleanup() { if [ "$redis_stopped" = true ]; then docker start "$AIGW_REDIS_CONTAINER" >/dev/null 2>&1 || true fi } trap cleanup EXIT readiness_has_redis_status() { expected="$1" body="$(curl --silent --show-error --fail "$AIGW_READY_URL" || true)" printf '%s' "$body" | grep -q '"status":"ok"' && printf '%s' "$body" | grep -q "\"redis\":{\"required\":false,\"status\":\"$expected\"}" } shared_history_connected() { [ -n "${AIGW_METRICS_URL:-}" ] || return 0 curl --silent --show-error --fail "$AIGW_METRICS_URL" | grep -q '^aigw_provider_health_shared_connected 1$' } shared_history_disconnected() { [ -n "${AIGW_METRICS_URL:-}" ] || return 0 curl --silent --show-error --fail "$AIGW_METRICS_URL" | grep -q '^aigw_provider_health_shared_connected 0$' } if ! readiness_has_redis_status ok; then printf 'Redis must be healthy before starting the drill\n' >&2 exit 1 fi if ! shared_history_connected; then printf 'shared provider-health history must be connected before starting the drill\n' >&2 exit 1 fi docker stop "$AIGW_REDIS_CONTAINER" >/dev/null redis_stopped=true degraded=false for _ in $(seq 1 "$timeout_seconds"); do if readiness_has_redis_status degraded && shared_history_disconnected; then degraded=true break fi sleep 1 done if [ "$degraded" != true ]; then printf 'gateway did not report recoverable Redis degradation before timeout\n' >&2 exit 1 fi docker start "$AIGW_REDIS_CONTAINER" >/dev/null redis_stopped=false for _ in $(seq 1 "$timeout_seconds"); do if readiness_has_redis_status ok && shared_history_connected; then printf 'redis degradation and automatic recovery confirmed; gateway remained ready\n' exit 0 fi sleep 1 done printf 'Redis restarted but gateway did not reconnect before timeout\n' >&2 exit 1