diff --git a/documents/DEPLOYMENT_GUIDE.md b/documents/DEPLOYMENT_GUIDE.md index b5b2575..630aeec 100644 --- a/documents/DEPLOYMENT_GUIDE.md +++ b/documents/DEPLOYMENT_GUIDE.md @@ -44,12 +44,6 @@ The system consists of 6 independent microservices coordinated by an API Gateway ## 🚀 Quick Start -### Prerequisites -- Docker 20.0+ -- Docker Compose 2.0+ -- 8GB RAM minimum -- 10GB free disk space - ### 1. Deploy the Complete System ```bash cd microservices/ diff --git a/microservices/api-gateway/main.py b/microservices/api-gateway/main.py index 3a6cba4..16f0be3 100644 --- a/microservices/api-gateway/main.py +++ b/microservices/api-gateway/main.py @@ -27,16 +27,12 @@ logger = logging.getLogger(__name__) @asynccontextmanager async def lifespan(app: FastAPI): - """Application lifespan manager""" logger.info("API Gateway starting up...") - # Initialize service registry await service_registry.initialize() - # Register all services await service_registry.register_services(SERVICES) - # Start health check task asyncio.create_task(health_check_task()) logger.info("API Gateway startup complete") diff --git a/microservices/api-gateway/service_registry.py b/microservices/api-gateway/service_registry.py index 21ea678..e00fd7f 100644 --- a/microservices/api-gateway/service_registry.py +++ b/microservices/api-gateway/service_registry.py @@ -39,7 +39,6 @@ class ServiceRegistry: logger.info(f"Registered {len(services)} services") - # Perform initial health check await self.update_all_service_health() async def register_service(self, service_config: ServiceConfig): @@ -83,7 +82,6 @@ class ServiceRegistry: if response.status == 200: health_data = await response.json() status = "healthy" if health_data.get("status") in ["healthy", "ok"] else "unhealthy" - health = ServiceHealth( service=service_name, status=status, @@ -114,10 +112,8 @@ class ServiceRegistry: error_message=f"Health check failed: {str(e)}" ) - # Update health status self.service_health[service_name] = health - # Log health status changes if health.status != "healthy": logger.warning(f"Service {service_name} health check failed: {health.error_message}") @@ -128,13 +124,12 @@ class ServiceRegistry: self.check_service_health(service_name) for service_name in self.services.keys() ] - if health_checks: await asyncio.gather(*health_checks, return_exceptions=True) healthy_count = sum(1 for h in self.service_health.values() if h.status == "healthy") total_count = len(self.services) - logger.info(f"Health check complete: {healthy_count}/{total_count} services healthy {self.service_health.values()}") + logger.info(f"Health check complete: {healthy_count}/{total_count} services healthy") async def get_service_health(self, service_name: str) -> Optional[ServiceHealth]: return self.service_health.get(service_name) diff --git a/microservices/data-ingestion-service/src/main.py b/microservices/data-ingestion-service/src/main.py index bf02b30..9c73c49 100644 --- a/microservices/data-ingestion-service/src/main.py +++ b/microservices/data-ingestion-service/src/main.py @@ -19,19 +19,12 @@ db_manager = None async def lifespan(app: FastAPI): global ftp_monitor, db_manager - logger.info("Starting SA4CPS Data Ingestion Service...") - db_manager = DatabaseManager() await db_manager.connect() - logger.info("Database connection established") ftp_monitor = FTPMonitor(db_manager) - logger.info("FTP monitor created") monitoring_task = asyncio.create_task(ftp_monitor.start_monitoring()) - logger.info("FTP monitoring task started in background") - - logger.info("Service startup complete - HTTP server ready to accept requests") yield @@ -78,7 +71,8 @@ async def health_check(): global ftp_monitor, db_manager health_status = { - "service": "healthy", + "service": "data-ingestion-service", + "status": "healthy", "timestamp": datetime.now().isoformat(), "database": "unknown", "ftp_monitor": "unknown" diff --git a/microservices/docker-compose.yml b/microservices/docker-compose.yml index e5b02c8..218e956 100644 --- a/microservices/docker-compose.yml +++ b/microservices/docker-compose.yml @@ -169,7 +169,6 @@ services: # networks: # - energy-network - # Data Ingestion Service (FTP Monitoring & SA4CPS Integration) data-ingestion-service: build: context: ./data-ingestion-service diff --git a/microservices/sensor-service/database.py b/microservices/sensor-service/database.py index 096b063..0937a0f 100644 --- a/microservices/sensor-service/database.py +++ b/microservices/sensor-service/database.py @@ -17,50 +17,44 @@ redis_client: Optional[redis.Redis] = None database = None async def connect_to_mongo(): - """Connect to MongoDB""" global mongo_client, database - + try: mongo_url = os.getenv("MONGO_URL", "mongodb://admin:password123@mongodb:27017/energy_dashboard_sensors?authSource=admin") - + mongo_client = AsyncIOMotorClient(mongo_url) database = mongo_client.energy_dashboard_sensors - - # Test connection + await mongo_client.admin.command('ping') logger.info("Connected to MongoDB successfully") - + except Exception as e: logger.error(f"Failed to connect to MongoDB: {e}") raise async def close_mongo_connection(): - """Close MongoDB connection""" global mongo_client if mongo_client: mongo_client.close() logger.info("Closed MongoDB connection") async def connect_to_redis(): - """Connect to Redis""" global redis_client - + try: redis_url = os.getenv("REDIS_URL", "redis://redis:6379") redis_client = redis.from_url(redis_url, decode_responses=True) - + # Test connection await redis_client.ping() logger.info("Connected to Redis successfully") - + except Exception as e: logger.error(f"Failed to connect to Redis: {e}") raise async def get_database(): - """Get database instance""" return database async def get_redis(): - """Get Redis client instance""" - return redis_client \ No newline at end of file + return redis_client diff --git a/microservices/sensor-service/main.py b/microservices/sensor-service/main.py index 04de04a..5b473a1 100644 --- a/microservices/sensor-service/main.py +++ b/microservices/sensor-service/main.py @@ -25,21 +25,17 @@ from room_service import RoomService from analytics_service import AnalyticsService from websocket_manager import WebSocketManager -# Configure logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) -# WebSocket manager for real-time updates websocket_manager = WebSocketManager() @asynccontextmanager async def lifespan(app: FastAPI): - """Application lifespan manager""" logger.info("Sensor Service starting up...") await connect_to_mongo() await connect_to_redis() - # Initialize default rooms if none exist db = await get_database() redis_client = await get_redis() room_service = RoomService(db, redis_client)