From 364af2c764077777b50e0b6f43807a8b846a5e03 Mon Sep 17 00:00:00 2001 From: Andrei Markin Date: Wed, 31 Jan 2024 23:10:05 +0400 Subject: [PATCH] Improve processing commands * If commands handler raises TerkaAddedToEntity exception, continue execution --- terka/service_layer/handlers.py | 2 +- terka/service_layer/messagebus.py | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/terka/service_layer/handlers.py b/terka/service_layer/handlers.py index a258b02..a157873 100644 --- a/terka/service_layer/handlers.py +++ b/terka/service_layer/handlers.py @@ -575,7 +575,7 @@ def _process_extra_args(id, context, uow): if comment := context.get("comment"): uow.published_messages.append( commands.CommentTask(id=id, text=comment)) - if hours := context.get("time_spent"): + if hours := context.get("hours"): uow.published_messages.append(commands.TrackTask(id=id, hours=hours)) @register(cmd=commands.ListTask) diff --git a/terka/service_layer/messagebus.py b/terka/service_layer/messagebus.py index 62e1072..73d87a9 100644 --- a/terka/service_layer/messagebus.py +++ b/terka/service_layer/messagebus.py @@ -1,6 +1,8 @@ from __future__ import annotations from typing import Type +import logging +from terka import exceptions from terka.adapters import publisher from terka.domain import commands, events from terka.service_layer import unit_of_work, handlers @@ -41,9 +43,12 @@ def handle(self, message: Message, context: dict = {}): def handle_command(self, command: commands.Command, context: dict) -> None: handler = self.command_handlers[type(command)] - if result := handler(command, self, context): - self.return_value = result - self.queue.extend(self.uow.collect_new_events()) + try: + if result := handler(command, self, context): + self.return_value = result + self.queue.extend(self.uow.collect_new_events()) + except exceptions.TaskAddedToEntity: + logging.warning("Task already added to compound entity") def handle_event(self, event: events.Event, context: dict) -> None: for handler in self.event_handlers[type(event)]: