2025-07-18 14:41:39 +02:00
|
|
|
# ================================================================
|
|
|
|
|
# services/training/app/services/messaging.py
|
|
|
|
|
# ================================================================
|
2025-07-17 13:09:24 +02:00
|
|
|
"""
|
2025-07-18 14:41:39 +02:00
|
|
|
Messaging service for training service
|
2025-07-17 13:09:24 +02:00
|
|
|
"""
|
|
|
|
|
|
2025-07-18 14:41:39 +02:00
|
|
|
import structlog
|
2025-07-17 13:09:24 +02:00
|
|
|
from shared.messaging.rabbitmq import RabbitMQClient
|
|
|
|
|
from app.core.config import settings
|
2025-07-17 19:46:41 +02:00
|
|
|
|
2025-07-18 14:41:39 +02:00
|
|
|
logger = structlog.get_logger()
|
2025-07-18 14:18:52 +02:00
|
|
|
|
|
|
|
|
# Single global instance
|
|
|
|
|
training_publisher = RabbitMQClient(settings.RABBITMQ_URL, "training-service")
|
|
|
|
|
|
|
|
|
|
async def setup_messaging():
|
|
|
|
|
"""Initialize messaging for training service"""
|
|
|
|
|
success = await training_publisher.connect()
|
|
|
|
|
if success:
|
|
|
|
|
logger.info("Training service messaging initialized")
|
|
|
|
|
else:
|
|
|
|
|
logger.warning("Training service messaging failed to initialize")
|
|
|
|
|
|
|
|
|
|
async def cleanup_messaging():
|
|
|
|
|
"""Cleanup messaging for training service"""
|
|
|
|
|
await training_publisher.disconnect()
|
|
|
|
|
logger.info("Training service messaging cleaned up")
|
|
|
|
|
|
|
|
|
|
# Convenience functions for training-specific events
|
2025-07-18 14:41:39 +02:00
|
|
|
async def publish_training_started(job_data: dict) -> bool:
|
2025-07-18 14:18:52 +02:00
|
|
|
"""Publish training started event"""
|
2025-07-18 14:41:39 +02:00
|
|
|
return await training_publisher.publish_training_event("started", job_data)
|
2025-07-18 14:18:52 +02:00
|
|
|
|
2025-07-18 14:41:39 +02:00
|
|
|
async def publish_training_completed(job_data: dict) -> bool:
|
2025-07-18 14:18:52 +02:00
|
|
|
"""Publish training completed event"""
|
2025-07-18 14:41:39 +02:00
|
|
|
return await training_publisher.publish_training_event("completed", job_data)
|
2025-07-18 14:18:52 +02:00
|
|
|
|
2025-07-18 14:41:39 +02:00
|
|
|
async def publish_training_failed(job_data: dict) -> bool:
|
2025-07-18 14:18:52 +02:00
|
|
|
"""Publish training failed event"""
|
2025-07-18 14:41:39 +02:00
|
|
|
return await training_publisher.publish_training_event("failed", job_data)
|
|
|
|
|
|
|
|
|
|
async def publish_model_validated(model_data: dict) -> bool:
|
|
|
|
|
"""Publish model validation event"""
|
|
|
|
|
return await training_publisher.publish_training_event("model.validated", model_data)
|
|
|
|
|
|
|
|
|
|
async def publish_model_saved(model_data: dict) -> bool:
|
|
|
|
|
"""Publish model saved event"""
|
|
|
|
|
return await training_publisher.publish_training_event("model.saved", model_data)
|