#!/bin/bash

# Redis Monitoring and Auto-Recovery Script
# Cron: */5 * * * * /path/to/redis-monitor.sh

LOG_FILE="/var/log/redis-monitor.log"
REDIS_CONTAINER="cbt-redis"
MAX_RESTART_ATTEMPTS=3
RESTART_COOLDOWN=300  # 5 minutes

# Function to log messages
log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"
}

# Function to check Redis health
check_redis_health() {
    docker exec "$REDIS_CONTAINER" redis-cli ping > /dev/null 2>&1
    return $?
}

# Function to restart Redis
restart_redis() {
    log "WARNING: Redis is not responding, attempting restart..."

    # Check if we're in cooldown period
    LAST_RESTART_FILE="/tmp/redis_last_restart"
    if [ -f "$LAST_RESTART_FILE" ]; then
        LAST_RESTART=$(cat "$LAST_RESTART_FILE")
        CURRENT_TIME=$(date +%s)
        ELAPSED=$((CURRENT_TIME - LAST_RESTART))

        if [ $ELAPSED -lt $RESTART_COOLDOWN ]; then
            log "WARNING: Restart cooldown active. Skipping restart."
            return 1
        fi
    fi

    # Restart Redis container
    docker restart "$REDIS_CONTAINER" >> "$LOG_FILE" 2>&1

    if [ $? -eq 0 ]; then
        log "SUCCESS: Redis container restarted successfully"
        echo "$(date +%s)" > "$LAST_RESTART_FILE"

        # Wait for Redis to be ready
        sleep 10

        # Verify it's working
        if check_redis_health; then
            log "SUCCESS: Redis is now responding"
            return 0
        else
            log "ERROR: Redis restarted but still not responding"
            return 1
        fi
    else
        log "ERROR: Failed to restart Redis container"
        return 1
    fi
}

# Main monitoring logic
log "INFO: Starting Redis health check..."

if check_redis_health; then
    log "INFO: Redis is healthy"
else
    log "ERROR: Redis is not responding"
    restart_redis
fi