Files
titlebot-ng-ng/run_all.sh

183 lines
5.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# TitleBot-NG - Master Launch Script
# Starts all components of the TitleBot system
# THIS SCRIPT WAS CREATED VIA BLACKBOX AI -- HUMANIZATION REQUIRED
set -e
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# PID file directory
PID_DIR="$SCRIPT_DIR/.pids"
mkdir -p "$PID_DIR"
# Log directory
LOG_DIR="$SCRIPT_DIR/logs"
mkdir -p "$LOG_DIR"
# Cleanup function
cleanup() {
echo -e "\n${YELLOW}Shutting down all services...${NC}"
# Kill all background processes
if [ -d "$PID_DIR" ]; then
for pidfile in "$PID_DIR"/*.pid; do
if [ -f "$pidfile" ]; then
pid=$(cat "$pidfile")
name=$(basename "$pidfile" .pid)
if kill -0 "$pid" 2>/dev/null; then
echo -e "${YELLOW}Stopping $name (PID: $pid)${NC}"
kill "$pid" 2>/dev/null || true
fi
rm -f "$pidfile"
fi
done
fi
echo -e "${GREEN}All services stopped.${NC}"
exit 0
}
# Set up trap for cleanup
trap cleanup SIGINT SIGTERM EXIT
# Function to start a Go service
start_go_service() {
local name=$1
local dir=$2
local binary=$3
echo -e "${BLUE}Building $name...${NC}"
(cd "$dir" && go build -o "$binary") || {
echo -e "${RED}Failed to build $name${NC}"
return 1
}
echo -e "${GREEN}Starting $name...${NC}"
(cd "$dir" && ./"$binary" > "$LOG_DIR/$name.log" 2>&1) &
echo $! > "$PID_DIR/$name.pid"
# Brief wait to check if it started successfully
sleep 1
if kill -0 $(cat "$PID_DIR/$name.pid") 2>/dev/null; then
echo -e "${GREEN}✓ $name started (PID: $(cat "$PID_DIR/$name.pid"))${NC}"
return 0
else
echo -e "${RED}✗ $name failed to start${NC}"
return 1
fi
}
# Function to start a Python service
start_python_service() {
local name=$1
local script=$2
echo -e "${GREEN}Starting $name...${NC}"
# Activate virtual environment and run script
(source .env/bin/activate && python3 "$script" > "$LOG_DIR/$name.log" 2>&1) &
echo $! > "$PID_DIR/$name.pid"
# Brief wait to check if it started successfully
sleep 1
if kill -0 $(cat "$PID_DIR/$name.pid") 2>/dev/null; then
echo -e "${GREEN}✓ $name started (PID: $(cat "$PID_DIR/$name.pid"))${NC}"
return 0
else
echo -e "${RED}✗ $name failed to start${NC}"
return 1
fi
}
# Check if virtual environment exists
if [ ! -d ".env" ]; then
echo -e "${RED}Error: Python virtual environment not found at .env${NC}"
echo -e "${YELLOW}Please create it with: python3 -m venv .env${NC}"
echo -e "${YELLOW}Then install dependencies: source .env/bin/activate && pip install matrix-nio pyyaml requests${NC}"
exit 1
fi
# Check if config.yaml exists
if [ ! -f "config.yaml" ]; then
echo -e "${RED}Error: config.yaml not found${NC}"
echo -e "${YELLOW}Please copy config_example.yaml to config.yaml and configure it${NC}"
exit 1
fi
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE} TitleBot-NG - Starting All Services ${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
# Start services in order
echo -e "${YELLOW}Step 1: Starting backend services...${NC}"
start_go_service "jsonEndpoint" "jsonEndpoint" "jep"
sleep 2 # Give jsonEndpoint time to fully start
start_go_service "voteServer" "voteServer" "vs"
sleep 2 # Give voteServer time to fully start
echo ""
echo -e "${YELLOW}Step 2: Starting Matrix bots...${NC}"
start_python_service "scraper" "scraper.py"
sleep 2
start_python_service "post_results" "post_results.py"
sleep 2
start_python_service "get_votes" "get_votes.py"
sleep 2
start_python_service "get_user_list" "get_user_list.py"
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN} All services started successfully! ${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo -e "${BLUE}Service Status:${NC}"
echo -e " • JSON Endpoint: http://localhost:9080"
echo -e " • Vote Server: http://localhost:9081"
echo -e " • Scraper: Running"
echo -e " • Post Results: Running"
echo -e " • Get Votes: Running"
echo -e " • Get User List: Running"
echo ""
echo -e "${BLUE}Logs are available in: $LOG_DIR${NC}"
echo -e "${BLUE}PIDs are stored in: $PID_DIR${NC}"
echo ""
echo -e "${YELLOW}Press Ctrl+C to stop all services${NC}"
echo ""
# Keep script running and monitor processes
while true; do
# Check if all processes are still running
all_running=true
for pidfile in "$PID_DIR"/*.pid; do
if [ -f "$pidfile" ]; then
pid=$(cat "$pidfile")
if ! kill -0 "$pid" 2>/dev/null; then
name=$(basename "$pidfile" .pid)
echo -e "${RED}Warning: $name (PID: $pid) has stopped unexpectedly${NC}"
all_running=false
fi
fi
done
if [ "$all_running" = false ]; then
echo -e "${YELLOW}Some services have stopped. Check logs in $LOG_DIR${NC}"
fi
sleep 10
done