#!/bin/bash # ============================================ # DocGenie RQ Worker Startup Script # ============================================ # This script starts an RQ (Redis Queue) worker for processing # background document generation jobs. set -e # Exit on error echo "🚀 Starting DocGenie RQ Worker..." # Activate virtual environment VENV_PATH="../.venv" if [ -d "$VENV_PATH" ]; then echo "✓ Activating virtual environment..." source "$VENV_PATH/bin/activate" else echo "⚠ Warning: Virtual environment not found at $VENV_PATH" fi # Load environment variables from .env using Python (handles special characters properly) if [ -f .env ]; then echo "✓ Loading .env file..." eval $(python -c " import os from dotenv import load_dotenv load_dotenv() for key, value in os.environ.items(): # Only export DocGenie related variables if key.startswith(('REDIS', 'SUPABASE', 'ANTHROPIC', 'BATCH', 'MESSAGE', 'RQ_', 'GOOGLE')): # Properly escape single quotes in the value safe_value = value.replace(\"'\", \"'\\\\''\" ) print(f\"export {key}='{safe_value}'\") ") else echo "⚠ Warning: .env file not found" fi # Check Redis connection echo "🔍 Checking Redis connection..." if ! python -c "import redis; r = redis.from_url('${REDIS_URL:-redis://localhost:6379/0}'); r.ping()" 2>/dev/null; then echo "❌ Error: Cannot connect to Redis" echo " Please ensure Redis is running:" echo " $ docker run -d -p 6379:6379 redis:latest" echo " OR" echo " $ redis-server" exit 1 fi echo "✓ Redis connected" # Check Supabase configuration if [ -z "$SUPABASE_URL" ] || [ -z "$SUPABASE_KEY" ]; then echo "❌ Error: SUPABASE_URL and SUPABASE_KEY must be set in .env" exit 1 fi echo "✓ Supabase configured" # Check Claude API key if [ -z "$ANTHROPIC_API_KEY" ]; then echo "❌ Error: ANTHROPIC_API_KEY must be set in .env" exit 1 fi echo "✓ Claude API key configured" # Create temporary directories mkdir -p "${BATCH_DATA_DIR:-/tmp/docgenie_batches}" mkdir -p "${MESSAGE_DATA_DIR:-/tmp/docgenie_messages}" echo "✓ Temporary directories created" # Start worker QUEUE_NAME="${RQ_QUEUE_NAME:-docgenie}" echo "" echo "============================================" echo "Worker Configuration:" echo " Queue: $QUEUE_NAME" # Mask Redis credentials for security echo " Redis: [HIDDEN]" echo " Batch Data: ${BATCH_DATA_DIR:-/tmp/docgenie_batches}" echo " Message Data: ${MESSAGE_DATA_DIR:-/tmp/docgenie_messages}" echo "============================================" echo "" echo "✅ Starting RQ worker (press Ctrl+C to stop)..." echo "" # Run RQ worker # - Listen on specified queue # - Burst mode: exit when queue is empty (use for testing) # - Remove --burst for production (keeps running) # Use PYTHONPATH to ensure worker.py can be imported PYTHONPATH="$(pwd):$PYTHONPATH" rq worker "$QUEUE_NAME" \ --url "${REDIS_URL:-redis://localhost:6379/0}" \ --verbose # --burst # Uncomment for testing (exit when queue empty) # Note: Worker will keep running until Ctrl+C is pressed # In production, use a process manager like systemd or supervisor