From ac2d3a565b5c7d5677c02333d12cd99ef9f4ff84 Mon Sep 17 00:00:00 2001 From: Amir Rachum Date: Sat, 24 Feb 2024 20:31:24 +0200 Subject: [PATCH] Exception handling + Py3.12 --- pyproject.toml | 2 +- src/spanreed/apis/obsidian.py | 11 +++++++---- src/spanreed/plugins/habit_tracker.py | 5 +++-- src/spanreed/plugins/spanreed_monitor.py | 9 +++++++-- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2114c78..d1c1766 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ authors = ["Amir Rachum "] readme = "README.md" [tool.poetry.dependencies] -python = "^3.11" +python = "^3.12" todoist-api-python = "*" python-telegram-bot = {extras = ["job-queue", "callback-data"], version = "^20.1"} redis = "*" diff --git a/src/spanreed/apis/obsidian.py b/src/spanreed/apis/obsidian.py index 5dddc70..7660ad0 100644 --- a/src/spanreed/apis/obsidian.py +++ b/src/spanreed/apis/obsidian.py @@ -43,10 +43,13 @@ async def _send_request( self._logger.info(f"Sending request: {request=}") try: - await redis_api.lpush( - f"obsidian-plugin-tasks:{self._user.id}", - json.dumps(request), - ) + async with asyncio.timeout( + datetime.timedelta(seconds=30).total_seconds() + ): + await redis_api.lpush( + f"obsidian-plugin-tasks:{self._user.id}", + json.dumps(request), + ) except Exception: self._logger.error(f"Failed to send request: {request=}") raise diff --git a/src/spanreed/plugins/habit_tracker.py b/src/spanreed/plugins/habit_tracker.py index dd72c57..6cd1fdf 100644 --- a/src/spanreed/plugins/habit_tracker.py +++ b/src/spanreed/plugins/habit_tracker.py @@ -145,6 +145,9 @@ async def run_for_user(self, user: User) -> None: config.habit_tracker_property_name, habit.name, ) + await bot.send_message(f"Awesome! Keep it up!") + else: + await bot.send_message("I'll ask again later") self._logger.info("Sleeping for 4 hours") await asyncio.sleep(datetime.timedelta(hours=4).total_seconds()) @@ -155,8 +158,6 @@ async def poll_user(self, habit: Habit, bot: TelegramBotApi) -> bool: prompt = f"Did you {habit.description} today?" if await bot.request_user_choice(prompt, ["Yes", "No"]) == 0: self._logger.info(f"User said yes to {habit.name}") - await bot.send_message(f"Awesome! Keep it up!") return True self._logger.info(f"User said no to {habit.name}") - await bot.send_message("I'll ask again later") return False diff --git a/src/spanreed/plugins/spanreed_monitor.py b/src/spanreed/plugins/spanreed_monitor.py index 6e504fa..a817d3d 100644 --- a/src/spanreed/plugins/spanreed_monitor.py +++ b/src/spanreed/plugins/spanreed_monitor.py @@ -1,5 +1,6 @@ import asyncio import traceback +import logging import datetime from contextlib import suppress, asynccontextmanager @@ -51,12 +52,16 @@ async def run_for_user(self, user: User) -> None: async def suppress_and_log_exception( *exceptions: type[Exception], ) -> AsyncGenerator[None, None]: + logger = logging.getLogger(__name__) try: yield except Exception as e: if not any(isinstance(e, exception) for exception in exceptions): raise + exception_str = "".join( + traceback.format_exception(type(e), e, None, limit=1) + ) + logger.error(f"Suppressed exception: {exception_str}") await redis_api.lpush( - SpanreedMonitorPlugin.EXCEPTION_QUEUE_NAME, - "".join(traceback.format_exception(type(e), e, None)), + SpanreedMonitorPlugin.EXCEPTION_QUEUE_NAME, exception_str )