Slide 5
Slide 5 text
1.
2.
class MessageBus:
def __init__(
self,
uow: unit_of_work.AbstractUnitOfWork,
event_handlers: Dict[Type[events.Event], List[Callable]],
command_handlers: Dict[Type[commands.Command], Callable],
):
self.uow = uow
self.event_handlers = event_handlers
self.command_handlers = command_handlers
def handle(self, message: Message):
self.queue = [message]
while self.queue:
message = self.queue.pop(0)
if isinstance(message, events.Event):
self.handle_event(message)
elif isinstance(message, commands.Command):
self.handle_command(message)
else:
raise Exception(f"{message} was not an Event or
Command")
def handle_event(self, event: events.Event):
for handler in self.event_handlers[type(event)]:
try:
logger.debug("handling event %s with handler %s",
event, handler)
handler(event)
self.queue.extend(self.uow.collect_new_events())
except Exception:
logger.exception("Exception handling event %s", event)
continue
def handle_command(self, command: commands.Command):
logger.debug("handling command %s", command)
try:
handler = self.command_handlers[type(command)]
handler(command)
self.queue.extend(self.uow.collect_new_events())
except Exception:
logger.exception("Exception handling command %s", command)
raise
문제점
Domain Event는 항상 Command 실행 종료 이후에 실행된다
Event Handler에서 이벤트 발생 시점을 기록하고 싶어도 항상 Command 처리 시간 만큼의 delay가 추가된다