diff options
| author | 2020-12-01 22:09:57 +0100 | |
|---|---|---|
| committer | 2020-12-01 22:09:57 +0100 | |
| commit | 82a600747e4142f1c7073c2be57c520be6455a87 (patch) | |
| tree | 1b9113956e08dea6e8a87b2eb4f5418d77419260 | |
| parent | Fix daily stats by converting day, star to str (diff) | |
Add function that allows tasks to sleep until AoC
Our Advent of Code background tasks were written in such a way that
they relied on the extension being loaded in December and only in
December.
However, since the deseasonification, the extension is loaded prior to
December, with some commands being locked to that month with a check
instead. This meant that our background tasks immediately cancelled
themselves, as they observed themselves to be running outside of the
boundaries of the event. As there was no mechanism for starting them
back up again, these tasks would only start running again after
redeployment of Sir Lancebot.
To solve this issue, I've added a helper function that allows tasks to
wait until a x hours before the event starts. This allows them to wake
up in time to prepare for the release of the first puzzle.
| -rw-r--r-- | bot/exts/christmas/advent_of_code/_helpers.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/bot/exts/christmas/advent_of_code/_helpers.py b/bot/exts/christmas/advent_of_code/_helpers.py index f4a20955..145fa30a 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 @@ -362,3 +363,24 @@ 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) + + +async def wait_for_advent_of_code(*, hours_before: int = 1) -> None: + """ + Wait for the Advent of Code event to start. + + This function returns `hours_before` (default: 1) before the Advent of Code + actually starts. This allows functions to schedule and execute code that + needs to run before the event starts. + """ + start = datetime.datetime(AdventOfCode.year, 12, 1, 0, 0, 0, tzinfo=EST) + target = start - datetime.timedelta(hours=hours_before) + now = datetime.datetime.now(EST) + + # If we've already reached or passed to target, we + # simply return immediately. + if now >= target: + return + + delta = target - now + await asyncio.sleep(delta.total_seconds()) |