REFACTOR production scheduler

This commit is contained in:
Urtzi Alfaro
2025-10-09 18:01:24 +02:00
parent 3c689b4f98
commit b420af32c5
13 changed files with 4046 additions and 6 deletions

View File

@@ -12,6 +12,7 @@ from sqlalchemy import text
from app.core.config import settings
from app.core.database import database_manager
from app.services.production_alert_service import ProductionAlertService
from app.services.production_scheduler_service import ProductionSchedulerService
from shared.service_base import StandardFastAPIService
# Import standardized routers
@@ -56,8 +57,9 @@ class ProductionService(StandardFastAPIService):
]
self.alert_service = None
self.scheduler_service = None
# Create custom checks for alert service
# Create custom checks for services
async def check_alert_service():
"""Check production alert service health"""
try:
@@ -66,6 +68,14 @@ class ProductionService(StandardFastAPIService):
self.logger.error("Alert service health check failed", error=str(e))
return False
async def check_scheduler_service():
"""Check production scheduler service health"""
try:
return bool(self.scheduler_service) if self.scheduler_service else False
except Exception as e:
self.logger.error("Scheduler service health check failed", error=str(e))
return False
super().__init__(
service_name=settings.SERVICE_NAME,
app_name=settings.APP_NAME,
@@ -74,7 +84,10 @@ class ProductionService(StandardFastAPIService):
api_prefix="", # Empty because RouteBuilder already includes /api/v1
database_manager=database_manager,
expected_tables=production_expected_tables,
custom_health_checks={"alert_service": check_alert_service}
custom_health_checks={
"alert_service": check_alert_service,
"scheduler_service": check_scheduler_service
}
)
async def on_startup(self, app: FastAPI):
@@ -84,11 +97,22 @@ class ProductionService(StandardFastAPIService):
await self.alert_service.start()
self.logger.info("Production alert service started")
# Store alert service in app state
# Initialize production scheduler service
self.scheduler_service = ProductionSchedulerService(settings)
await self.scheduler_service.start()
self.logger.info("Production scheduler service started")
# Store services in app state
app.state.alert_service = self.alert_service
app.state.scheduler_service = self.scheduler_service
async def on_shutdown(self, app: FastAPI):
"""Custom shutdown logic for production service"""
"""Custom startup logic for production service"""
# Stop scheduler service
if self.scheduler_service:
await self.scheduler_service.stop()
self.logger.info("Scheduler service stopped")
# Stop alert service
if self.alert_service:
await self.alert_service.stop()
@@ -100,6 +124,7 @@ class ProductionService(StandardFastAPIService):
"production_planning",
"batch_management",
"production_scheduling",
"automated_daily_scheduling", # NEW: Automated scheduler
"quality_control",
"equipment_management",
"capacity_planning",
@@ -144,6 +169,21 @@ service.add_router(production_dashboard.router)
service.add_router(analytics.router)
@app.post("/test/production-scheduler")
async def test_production_scheduler():
"""Test endpoint to manually trigger production scheduler"""
try:
if hasattr(app.state, 'scheduler_service'):
scheduler_service = app.state.scheduler_service
await scheduler_service.test_production_schedule_generation()
return {"message": "Production scheduler test triggered successfully"}
else:
return {"error": "Scheduler service not available"}
except Exception as e:
service.logger.error("Error testing production scheduler", error=str(e))
return {"error": f"Failed to trigger scheduler test: {str(e)}"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(