#!/usr/bin/env bash set -euo pipefail cd "$(dirname "$0")" # ── Config ────────────────────────────────────────────────────────────────── API_PORT="${API_PORT:-7860}" API_LOG="/tmp/rag-pipeline-api.log" API_PID="/tmp/rag-pipeline-api.pid" # ── Prerequisites ─────────────────────────────────────────────────────────── check_prereqs() { if [ ! -f .env ]; then echo "WARNING: .env file not found. Copy .env.example to .env and configure." fi if [ ! -d venv ]; then echo "ERROR: venv/ not found. Run: python3 -m venv venv && uv pip install -r requirements.txt --python venv/bin/python" exit 1 fi } # ── PID helpers ────────────────────────────────────────────────────────────── is_running() { [ -f "$1" ] && kill -0 "$(cat "$1")" 2>/dev/null } # ── Service management ────────────────────────────────────────────────────── stop_port() { local port="$1" pids="" pids="$(lsof -ti:"${port}" 2>/dev/null || true)" if [ -z "$pids" ] && command -v fuser >/dev/null 2>&1; then pids="$(fuser "${port}/tcp" 2>/dev/null || true)" fi if [ -z "$pids" ] && command -v ss >/dev/null 2>&1; then pids="$(ss -tlnp "sport = :${port}" 2>/dev/null | grep -o "pid=[0-9]*" | cut -d= -f2 || true)" fi if [ -n "$pids" ]; then echo " Killing process(es) on port $port: $pids" kill $pids 2>/dev/null || true sleep 1 fi } stop_service() { local name="$1" port="$2" pid_file="$3" stop_port "$port" if [ -f "$pid_file" ]; then local pid pid="$(cat "$pid_file")" kill "$pid" 2>/dev/null || true rm -f "$pid_file" fi } # ── Start functions ───────────────────────────────────────────────────────── start_backend() { if is_running "$API_PID"; then echo "Backend already running (PID $(cat "$API_PID"))" return fi echo "Starting backend (port $API_PORT)..." setsid env UVICORN_RELOAD=0 venv/bin/uvicorn app.main:app --host 0.0.0.0 --port "$API_PORT" --log-level info >> "$API_LOG" 2>&1 & echo $! > "$API_PID" for i in $(seq 1 30); do if curl -s http://localhost:$API_PORT/health > /dev/null 2>&1; then echo " Backend started (PID $(cat "$API_PID"))" return fi sleep 1 done echo " ERROR: Backend failed to start. Check $API_LOG" rm -f "$API_PID" } # ── Status ─────────────────────────────────────────────────────────────────── show_status() { echo "── RAG Pipeline Services ──" if is_running "$API_PID"; then echo " Backend: RUNNING (PID $(cat "$API_PID")) → http://localhost:$API_PORT" echo " Status: ✓ All services running" else echo " Backend: STOPPED" echo " Status: ✗ Backend is down" fi } # ── Commands ───────────────────────────────────────────────────────────────── check_prereqs case "${1:-status}" in start) start_backend show_status ;; stop) echo "Stopping services..." stop_service "backend" "$API_PORT" "$API_PID" echo "All services stopped." ;; restart) stop_service "backend" "$API_PORT" "$API_PID" sleep 1 start_backend show_status ;; status) show_status ;; logs) if [ -f "$API_LOG" ]; then tail -f "$API_LOG" else echo "No log file found at $API_LOG" fi ;; *) echo "Usage: $0 {start|stop|restart|status|logs}" exit 1 ;; esac