#!/bin/bash # Agentic RAG Service Startup Script set -e # Configuration PORT=${PORT:-8000} HOST=${HOST:-127.0.0.1} CONFIG_FILE="config.yaml" # Colors for output GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' # No Color echo -e "${GREEN}🚀 Starting Agentic RAG Service${NC}" # Check if config file exists if [[ ! -f "$CONFIG_FILE" ]]; then echo -e "${RED}❌ Configuration file '$CONFIG_FILE' not found!${NC}" echo -e "${YELLOW}💡 Make sure config.yaml is in the root directory${NC}" exit 1 fi echo -e "${GREEN}✅ Found configuration file: $CONFIG_FILE${NC}" # Check if port is available echo -e "${GREEN}🔍 Checking port $PORT availability...${NC}" PIDS=$(ss -tulpn 2>/dev/null | grep ":$PORT " | grep -o 'pid=[0-9]*' | cut -d'=' -f2 || true) if [ -n "$PIDS" ]; then echo -e "${YELLOW}⚠️ Port $PORT is in use by:${NC}" for PID in $PIDS; do PROCESS_INFO=$(ps -p $PID -o cmd --no-headers 2>/dev/null || echo "Unknown process") echo -e "${YELLOW} PID $PID: $PROCESS_INFO${NC}" done echo -e "${YELLOW}💀 Stopping existing processes on port $PORT...${NC}" for PID in $PIDS; do if kill -TERM $PID 2>/dev/null; then echo -e "${GREEN} ✅ Terminated PID $PID${NC}" sleep 1 # Force kill if still running if kill -0 $PID 2>/dev/null; then kill -KILL $PID 2>/dev/null && echo -e "${GREEN} 🔥 Force killed PID $PID${NC}" fi fi done # Verify port is free sleep 1 NEW_PIDS=$(ss -tulpn 2>/dev/null | grep ":$PORT " | grep -o 'pid=[0-9]*' | cut -d'=' -f2 || true) if [ -z "$NEW_PIDS" ]; then echo -e "${GREEN}✅ Port $PORT is now free${NC}" else echo -e "${RED}❌ Warning: Port $PORT may still be in use${NC}" fi else echo -e "${GREEN}✅ Port $PORT is available${NC}" fi # Start the service echo -e "${GREEN}🔄 Starting service on http://$HOST:$PORT${NC}" if [[ "$1" == "--dev" ]]; then echo -e "${YELLOW}🛠️ Development mode: auto-reload enabled${NC}" uv run uvicorn service.main:app --host $HOST --port $PORT --reload elif [[ "$1" == "--background" ]]; then echo -e "${GREEN}🏃 Background mode${NC}" nohup uv run uvicorn service.main:app --host $HOST --port $PORT > server.log 2>&1 & SERVER_PID=$! echo -e "${GREEN}✅ Service started with PID: $SERVER_PID${NC}" echo -e "${GREEN}📋 Logs: tail -f server.log${NC}" # Wait a moment and check if service is healthy sleep 3 if curl -s http://$HOST:$PORT/health >/dev/null 2>&1; then echo -e "${GREEN}🎉 Service is healthy and ready!${NC}" echo -e "${GREEN}🌐 Health check: http://$HOST:$PORT/health${NC}" echo -e "${GREEN}📖 API docs: http://$HOST:$PORT/docs${NC}" else echo -e "${RED}❌ Service health check failed${NC}" echo -e "${YELLOW}📋 Check logs: tail server.log${NC}" exit 1 fi else echo -e "${GREEN}🏃 Foreground mode (default)${NC}" echo -e "${YELLOW}💡 Use --background to run in background, --dev for development mode${NC}" echo -e "${GREEN}🌐 Service will be available at: http://$HOST:$PORT${NC}" echo -e "${GREEN}📖 API docs: http://$HOST:$PORT/docs${NC}" echo -e "${YELLOW}⚠️ Press Ctrl+C to stop the service${NC}" echo "" # Run in foreground uv run uvicorn service.main:app --host $HOST --port $PORT fi