49 lines
1.0 KiB
Python
Executable File
49 lines
1.0 KiB
Python
Executable File
import logging
|
|
from dataclasses import dataclass
|
|
from typing import List, Callable, Any
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@dataclass
|
|
class SegmentStartEvent:
|
|
segment_id: str
|
|
timestamp: float
|
|
|
|
|
|
@dataclass
|
|
class SegmentEndEvent:
|
|
segment_id: str
|
|
duration: float
|
|
timestamp: float
|
|
|
|
|
|
@dataclass
|
|
class ExecutionErrorEvent:
|
|
segment_id: str
|
|
error_msg: str
|
|
timestamp: float
|
|
|
|
|
|
class RuntimeEventBus:
|
|
def __init__(self):
|
|
self._subscribers: List[Callable] = []
|
|
self._errors: List[Exception] = []
|
|
|
|
def subscribe(self, callback: Callable):
|
|
self._subscribers.append(callback)
|
|
|
|
def publish(self, event: Any):
|
|
for callback in self._subscribers:
|
|
try:
|
|
callback(event)
|
|
except Exception as e:
|
|
self._errors.append(e)
|
|
logger.warning(f"RuntimeEventBus handler error: {e}")
|
|
|
|
def get_errors(self) -> List[Exception]:
|
|
return self._errors.copy()
|
|
|
|
def clear_errors(self) -> None:
|
|
self._errors.clear()
|