aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar SebastiaanZ <[email protected]>2019-04-15 08:39:45 +0200
committerGravatar SebastiaanZ <[email protected]>2019-04-15 08:39:45 +0200
commit599f23c18304113dfb8877810d654727c3367e2c (patch)
tree2f1c09bc8433aa416269349dfe72a4d91fc32d33
parentPreventing the ot-names cycling bug by sleeping until one minute past midnight (diff)
Changing the way the bug is handled, by courtesy of Mark
-rw-r--r--bot/cogs/off_topic_names.py14
1 files changed, 9 insertions, 5 deletions
diff --git a/bot/cogs/off_topic_names.py b/bot/cogs/off_topic_names.py
index 0b0246667..44d31464f 100644
--- a/bot/cogs/off_topic_names.py
+++ b/bot/cogs/off_topic_names.py
@@ -47,13 +47,17 @@ async def update_names(bot: Bot, headers: dict):
website via the bot's `http_session`.
"""
+ # To ensure we only cycle once per day, we increase the reference midnight point
+ # by one day each time the task runs instead of relying on "the most recent"
+ # midnight, since that may fail if the task is triggered early.
+ midnight = None
while True:
- # To prevent the bot from sleeping until one second before midnight, we aim
- # for one minute past midnight. This should prevent the ot-names cycling bug.
+ if midnight is None:
+ midnight = datetime.utcnow().replace(microsecond=0, second=0, minute=0, hour=0)
- today_at_midnight = datetime.utcnow().replace(microsecond=0, second=0, minute=0, hour=0)
- next_midnight = today_at_midnight + timedelta(days=1, minutes=1)
- seconds_to_sleep = (next_midnight - datetime.utcnow()).seconds
+ midnight += timedelta(days=1)
+ seconds_to_sleep = (midnight - datetime.utcnow()).seconds
+ log.debug(f"update_names: seconds to sleep {seconds_to_sleep}")
await asyncio.sleep(seconds_to_sleep)
response = await bot.http_session.get(