freeleaps-service-hub/apps/devops/app/providers/message_queue.py
2025-07-30 10:50:22 +08:00

30 lines
1.2 KiB
Python

import asyncio
from app.backend.infra.rabbitmq.async_subscriber import AsyncMQSubscriber
from app.backend.services.deployment_status_update_service import DeploymentStatusUpdateService
def register(app):
# Initialize the message subscriber and status update service
app.deployment_heartbeat_subscriber = AsyncMQSubscriber("devops_reconcile_heartbeat")
app.deployment_status_service = DeploymentStatusUpdateService()
@app.on_event("startup")
async def start_message_consumers():
# Register the heartbeat processor
await app.deployment_heartbeat_subscriber.register_consumer(
registry_key="deployment_heartbeat_processor",
callback_method=app.deployment_status_service.process_heartbeat_message,
args={}
)
# Start the subscriber
loop = asyncio.get_running_loop()
await loop.create_task(
app.deployment_heartbeat_subscriber.subscribe(max_retries=5, event_loop=loop)
)
@app.on_event("shutdown")
async def stop_message_consumers():
# Clear consumers and close connection
await app.deployment_heartbeat_subscriber.clear_all_consumers()
await app.deployment_heartbeat_subscriber.close()