Add new infra architecture

This commit is contained in:
Urtzi Alfaro
2026-01-19 11:55:17 +01:00
parent 21d35ea92b
commit 35f164f0cd
311 changed files with 13241 additions and 3700 deletions

View File

@@ -20,28 +20,12 @@ from shared.service_base import StandardFastAPIService
class POSService(StandardFastAPIService):
"""POS Integration Service with standardized setup"""
expected_migration_version = "00001"
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
expected_migration_version = "e9976ec9fe9e"
def __init__(self):
# Initialize scheduler reference
self.pos_scheduler = None
# Define expected database tables for health checks
pos_expected_tables = [
'pos_configurations', 'pos_transactions', 'pos_transaction_items',
@@ -87,15 +71,42 @@ class POSService(StandardFastAPIService):
custom_metrics=pos_custom_metrics
)
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
async def on_startup(self, app: FastAPI):
"""Custom startup logic for POS service"""
# Start background scheduler for POS-to-Sales sync
# Verify migrations first
await self.verify_migrations()
# Call parent startup
await super().on_startup(app)
# Start background scheduler for POS-to-Sales sync with leader election
try:
from app.scheduler import start_scheduler
start_scheduler()
self.logger.info("Background scheduler started successfully")
from app.scheduler import POSScheduler
self.pos_scheduler = POSScheduler(
redis_url=settings.REDIS_URL, # Pass Redis URL for leader election
sync_interval_minutes=settings.SYNC_INTERVAL_SECONDS // 60 if settings.SYNC_INTERVAL_SECONDS >= 60 else 5
)
await self.pos_scheduler.start()
self.logger.info("POS scheduler started successfully with leader election")
# Store scheduler in app state for status checks
app.state.pos_scheduler = self.pos_scheduler
except Exception as e:
self.logger.error(f"Failed to start background scheduler: {e}", exc_info=True)
self.logger.error(f"Failed to start POS scheduler: {e}", exc_info=True)
# Don't fail startup if scheduler fails
# Custom startup completed
@@ -103,13 +114,13 @@ class POSService(StandardFastAPIService):
async def on_shutdown(self, app: FastAPI):
"""Custom shutdown logic for POS service"""
# Shutdown background scheduler
# Shutdown POS scheduler
try:
from app.scheduler import shutdown_scheduler
shutdown_scheduler()
self.logger.info("Background scheduler stopped successfully")
if self.pos_scheduler:
await self.pos_scheduler.stop()
self.logger.info("POS scheduler stopped successfully")
except Exception as e:
self.logger.error(f"Failed to stop background scheduler: {e}", exc_info=True)
self.logger.error(f"Failed to stop POS scheduler: {e}", exc_info=True)
# Database cleanup is handled by the base class
pass