#!/bin/bash

# Queue Health Check - Cron Job for Production Monitoring
# Checks queue health and alerts if issues detected
#
# Install as cron job:
# */5 * * * * /path/to/showprima/scripts/cron/queue-health-check.sh >> /var/log/showprima-queue-health.log 2>&1

set -e

# Configuration
PROJECT_ROOT="/path/to/showprima"  # UPDATE THIS
ALERT_EMAIL="admin@showprima.com"   # UPDATE THIS
MAX_QUEUE_SIZE=100
MAX_FAILED_JOBS=10

# Change to project root
cd "$PROJECT_ROOT"

# Load database helper functions
source "$PROJECT_ROOT/scripts/_db-helper.sh"

# Get current timestamp
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')

echo "[$TIMESTAMP] Queue Health Check"

# Function to send alert
send_alert() {
    SUBJECT=$1
    MESSAGE=$2

    # Send email alert (requires mail command)
    echo "$MESSAGE" | mail -s "$SUBJECT" "$ALERT_EMAIL"

    # Log alert
    echo "[$TIMESTAMP] ALERT: $SUBJECT"
    echo "$MESSAGE"
}

# Check 1: Queue Worker Running
WORKER_COUNT=$(pgrep -f "queue:work" | wc -l)
if [ "$WORKER_COUNT" -eq 0 ]; then
    send_alert "⚠️ Queue Worker DOWN - Show Prima" \
        "Queue worker is not running!

Time: $TIMESTAMP
Workers Running: 0
Expected: 2

Action Required:
sudo supervisorctl start showprima-queue-worker:*

Check logs:
sudo supervisorctl tail showprima-queue-worker:00 stdout"
else
    echo "  ✓ Queue workers running: $WORKER_COUNT"
fi

# Check 2: Queue Size
QUEUE_SIZE=$(php artisan tinker --execute="echo DB::table('jobs')->count();")
echo "  Queue size: $QUEUE_SIZE jobs"

if [ "$QUEUE_SIZE" -gt "$MAX_QUEUE_SIZE" ]; then
    send_alert "⚠️ High Queue Size - Show Prima" \
        "Queue size exceeded threshold!

Time: $TIMESTAMP
Current Size: $QUEUE_SIZE jobs
Threshold: $MAX_QUEUE_SIZE jobs

Queues:
$(php artisan queue:monitor)

Action Required:
1. Check if workers are processing jobs
2. Restart workers if stuck:
   sudo supervisorctl restart showprima-queue-worker:*
3. Monitor queue:
   php artisan queue:monitor"
fi

# Check 3: Failed Jobs
FAILED_COUNT=$(php artisan tinker --execute="echo DB::table('failed_jobs')->count();")
echo "  Failed jobs: $FAILED_COUNT"

if [ "$FAILED_COUNT" -gt "$MAX_FAILED_JOBS" ]; then
    send_alert "⚠️ High Failed Jobs Count - Show Prima" \
        "Failed jobs exceeded threshold!

Time: $TIMESTAMP
Failed Jobs: $FAILED_COUNT
Threshold: $MAX_FAILED_JOBS

Action Required:
1. Review failed jobs:
   php artisan queue:failed

2. Retry if safe:
   php artisan queue:retry all

3. Check logs:
   tail -100 storage/logs/laravel-$(date +%Y-%m-%d).log

Recent failures:
$(php artisan queue:failed | head -20)"
fi

# Check 4: Queue Processing Speed
# Get oldest job age (if any)
if [ "$QUEUE_SIZE" -gt 0 ]; then
    OLDEST_JOB=$(php artisan tinker --execute="
        \$job = DB::table('jobs')->orderBy('created_at', 'asc')->first();
        if (\$job) {
            echo now()->diffInMinutes(\$job->created_at);
        } else {
            echo 0;
        }
    ")

    echo "  Oldest job age: ${OLDEST_JOB} minutes"

    if [ "$OLDEST_JOB" -gt 30 ]; then
        send_alert "⚠️ Slow Queue Processing - Show Prima" \
            "Oldest job in queue is too old!

Time: $TIMESTAMP
Oldest Job Age: ${OLDEST_JOB} minutes
Queue Size: $QUEUE_SIZE jobs
Workers: $WORKER_COUNT

This indicates workers may be stuck or overwhelmed.

Action Required:
1. Restart workers:
   sudo supervisorctl restart showprima-queue-worker:*

2. Check for stuck jobs:
   php artisan queue:monitor

3. Consider increasing workers:
   Edit /etc/supervisor/conf.d/showprima-queue.conf
   Update numprocs=2 to numprocs=4"
    fi
fi

# Summary
echo "[$TIMESTAMP] Health check complete"
echo "  Workers: $WORKER_COUNT"
echo "  Queue size: $QUEUE_SIZE"
echo "  Failed jobs: $FAILED_COUNT"
echo ""

# Exit with success
exit 0
