Skip to content

Commit

Permalink
Exception handling + Py3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
Nurdok committed Feb 24, 2024
1 parent 20633e2 commit ac2d3a5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = ["Amir Rachum <[email protected]>"]
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 = "*"
Expand Down
11 changes: 7 additions & 4 deletions src/spanreed/apis/obsidian.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/spanreed/plugins/habit_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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
9 changes: 7 additions & 2 deletions src/spanreed/plugins/spanreed_monitor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import traceback
import logging

import datetime
from contextlib import suppress, asynccontextmanager
Expand Down Expand Up @@ -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
)

0 comments on commit ac2d3a5

Please sign in to comment.