#!/bin/bash # Telegram Marketing Intelligence Agent System Startup Script echo "🚀 Starting Telegram Marketing Intelligence Agent System" echo "======================================================" echo "" # Colors for output GREEN='\033[0;32m' RED='\033[0;31m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Function to check if a service is running check_service() { local service_name=$1 local port=$2 if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null ; then echo -e "${GREEN}✓${NC} $service_name is running on port $port" return 0 else echo -e "${RED}✗${NC} $service_name is not running on port $port" return 1 fi } # Function to wait for a service to be ready wait_for_service() { local service_name=$1 local port=$2 local max_attempts=30 local attempt=0 echo -n "Waiting for $service_name to be ready..." while [ $attempt -lt $max_attempts ]; do if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null 2>&1; then echo -e " ${GREEN}Ready!${NC}" return 0 fi echo -n "." sleep 1 attempt=$((attempt + 1)) done echo -e " ${RED}Failed!${NC}" return 1 } # Check if Docker is running if ! docker info > /dev/null 2>&1; then echo -e "${RED}Docker is not running. Please start Docker first.${NC}" exit 1 fi # Start infrastructure services echo "1. Starting Infrastructure Services..." echo "-------------------------------------" # Start MongoDB echo -n "Starting MongoDB..." docker-compose up -d mongodb > /dev/null 2>&1 if wait_for_service "MongoDB" 27018; then echo -e "${GREEN}✓ MongoDB started${NC}" else echo -e "${RED}✗ Failed to start MongoDB${NC}" exit 1 fi # Start Redis echo -n "Starting Redis..." docker-compose up -d redis > /dev/null 2>&1 # Redis is not exposed externally in docker-compose, so we'll skip the port check echo -e "${GREEN}✓ Redis started${NC}" # Start RabbitMQ echo -n "Starting RabbitMQ..." docker-compose up -d rabbitmq > /dev/null 2>&1 if wait_for_service "RabbitMQ" 5673; then echo -e "${GREEN}✓ RabbitMQ started${NC}" else echo -e "${RED}✗ Failed to start RabbitMQ${NC}" exit 1 fi echo "" echo "2. Starting Backend Services..." echo "-------------------------------" # Function to start a Node.js service start_node_service() { local service_name=$1 local service_dir=$2 local port=$3 echo -n "Starting $service_name..." # Check if service directory exists if [ ! -d "$service_dir" ]; then echo -e " ${RED}Directory not found${NC}" return 1 fi # Kill any existing process on the port lsof -ti:$port | xargs kill -9 2>/dev/null # Start the service cd "$service_dir" # Install dependencies if needed if [ ! -d "node_modules" ]; then npm install > /dev/null 2>&1 fi # Start the service in background nohup npm start > "$service_name.log" 2>&1 & # Wait for service to be ready if wait_for_service "$service_name" $port; then echo -e " ${GREEN}✓ Started${NC}" return 0 else echo -e " ${RED}✗ Failed${NC}" return 1 fi } # Start all backend services start_node_service "API Gateway" "./api-gateway" 3000 start_node_service "Auth Service" "./auth-service" 3001 start_node_service "Campaign Orchestrator" "./campaign-orchestrator" 3002 start_node_service "Telegram Adapter" "./gramjs-adapter" 3003 start_node_service "Message Queue" "./message-queue-processor" 3004 start_node_service "Analytics Service" "./analytics-service" 3005 start_node_service "Compliance Service" "./compliance-service" 3006 start_node_service "AI Service" "./ai-service" 3007 start_node_service "Monitoring Service" "./monitoring-service" 3008 start_node_service "Backup Service" "./backup-service" 3009 start_node_service "Notification Service" "./notification-service" 3010 start_node_service "Billing Service" "./billing-service" 3011 echo "" echo "3. Starting Frontend..." echo "----------------------" # Start frontend echo -n "Starting Frontend..." cd frontend if [ ! -d "node_modules" ]; then npm install > /dev/null 2>&1 fi nohup npm run dev > frontend.log 2>&1 & if wait_for_service "Frontend" 5173; then echo -e " ${GREEN}✓ Frontend started${NC}" else echo -e " ${RED}✗ Failed to start Frontend${NC}" fi echo "" echo "4. System Status Check..." echo "------------------------" # Check all services services=( "MongoDB:27017" "Redis:6379" "RabbitMQ:5672" "API Gateway:3000" "Auth Service:3001" "Campaign Orchestrator:3002" "Telegram Adapter:3003" "Message Queue:3004" "Analytics Service:3005" "Compliance Service:3006" "AI Service:3007" "Monitoring Service:3008" "Backup Service:3009" "Notification Service:3010" "Billing Service:3011" "Frontend:5173" ) all_running=true for service in "${services[@]}"; do IFS=':' read -r name port <<< "$service" if ! check_service "$name" "$port"; then all_running=false fi done echo "" if [ "$all_running" = true ]; then echo -e "${GREEN}✅ All services are running successfully!${NC}" echo "" echo "📌 Access Points:" echo " Frontend: http://localhost:5173" echo " API Gateway: http://localhost:3000" echo " API Documentation: http://localhost:3000/api-docs" echo " RabbitMQ Management: http://localhost:15672 (guest/guest)" echo " Monitoring Dashboard: http://localhost:3008/metrics" echo "" echo "📝 Default Admin Credentials:" echo " Email: admin@example.com" echo " Password: admin123" echo "" echo "🔍 View logs:" echo " tail -f */*.log" else echo -e "${YELLOW}⚠️ Some services failed to start. Check the logs for details.${NC}" echo "" echo "Debug commands:" echo " docker-compose logs [service-name]" echo " cat [service-directory]/[service-name].log" fi echo "" echo "To stop all services, run: ./stop-system.sh"