File size: 4,200 Bytes
59b7a59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/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