diff options
author | 2020-12-13 10:40:56 +0100 | |
---|---|---|
committer | 2020-12-13 10:40:56 +0100 | |
commit | 0a91d49969600fa2d5f5b9e429ff693f3b94da72 (patch) | |
tree | 1edb48462f20fbc767de18c651a9ea7cdd083c7c | |
parent | Merge pull request #541 from janine9vn/aoc2020 (diff) |
Add callback to log errors in AoC background tasks
Currently, our Advent of Code background tasks fail without logging
errors or printing error messages. This makes it difficult to debug the
errors and means that they may fail silently.
While we should ideally find the root cause that hides such errors, I've
added a done_callback function in the meantime to help us debug the
current issues with the Advent of Code Notification Task.
-rw-r--r-- | bot/exts/christmas/advent_of_code/_cog.py | 6 | ||||
-rw-r--r-- | bot/exts/christmas/advent_of_code/_helpers.py | 11 |
2 files changed, 17 insertions, 0 deletions
diff --git a/bot/exts/christmas/advent_of_code/_cog.py b/bot/exts/christmas/advent_of_code/_cog.py index 0968dd26..0671203e 100644 --- a/bot/exts/christmas/advent_of_code/_cog.py +++ b/bot/exts/christmas/advent_of_code/_cog.py @@ -30,6 +30,7 @@ AOC_WHITELIST = AOC_WHITELIST_RESTRICTED + (Channels.advent_of_code,) async def countdown_status(bot: commands.Bot) -> None: """Set the playing status of the bot to the minutes & hours left until the next day's challenge.""" + log.info("Started `AoC Status Countdown` task") while _helpers.is_in_advent(): _, time_left = _helpers.time_left_to_aoc_midnight() @@ -62,6 +63,7 @@ async def day_countdown(bot: commands.Bot) -> None: Once we have calculated this we should then sleep that number and when the time is reached, ping the Advent of Code role notifying them that the new challenge is ready. """ + log.info("Started `Daily AoC Notification` task") while _helpers.is_in_advent(): tomorrow, time_left = _helpers.time_left_to_aoc_midnight() @@ -127,9 +129,13 @@ class AdventOfCode(commands.Cog): countdown_coro = day_countdown(self.bot) self.countdown_task = self.bot.loop.create_task(countdown_coro) + self.countdown_task.set_name("Daily AoC Notification") + self.countdown_task.add_done_callback(_helpers.background_task_callback) status_coro = countdown_status(self.bot) self.status_task = self.bot.loop.create_task(status_coro) + self.status_task.set_name("AoC Status Countdown") + self.status_task.add_done_callback(_helpers.background_task_callback) @commands.group(name="adventofcode", aliases=("aoc",)) @override_in_channel(AOC_WHITELIST) diff --git a/bot/exts/christmas/advent_of_code/_helpers.py b/bot/exts/christmas/advent_of_code/_helpers.py index d883c09f..da139e40 100644 --- a/bot/exts/christmas/advent_of_code/_helpers.py +++ b/bot/exts/christmas/advent_of_code/_helpers.py @@ -1,3 +1,4 @@ +import asyncio import collections import datetime import json @@ -407,3 +408,13 @@ def time_left_to_aoc_midnight() -> Tuple[datetime.datetime, datetime.timedelta]: # Calculate the timedelta between the current time and midnight return tomorrow, tomorrow - datetime.datetime.now(EST) + + +def background_task_callback(task: asyncio.Task) -> None: + """Check if the finished background task failed to make sure we log errors.""" + if task.cancelled(): + log.info(f"Background task `{task.get_name()}` was cancelled.") + elif exception := task.exception(): + log.error(f"Background task `{task.get_name()}` failed:", exc_info=exception) + else: + log.info(f"Background task `{task.get_name()}` exited normally.") |