25 lines
976 B
Python
25 lines
976 B
Python
"""
|
|
@event Event Publisher (Backend/app/events/publisher.py)
|
|
@purpose Event Bus publisher for broadcasting domain events across Commerce, Repair, and Core services.
|
|
"""
|
|
from typing import Dict, Any, List, Callable
|
|
import logging
|
|
|
|
logger = logging.getLogger("EventBus")
|
|
|
|
_SUBSCRIBERS: Dict[str, List[Callable[[Dict[str, Any]], None]]] = {}
|
|
|
|
def subscribe(event_type: str, handler: Callable[[Dict[str, Any]], None]):
|
|
if event_type not in _SUBSCRIBERS:
|
|
_SUBSCRIBERS[event_type] = []
|
|
_SUBSCRIBERS[event_type].append(handler)
|
|
logger.info(f"Subscribed handler for event: {event_type}")
|
|
|
|
def publish_event(event_type: str, payload: Dict[str, Any]):
|
|
logger.info(f"Publishing event [{event_type}] with payload keys: {list(payload.keys())}")
|
|
handlers = _SUBSCRIBERS.get(event_type, [])
|
|
for handler in handlers:
|
|
try:
|
|
handler(payload)
|
|
except Exception as e:
|
|
logger.error(f"Error handling event [{event_type}]: {e}")
|