From 7b4994a07ea9d3f42e796950856a017f887dabb8 Mon Sep 17 00:00:00 2001 From: SebastiaanZ <33516116+SebastiaanZ@users.noreply.github.com> Date: Mon, 15 Apr 2019 07:16:26 +0200 Subject: Preventing the ot-names cycling bug by sleeping until one minute past midnight --- bot/cogs/off_topic_names.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bot/cogs/off_topic_names.py b/bot/cogs/off_topic_names.py index 4dde9169f..0b0246667 100644 --- a/bot/cogs/off_topic_names.py +++ b/bot/cogs/off_topic_names.py @@ -48,8 +48,11 @@ async def update_names(bot: Bot, headers: dict): """ 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. + today_at_midnight = datetime.utcnow().replace(microsecond=0, second=0, minute=0, hour=0) - next_midnight = today_at_midnight + timedelta(days=1) + next_midnight = today_at_midnight + timedelta(days=1, minutes=1) seconds_to_sleep = (next_midnight - datetime.utcnow()).seconds await asyncio.sleep(seconds_to_sleep) -- cgit v1.2.3 From 599f23c18304113dfb8877810d654727c3367e2c Mon Sep 17 00:00:00 2001 From: SebastiaanZ <33516116+SebastiaanZ@users.noreply.github.com> Date: Mon, 15 Apr 2019 08:39:45 +0200 Subject: Changing the way the bug is handled, by courtesy of Mark --- bot/cogs/off_topic_names.py | 14 +++++++++----- 1 file 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( -- cgit v1.2.3 From ef6ea2fb05c4cbb987494de1f1a37e213aeffc51 Mon Sep 17 00:00:00 2001 From: SebastiaanZ <33516116+SebastiaanZ@users.noreply.github.com> Date: Mon, 15 Apr 2019 08:48:23 +0200 Subject: Cleaning up the fix by removing unnecessary initial None and None check --- bot/cogs/off_topic_names.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bot/cogs/off_topic_names.py b/bot/cogs/off_topic_names.py index 44d31464f..99dc725d2 100644 --- a/bot/cogs/off_topic_names.py +++ b/bot/cogs/off_topic_names.py @@ -50,11 +50,8 @@ async def update_names(bot: Bot, headers: dict): # 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 + midnight = midnight = datetime.utcnow().replace(microsecond=0, second=0, minute=0, hour=0) while True: - if midnight is None: - midnight = datetime.utcnow().replace(microsecond=0, second=0, minute=0, hour=0) - midnight += timedelta(days=1) seconds_to_sleep = (midnight - datetime.utcnow()).seconds log.debug(f"update_names: seconds to sleep {seconds_to_sleep}") -- cgit v1.2.3 From cf9485985336aaf0e90617a124c52d6f768c5ba3 Mon Sep 17 00:00:00 2001 From: SebastiaanZ <33516116+SebastiaanZ@users.noreply.github.com> Date: Mon, 15 Apr 2019 09:04:23 +0200 Subject: Fixing the off-by-one-second bug by adding additional second to sleep time --- bot/cogs/off_topic_names.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/bot/cogs/off_topic_names.py b/bot/cogs/off_topic_names.py index 99dc725d2..58a53118e 100644 --- a/bot/cogs/off_topic_names.py +++ b/bot/cogs/off_topic_names.py @@ -47,14 +47,12 @@ 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 = midnight = datetime.utcnow().replace(microsecond=0, second=0, minute=0, hour=0) while True: - midnight += timedelta(days=1) - seconds_to_sleep = (midnight - datetime.utcnow()).seconds - log.debug(f"update_names: seconds to sleep {seconds_to_sleep}") + # Since we truncate the compute timedelta to seconds, we add one second to ensure + # we go past midnight in the `seconds_to_sleep` set below. + today_at_midnight = datetime.utcnow().replace(microsecond=0, second=0, minute=0, hour=0) + next_midnight = today_at_midnight + timedelta(days=1) + seconds_to_sleep = (next_midnight - datetime.utcnow()).seconds + 1 await asyncio.sleep(seconds_to_sleep) response = await bot.http_session.get( -- cgit v1.2.3