From d8c24cf5e9a549482ac489d890b6f4ce78033af3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Praszmo?= Date: Mon, 25 Mar 2024 13:40:02 +0100 Subject: [PATCH 1/9] Correctly handle timeoutexception --- karton/core/karton.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/karton/core/karton.py b/karton/core/karton.py index 88fc058..0196e04 100644 --- a/karton/core/karton.py +++ b/karton/core/karton.py @@ -12,6 +12,7 @@ from .backend import KartonBackend, KartonBind, KartonMetrics from .base import KartonBase, KartonServiceBase from .config import Config +from .exceptions import TimeoutException from .resource import LocalResource from .task import Task, TaskState from .utils import timeout @@ -179,14 +180,14 @@ def internal_process(self, task: Task) -> None: self.process(self.current_task) else: self.process(self.current_task) - except Exception as exc: + except (Exception, TimeoutException) as exc: saved_exception = exc raise finally: self._run_post_hooks(saved_exception) self.log.info("Task done - %s", self.current_task.uid) - except Exception: + except (Exception, TimeoutException): exc_info = sys.exc_info() exception_str = traceback.format_exception(*exc_info) From 79b6edb3ef77e553fb7215a4be52c6fbc6092f38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Praszmo?= Date: Mon, 25 Mar 2024 13:44:19 +0100 Subject: [PATCH 2/9] Fix name typo --- karton/core/karton.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/karton/core/karton.py b/karton/core/karton.py index 0196e04..84556cc 100644 --- a/karton/core/karton.py +++ b/karton/core/karton.py @@ -12,7 +12,7 @@ from .backend import KartonBackend, KartonBind, KartonMetrics from .base import KartonBase, KartonServiceBase from .config import Config -from .exceptions import TimeoutException +from .exceptions import TaskTimeoutError from .resource import LocalResource from .task import Task, TaskState from .utils import timeout @@ -180,14 +180,14 @@ def internal_process(self, task: Task) -> None: self.process(self.current_task) else: self.process(self.current_task) - except (Exception, TimeoutException) as exc: + except (Exception, TaskTimeoutError) as exc: saved_exception = exc raise finally: self._run_post_hooks(saved_exception) self.log.info("Task done - %s", self.current_task.uid) - except (Exception, TimeoutException): + except (Exception, TaskTimeoutError): exc_info = sys.exc_info() exception_str = traceback.format_exception(*exc_info) From c1dc0269150ac9d41b9130254293cecee8870d6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Praszmo?= Date: Mon, 25 Mar 2024 13:50:42 +0100 Subject: [PATCH 3/9] Fix types --- karton/core/karton.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/karton/core/karton.py b/karton/core/karton.py index 84556cc..c2818e8 100644 --- a/karton/core/karton.py +++ b/karton/core/karton.py @@ -6,7 +6,7 @@ import sys import time import traceback -from typing import Any, Callable, Dict, List, Optional, Tuple, cast +from typing import Any, Callable, Dict, List, Optional, Tuple, Union, cast from .__version__ import __version__ from .backend import KartonBackend, KartonBind, KartonMetrics @@ -290,7 +290,9 @@ def _run_pre_hooks(self) -> None: else: self.log.exception("Pre-hook failed") - def _run_post_hooks(self, exception: Optional[Exception]) -> None: + def _run_post_hooks( + self, exception: Optional[Union[Exception, BaseException]] + ) -> None: """ Run registered postprocessing hooks From d20768668b5e8d3e27ddc5f171f43bffe4cb9dcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Praszmo?= Date: Mon, 25 Mar 2024 13:52:38 +0100 Subject: [PATCH 4/9] Fix types again --- karton/core/karton.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/karton/core/karton.py b/karton/core/karton.py index c2818e8..1a87ee0 100644 --- a/karton/core/karton.py +++ b/karton/core/karton.py @@ -130,7 +130,10 @@ def __init__( self.current_task: Optional[Task] = None self._pre_hooks: List[Tuple[Optional[str], Callable[[Task], None]]] = [] self._post_hooks: List[ - Tuple[Optional[str], Callable[[Task, Optional[Exception]], None]] + Tuple[ + Optional[str], + Callable[[Task, Optional[Union[Exception, BaseException]]], None], + ] ] = [] @abc.abstractmethod From 3d058d6603647e4c69bbdcce95e021f0bb7394ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Praszmo?= Date: Mon, 25 Mar 2024 13:58:53 +0100 Subject: [PATCH 5/9] Simplify types --- karton/core/karton.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/karton/core/karton.py b/karton/core/karton.py index 1a87ee0..3001428 100644 --- a/karton/core/karton.py +++ b/karton/core/karton.py @@ -17,6 +17,8 @@ from .task import Task, TaskState from .utils import timeout +KartonTaskException = Union[Exception, BaseException] + class Producer(KartonBase): """ @@ -132,7 +134,7 @@ def __init__( self._post_hooks: List[ Tuple[ Optional[str], - Callable[[Task, Optional[Union[Exception, BaseException]]], None], + Callable[[Task, Optional[KartonTaskException]], None], ] ] = [] @@ -264,7 +266,7 @@ def add_pre_hook( def add_post_hook( self, - callback: Callable[[Task, Optional[Exception]], None], + callback: Callable[[Task, Optional[KartonTaskException]], None], name: Optional[str] = None, ) -> None: """ @@ -293,9 +295,7 @@ def _run_pre_hooks(self) -> None: else: self.log.exception("Pre-hook failed") - def _run_post_hooks( - self, exception: Optional[Union[Exception, BaseException]] - ) -> None: + def _run_post_hooks(self, exception: Optional[KartonTaskException]) -> None: """ Run registered postprocessing hooks From 0d4d30b366ffae61f80fc423a1c9849f51db6ccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Praszmo?= Date: Mon, 25 Mar 2024 14:00:35 +0100 Subject: [PATCH 6/9] Why is this such a headache --- karton/core/karton.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/karton/core/karton.py b/karton/core/karton.py index 3001428..f7be257 100644 --- a/karton/core/karton.py +++ b/karton/core/karton.py @@ -437,7 +437,7 @@ def _send_signaling_status_task_begin(self, task: Task) -> None: self._send_signaling_status_task("task_begin") def _send_signaling_status_task_end( - self, task: Task, ex: Optional[Exception] + self, task: Task, ex: Optional[KartonTaskException] ) -> None: """Send a begin status signaling task. From 6cd1d9dfb78474f351659566deb329fabb2d24be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Praszmo?= Date: Tue, 26 Mar 2024 13:43:29 +0100 Subject: [PATCH 7/9] Use BaseException instead of union --- karton/core/karton.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/karton/core/karton.py b/karton/core/karton.py index f7be257..6fb5992 100644 --- a/karton/core/karton.py +++ b/karton/core/karton.py @@ -17,7 +17,6 @@ from .task import Task, TaskState from .utils import timeout -KartonTaskException = Union[Exception, BaseException] class Producer(KartonBase): @@ -134,7 +133,7 @@ def __init__( self._post_hooks: List[ Tuple[ Optional[str], - Callable[[Task, Optional[KartonTaskException]], None], + Callable[[Task, Optional[BaseException]], None], ] ] = [] @@ -266,7 +265,7 @@ def add_pre_hook( def add_post_hook( self, - callback: Callable[[Task, Optional[KartonTaskException]], None], + callback: Callable[[Task, Optional[BaseException]], None], name: Optional[str] = None, ) -> None: """ @@ -295,7 +294,7 @@ def _run_pre_hooks(self) -> None: else: self.log.exception("Pre-hook failed") - def _run_post_hooks(self, exception: Optional[KartonTaskException]) -> None: + def _run_post_hooks(self, exception: Optional[BaseException]) -> None: """ Run registered postprocessing hooks @@ -437,7 +436,7 @@ def _send_signaling_status_task_begin(self, task: Task) -> None: self._send_signaling_status_task("task_begin") def _send_signaling_status_task_end( - self, task: Task, ex: Optional[KartonTaskException] + self, task: Task, ex: Optional[BaseException] ) -> None: """Send a begin status signaling task. From 14c11997e8b83ad4156a7cb4fd7a7c07ac77b2fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Praszmo?= Date: Tue, 26 Mar 2024 13:44:32 +0100 Subject: [PATCH 8/9] lint --- karton/core/karton.py | 1 - 1 file changed, 1 deletion(-) diff --git a/karton/core/karton.py b/karton/core/karton.py index 6fb5992..0df795b 100644 --- a/karton/core/karton.py +++ b/karton/core/karton.py @@ -18,7 +18,6 @@ from .utils import timeout - class Producer(KartonBase): """ Producer part of Karton. Used for dispatching initial tasks into karton. From df22b0b373383667f394162f0b0f491dfb0cd186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Praszmo?= Date: Tue, 26 Mar 2024 13:45:58 +0100 Subject: [PATCH 9/9] lint --- karton/core/karton.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/karton/core/karton.py b/karton/core/karton.py index 0df795b..a6d7e38 100644 --- a/karton/core/karton.py +++ b/karton/core/karton.py @@ -6,7 +6,7 @@ import sys import time import traceback -from typing import Any, Callable, Dict, List, Optional, Tuple, Union, cast +from typing import Any, Callable, Dict, List, Optional, Tuple, cast from .__version__ import __version__ from .backend import KartonBackend, KartonBind, KartonMetrics