syntaxhacker/developer-portfolio-rag
0
1#!/usr/bin/env bash2set -euo pipefail3 4cd "$(dirname "$0")"5 6# ── Config ──────────────────────────────────────────────────────────────────7API_PORT="${API_PORT:-7860}"8API_LOG="/tmp/rag-pipeline-api.log"9API_PID="/tmp/rag-pipeline-api.pid"10 11# ── Prerequisites ───────────────────────────────────────────────────────────12check_prereqs() {13 if [ ! -f .env ]; then14 echo "WARNING: .env file not found. Copy .env.example to .env and configure."15 fi16 if [ ! -d venv ]; then17 echo "ERROR: venv/ not found. Run: python3 -m venv venv && uv pip install -r requirements.txt --python venv/bin/python"18 exit 119 fi20}21 22# ── PID helpers ──────────────────────────────────────────────────────────────23is_running() {24 [ -f "$1" ] && kill -0 "$(cat "$1")" 2>/dev/null25}26 27# ── Service management ──────────────────────────────────────────────────────28stop_port() {29 local port="$1" pids=""30 pids="$(lsof -ti:"${port}" 2>/dev/null || true)"31 if [ -z "$pids" ] && command -v fuser >/dev/null 2>&1; then32 pids="$(fuser "${port}/tcp" 2>/dev/null || true)"33 fi34 if [ -z "$pids" ] && command -v ss >/dev/null 2>&1; then35 pids="$(ss -tlnp "sport = :${port}" 2>/dev/null | grep -o "pid=[0-9]*" | cut -d= -f2 || true)"36 fi37 if [ -n "$pids" ]; then38 echo " Killing process(es) on port $port: $pids"39 kill $pids 2>/dev/null || true40 sleep 141 fi42}43 44stop_service() {45 local name="$1" port="$2" pid_file="$3"46 stop_port "$port"47 if [ -f "$pid_file" ]; then48 local pid49 pid="$(cat "$pid_file")"50 kill "$pid" 2>/dev/null || true51 rm -f "$pid_file"52 fi53}54 55# ── Start functions ─────────────────────────────────────────────────────────56start_backend() {57 if is_running "$API_PID"; then58 echo "Backend already running (PID $(cat "$API_PID"))"59 return60 fi61 62 echo "Starting backend (port $API_PORT)..."63 64 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 &65 echo $! > "$API_PID"66 67 for i in $(seq 1 30); do68 if curl -s http://localhost:$API_PORT/health > /dev/null 2>&1; then69 echo " Backend started (PID $(cat "$API_PID"))"70 return71 fi72 sleep 173 done74 75 echo " ERROR: Backend failed to start. Check $API_LOG"76 rm -f "$API_PID"77}78 79# ── Status ───────────────────────────────────────────────────────────────────80show_status() {81 echo "── RAG Pipeline Services ──"82 if is_running "$API_PID"; then83 echo " Backend: RUNNING (PID $(cat "$API_PID")) → http://localhost:$API_PORT"84 echo " Status: ✓ All services running"85 else86 echo " Backend: STOPPED"87 echo " Status: ✗ Backend is down"88 fi89}90 91# ── Commands ─────────────────────────────────────────────────────────────────92check_prereqs93 94case "${1:-status}" in95 start)96 start_backend97 show_status98 ;;99 stop)100 echo "Stopping services..."101 stop_service "backend" "$API_PORT" "$API_PID"102 echo "All services stopped."103 ;;104 restart)105 stop_service "backend" "$API_PORT" "$API_PID"106 sleep 1107 start_backend108 show_status109 ;;110 status)111 show_status112 ;;113 logs)114 if [ -f "$API_LOG" ]; then115 tail -f "$API_LOG"116 else117 echo "No log file found at $API_LOG"118 fi119 ;;120 *)121 echo "Usage: $0 {start|stop|restart|status|logs}"122 exit 1123 ;;124esac125 