From 7e29e7b48cae52795099357168100233c916cfc3 Mon Sep 17 00:00:00 2001 From: arabcoders Date: Tue, 24 Jun 2025 16:26:56 +0300 Subject: [PATCH] Handle tasks exception --- app/library/Tasks.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/app/library/Tasks.py b/app/library/Tasks.py index 48922a67..6b063916 100644 --- a/app/library/Tasks.py +++ b/app/library/Tasks.py @@ -223,19 +223,19 @@ class Tasks(metaclass=Singleton): if not task.get("name"): msg = "No name found." raise ValueError(msg) - else: - task["name"] = task["name"].strip() + + task["name"] = task["name"].strip() if not task.get("url"): msg = "No URL found." raise ValueError(msg) - else: - task["url"] = task["url"].strip() - try: - validate_url(task["url"], allow_internal=True) - except ValueError as e: - msg = f"Invalid URL format. '{e!s}'." - raise ValueError(msg) from e + + task["url"] = task["url"].strip() + try: + validate_url(task["url"], allow_internal=True) + except ValueError as e: + msg = f"Invalid URL format. '{e!s}'." + raise ValueError(msg) from e if task.get("timer"): try: @@ -380,10 +380,19 @@ class HandleTask: if handler is None: continue - asyncio.create_task(self.dispatch(task, handler=handler), name=f"task-{task.id}") + coro = self.dispatch(task, handler=handler) + t = asyncio.create_task(coro, name=f"task-{task.id}") + t.add_done_callback(lambda fut, t=task: self._handle_exception(fut, t)) except Exception as e: LOG.error(f"Failed to handle task '{task.name}'. '{e!s}'.") + def _handle_exception(self, fut: asyncio.Task, task: Task) -> None: + if fut.cancelled(): + return + + if exc := fut.exception(): + LOG.error(f"Exception while handling task '{task.name}': {exc}") + def _find_handler(self, task: Task) -> type | None: for cls in self._handlers: try: