#!/bin/bash
# SkillDarbar API - Production Startup Script
# Uses Gunicorn for production-ready concurrency handling

# Configuration
HOST="0.0.0.0"
PORT=30015
WORKERS=4
TIMEOUT=120
KEEP_ALIVE=5

# Set thread limits to prevent OpenBLAS issues
export OPENBLAS_NUM_THREADS=1
export MKL_NUM_THREADS=1

# Change to API directory
cd "$(dirname "$0")"

# Check if gunicorn is installed
if ! command -v gunicorn &> /dev/null; then
    echo "Gunicorn not found. Installing..."
    pip install gunicorn
fi

echo "Starting SkillDarbar API with Gunicorn..."
echo "  Host: $HOST"
echo "  Port: $PORT"
echo "  Workers: $WORKERS"
echo "  Timeout: ${TIMEOUT}s"

# Start Gunicorn
gunicorn \
    --bind $HOST:$PORT \
    --workers $WORKERS \
    --timeout $TIMEOUT \
    --keep-alive $KEEP_ALIVE \
    --worker-class sync \
    --access-logfile logs/access.log \
    --error-logfile logs/error.log \
    --capture-output \
    --enable-stdio-inheritance \
    app:app
