#!/bin/bash
# Startup script for API with auto-scaling workers

# Auto-detect worker count based on CPU cores
# Formula: (CPU_cores / 2) + 1, min 2, max 8
CPU_CORES=$(nproc)
WORKERS=$(( ($CPU_CORES / 2) + 1 ))
WORKERS=$(( WORKERS < 2 ? 2 : WORKERS ))
WORKERS=$(( WORKERS > 8 ? 8 : WORKERS ))

echo "Starting API with $WORKERS workers (auto-detected from $CPU_CORES CPU cores)"

exec gunicorn api.main:app \
    --workers $WORKERS \
    --worker-class uvicorn.workers.UvicornWorker \
    --bind 0.0.0.0:8000 \
    --timeout 120 \
    --max-requests 1000 \
    --max-requests-jitter 100 \
    --access-logfile - \
    --error-logfile - \
    --log-level info
