Files
bakery-ia/services/production/app/main.py

234 lines
9.5 KiB
Python
Raw Normal View History

2025-08-21 20:28:14 +02:00
# ================================================================
# services/production/app/main.py
# ================================================================
"""
Production Service - FastAPI Application
Production planning and batch management service
"""
2025-09-29 13:13:12 +02:00
import time
2025-08-21 20:28:14 +02:00
from fastapi import FastAPI, Request
2025-09-30 08:12:45 +02:00
from sqlalchemy import text
2025-08-21 20:28:14 +02:00
from app.core.config import settings
2025-09-29 13:13:12 +02:00
from app.core.database import database_manager
2025-08-23 10:19:58 +02:00
from app.services.production_alert_service import ProductionAlertService
2025-12-13 23:57:54 +01:00
from app.services.production_scheduler import ProductionScheduler
from app.services.production_notification_service import ProductionNotificationService
2025-09-29 13:13:12 +02:00
from shared.service_base import StandardFastAPIService
2025-10-06 15:27:01 +02:00
# Import standardized routers
from app.api import (
2025-12-13 23:57:54 +01:00
internal_demo,
2025-10-06 15:27:01 +02:00
production_batches,
production_schedules,
production_operations,
production_dashboard,
analytics,
quality_templates,
2025-10-19 19:22:37 +02:00
equipment,
2025-10-31 18:57:58 +01:00
orchestrator, # NEW: Orchestrator integration endpoint
2025-11-02 20:24:44 +01:00
production_orders_operations, # Tenant deletion endpoints
2025-11-05 13:34:56 +01:00
audit,
2025-12-05 20:07:01 +01:00
ml_insights, # ML insights endpoint
batch
2025-10-06 15:27:01 +02:00
)
2025-12-05 20:07:01 +01:00
from app.api.internal_alert_trigger import router as internal_alert_trigger_router
2025-10-06 15:27:01 +02:00
2025-09-29 13:13:12 +02:00
class ProductionService(StandardFastAPIService):
"""Production Service with standardized setup"""
2025-12-05 20:07:01 +01:00
expected_migration_version = "001_initial_schema"
2025-09-30 08:12:45 +02:00
async def on_startup(self, app):
"""Custom startup logic including migration verification"""
await self.verify_migrations()
await super().on_startup(app)
async def verify_migrations(self):
"""Verify database schema matches the latest migrations."""
try:
async with self.database_manager.get_session() as session:
result = await session.execute(text("SELECT version_num FROM alembic_version"))
version = result.scalar()
if version != self.expected_migration_version:
self.logger.error(f"Migration version mismatch: expected {self.expected_migration_version}, got {version}")
raise RuntimeError(f"Migration version mismatch: expected {self.expected_migration_version}, got {version}")
self.logger.info(f"Migration verification successful: {version}")
except Exception as e:
self.logger.error(f"Migration verification failed: {e}")
raise
2025-09-29 13:13:12 +02:00
def __init__(self):
# Define expected database tables for health checks
production_expected_tables = [
'production_batches', 'production_schedules', 'production_capacity',
'quality_check_templates', 'quality_checks', 'equipment'
]
self.alert_service = None
2025-12-13 23:57:54 +01:00
self.notification_service = None
2025-12-05 20:07:01 +01:00
self.rabbitmq_client = None
self.event_publisher = None
2025-10-30 21:08:07 +01:00
# REMOVED: scheduler_service (replaced by Orchestrator Service)
2025-09-29 13:13:12 +02:00
2025-10-09 18:01:24 +02:00
# Create custom checks for services
2025-09-29 13:13:12 +02:00
async def check_alert_service():
"""Check production alert service health"""
try:
return bool(self.alert_service) if self.alert_service else False
except Exception as e:
self.logger.error("Alert service health check failed", error=str(e))
return False
super().__init__(
service_name=settings.SERVICE_NAME,
app_name=settings.APP_NAME,
description=settings.DESCRIPTION,
version=settings.VERSION,
2025-10-06 15:27:01 +02:00
api_prefix="", # Empty because RouteBuilder already includes /api/v1
2025-09-29 13:13:12 +02:00
database_manager=database_manager,
expected_tables=production_expected_tables,
2025-10-09 18:01:24 +02:00
custom_health_checks={
2025-10-30 21:08:07 +01:00
"alert_service": check_alert_service
2025-12-05 20:07:01 +01:00
},
enable_messaging=True # Enable messaging support
2025-09-29 13:13:12 +02:00
)
2025-12-05 20:07:01 +01:00
async def _setup_messaging(self):
"""Setup messaging for production service using unified messaging"""
from shared.messaging import UnifiedEventPublisher, RabbitMQClient
try:
self.rabbitmq_client = RabbitMQClient(settings.RABBITMQ_URL, service_name="production-service")
await self.rabbitmq_client.connect()
# Create unified event publisher
self.event_publisher = UnifiedEventPublisher(self.rabbitmq_client, "production-service")
self.logger.info("Production service unified messaging setup completed")
except Exception as e:
self.logger.error("Failed to setup production unified messaging", error=str(e))
raise
async def _cleanup_messaging(self):
"""Cleanup messaging for production service"""
try:
if self.rabbitmq_client:
await self.rabbitmq_client.disconnect()
self.logger.info("Production service messaging cleanup completed")
except Exception as e:
self.logger.error("Error during production messaging cleanup", error=str(e))
2025-09-29 13:13:12 +02:00
async def on_startup(self, app: FastAPI):
"""Custom startup logic for production service"""
2025-12-05 20:07:01 +01:00
# Initialize messaging
await self._setup_messaging()
# Initialize alert service with EventPublisher and database manager
self.alert_service = ProductionAlertService(self.event_publisher, self.database_manager)
2025-09-29 13:13:12 +02:00
await self.alert_service.start()
self.logger.info("Production alert service started")
2025-12-13 23:57:54 +01:00
# Initialize notification service with EventPublisher
self.notification_service = ProductionNotificationService(self.event_publisher)
self.logger.info("Production notification service initialized")
2025-12-05 20:07:01 +01:00
2025-12-13 23:57:54 +01:00
# Initialize production scheduler with alert service and database manager
self.production_scheduler = ProductionScheduler(self.alert_service, self.database_manager)
await self.production_scheduler.start()
self.logger.info("Production scheduler started")
2025-10-09 18:01:24 +02:00
# Store services in app state
2025-09-29 13:13:12 +02:00
app.state.alert_service = self.alert_service
2025-12-05 20:07:01 +01:00
app.state.production_alert_service = self.alert_service # Also store with this name for internal trigger
2025-12-13 23:57:54 +01:00
app.state.notification_service = self.notification_service # Notification service for state change events
app.state.production_scheduler = self.production_scheduler # Store scheduler for manual triggering
2025-09-29 13:13:12 +02:00
async def on_shutdown(self, app: FastAPI):
2025-10-30 21:08:07 +01:00
"""Custom shutdown logic for production service"""
2025-12-13 23:57:54 +01:00
# Stop production scheduler
if hasattr(self, 'production_scheduler') and self.production_scheduler:
await self.production_scheduler.stop()
self.logger.info("Production scheduler stopped")
2025-08-23 10:19:58 +02:00
# Stop alert service
2025-09-29 13:13:12 +02:00
if self.alert_service:
await self.alert_service.stop()
self.logger.info("Alert service stopped")
2025-12-05 20:07:01 +01:00
# Cleanup messaging
await self._cleanup_messaging()
2025-09-29 13:13:12 +02:00
def get_service_features(self):
"""Return production-specific features"""
return [
"production_planning",
"batch_management",
"production_scheduling",
2025-10-30 21:08:07 +01:00
"orchestrator_integration", # NEW: Orchestrator-driven scheduling
2025-09-29 13:13:12 +02:00
"quality_control",
"equipment_management",
"capacity_planning",
"alert_notifications"
]
def setup_custom_middleware(self):
"""Setup custom middleware for production service"""
@self.app.middleware("http")
async def logging_middleware(request: Request, call_next):
"""Add request logging middleware"""
start_time = time.time()
response = await call_next(request)
process_time = time.time() - start_time
self.logger.info("HTTP request processed",
method=request.method,
url=str(request.url),
status_code=response.status_code,
process_time=round(process_time, 4))
return response
# Create service instance
service = ProductionService()
# Create FastAPI app with standardized setup
app = service.create_app()
# Setup standard endpoints
service.setup_standard_endpoints()
# Setup custom middleware
service.setup_custom_middleware()
2025-08-21 20:28:14 +02:00
2025-10-06 15:27:01 +02:00
# Include standardized routers
# NOTE: Register more specific routes before generic parameterized routes
2025-11-02 20:24:44 +01:00
# IMPORTANT: Register audit router FIRST to avoid route matching conflicts
service.add_router(audit.router)
2025-12-05 20:07:01 +01:00
service.add_router(batch.router)
2025-10-30 21:08:07 +01:00
service.add_router(orchestrator.router) # NEW: Orchestrator integration endpoint
2025-10-31 18:57:58 +01:00
service.add_router(production_orders_operations.router) # Tenant deletion endpoints
service.add_router(quality_templates.router) # Register first to avoid route conflicts
2025-10-19 19:22:37 +02:00
service.add_router(equipment.router)
2025-10-06 15:27:01 +02:00
service.add_router(production_batches.router)
service.add_router(production_schedules.router)
service.add_router(production_operations.router)
service.add_router(production_dashboard.router)
service.add_router(analytics.router)
2025-12-13 23:57:54 +01:00
service.add_router(internal_demo.router, tags=["internal-demo"])
2025-11-05 13:34:56 +01:00
service.add_router(ml_insights.router) # ML insights endpoint
2025-12-13 23:57:54 +01:00
service.add_router(ml_insights.internal_router) # Internal ML insights endpoint for demo cloning
2025-12-05 20:07:01 +01:00
service.add_router(internal_alert_trigger_router) # Internal alert trigger for demo cloning
2025-08-21 20:28:14 +02:00
2025-10-30 21:08:07 +01:00
# REMOVED: test_production_scheduler endpoint
# Production scheduling is now triggered by the Orchestrator Service
2025-10-09 18:01:24 +02:00
2025-08-21 20:28:14 +02:00
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"main:app",
host="0.0.0.0",
port=8000,
reload=settings.DEBUG
2025-12-13 23:57:54 +01:00
)