128 lines
4.4 KiB
Python
128 lines
4.4 KiB
Python
# services/inventory/app/main.py
|
|
"""
|
|
Inventory Service FastAPI Application
|
|
"""
|
|
|
|
import os
|
|
from fastapi import FastAPI
|
|
from sqlalchemy import text
|
|
|
|
# Import core modules
|
|
from app.core.config import settings
|
|
from app.core.database import database_manager
|
|
from app.api import ingredients, stock, classification, transformations
|
|
from app.services.inventory_alert_service import InventoryAlertService
|
|
from shared.service_base import StandardFastAPIService
|
|
|
|
# Import enhanced routers
|
|
from app.api.dashboard import router as dashboard_router
|
|
from app.api.food_safety import router as food_safety_router
|
|
|
|
|
|
class InventoryService(StandardFastAPIService):
|
|
"""Inventory Service with standardized setup"""
|
|
|
|
expected_migration_version = "001_initial_inventory"
|
|
|
|
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
|
|
|
|
def __init__(self):
|
|
# Define expected database tables for health checks
|
|
inventory_expected_tables = [
|
|
'ingredients', 'stock', 'stock_movements', 'product_transformations',
|
|
'stock_alerts', 'food_safety_compliance', 'temperature_logs', 'food_safety_alerts'
|
|
]
|
|
|
|
super().__init__(
|
|
service_name="inventory-service",
|
|
app_name=settings.APP_NAME,
|
|
description=settings.DESCRIPTION,
|
|
version=settings.VERSION,
|
|
log_level=settings.LOG_LEVEL,
|
|
cors_origins=settings.CORS_ORIGINS,
|
|
api_prefix=settings.API_V1_STR,
|
|
database_manager=database_manager,
|
|
expected_tables=inventory_expected_tables
|
|
)
|
|
|
|
async def on_startup(self, app: FastAPI):
|
|
"""Custom startup logic for inventory service"""
|
|
# Initialize alert service
|
|
alert_service = InventoryAlertService(settings)
|
|
await alert_service.start()
|
|
self.logger.info("Inventory alert service started")
|
|
|
|
# Store alert service in app state
|
|
app.state.alert_service = alert_service
|
|
|
|
async def on_shutdown(self, app: FastAPI):
|
|
"""Custom shutdown logic for inventory service"""
|
|
# Stop alert service
|
|
if hasattr(app.state, 'alert_service'):
|
|
await app.state.alert_service.stop()
|
|
self.logger.info("Alert service stopped")
|
|
|
|
def get_service_features(self):
|
|
"""Return inventory-specific features"""
|
|
return [
|
|
"ingredient_management",
|
|
"stock_tracking",
|
|
"expiration_alerts",
|
|
"low_stock_alerts",
|
|
"batch_tracking",
|
|
"fifo_consumption",
|
|
"barcode_support",
|
|
"food_safety_compliance",
|
|
"temperature_monitoring",
|
|
"dashboard_analytics",
|
|
"business_model_detection",
|
|
"real_time_alerts",
|
|
"regulatory_reporting"
|
|
]
|
|
|
|
|
|
# Create service instance
|
|
service = InventoryService()
|
|
|
|
# Create FastAPI app with standardized setup
|
|
app = service.create_app()
|
|
|
|
# Setup standard endpoints
|
|
service.setup_standard_endpoints()
|
|
|
|
# Include routers using the service helper
|
|
service.add_router(ingredients.router)
|
|
service.add_router(stock.router)
|
|
service.add_router(transformations.router)
|
|
service.add_router(classification.router)
|
|
service.add_router(dashboard_router)
|
|
service.add_router(food_safety_router)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
|
|
uvicorn.run(
|
|
"app.main:app",
|
|
host="0.0.0.0",
|
|
port=8000,
|
|
reload=os.getenv("RELOAD", "false").lower() == "true",
|
|
log_level="info"
|
|
) |