From 60d5352f157de897a4e472a1adf8d241188f60f6 Mon Sep 17 00:00:00 2001 From: Akarys42 Date: Mon, 23 Sep 2019 19:50:21 +0200 Subject: Catch error in case of non successfull API call for new ot-names This allows to keep the task running even if the call fail. --- bot/cogs/off_topic_names.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/bot/cogs/off_topic_names.py b/bot/cogs/off_topic_names.py index 8f1af347a..cd3d78403 100644 --- a/bot/cogs/off_topic_names.py +++ b/bot/cogs/off_topic_names.py @@ -49,9 +49,12 @@ async def update_names(bot: Bot) -> None: seconds_to_sleep = (next_midnight - datetime.utcnow()).seconds + 1 await asyncio.sleep(seconds_to_sleep) - channel_0_name, channel_1_name, channel_2_name = await bot.api_client.get( - 'bot/off-topic-channel-names', params={'random_items': 3} - ) + try: + channel_0_name, channel_1_name, channel_2_name = await bot.api_client.get( + 'bot/off-topic-channel-names', params={'random_items': 3} + ) + except bot.api.ResponseCodeError as e: + log.error(f"Failed to get new off topic channel names: code {e.response.status}") channel_0, channel_1, channel_2 = (bot.get_channel(channel_id) for channel_id in CHANNELS) await channel_0.edit(name=f'ot0-{channel_0_name}') -- cgit v1.2.3 From 258b0bb2ff98195e1ef07e1ce294a05a5b3aa690 Mon Sep 17 00:00:00 2001 From: Matteo Bertucci Date: Tue, 24 Sep 2019 07:20:44 +0200 Subject: Add `continue` in the except block --- bot/cogs/off_topic_names.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bot/cogs/off_topic_names.py b/bot/cogs/off_topic_names.py index cd3d78403..0e6c20423 100644 --- a/bot/cogs/off_topic_names.py +++ b/bot/cogs/off_topic_names.py @@ -55,6 +55,7 @@ async def update_names(bot: Bot) -> None: ) except bot.api.ResponseCodeError as e: log.error(f"Failed to get new off topic channel names: code {e.response.status}") + continue channel_0, channel_1, channel_2 = (bot.get_channel(channel_id) for channel_id in CHANNELS) await channel_0.edit(name=f'ot0-{channel_0_name}') -- cgit v1.2.3 From 48b2558a1cff7fd089e0662d355a75aea61f484a Mon Sep 17 00:00:00 2001 From: Matteo Bertucci Date: Tue, 24 Sep 2019 13:08:37 +0200 Subject: Fix importing error `bot.api.ResponseCodeError` is now imported --- bot/cogs/off_topic_names.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/cogs/off_topic_names.py b/bot/cogs/off_topic_names.py index 0e6c20423..487ce6852 100644 --- a/bot/cogs/off_topic_names.py +++ b/bot/cogs/off_topic_names.py @@ -9,6 +9,7 @@ from discord.ext.commands import BadArgument, Bot, Cog, Context, Converter, grou from bot.constants import Channels, MODERATION_ROLES from bot.decorators import with_role from bot.pagination import LinePaginator +from bot.api import ResponseCodeError CHANNELS = (Channels.off_topic_0, Channels.off_topic_1, Channels.off_topic_2) @@ -53,7 +54,7 @@ async def update_names(bot: Bot) -> None: channel_0_name, channel_1_name, channel_2_name = await bot.api_client.get( 'bot/off-topic-channel-names', params={'random_items': 3} ) - except bot.api.ResponseCodeError as e: + except ResponseCodeError as e: log.error(f"Failed to get new off topic channel names: code {e.response.status}") continue channel_0, channel_1, channel_2 = (bot.get_channel(channel_id) for channel_id in CHANNELS) -- cgit v1.2.3 From 152f11d2d17bcb999af518bd78eec125353bd305 Mon Sep 17 00:00:00 2001 From: Matteo Bertucci Date: Tue, 24 Sep 2019 13:15:57 +0200 Subject: Override waiting time to half an hour If an exception occurred --- bot/cogs/off_topic_names.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bot/cogs/off_topic_names.py b/bot/cogs/off_topic_names.py index 487ce6852..bdece9161 100644 --- a/bot/cogs/off_topic_names.py +++ b/bot/cogs/off_topic_names.py @@ -42,13 +42,15 @@ class OffTopicName(Converter): async def update_names(bot: Bot) -> None: """Background updater task that performs the daily channel name update.""" + skip_sleep = False while True: # 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) + if skip_sleep is False: + await asyncio.sleep(seconds_to_sleep) try: channel_0_name, channel_1_name, channel_2_name = await bot.api_client.get( @@ -56,6 +58,8 @@ async def update_names(bot: Bot) -> None: ) except ResponseCodeError as e: log.error(f"Failed to get new off topic channel names: code {e.response.status}") + skip_sleep = True + await asyncio.sleep(1800) continue channel_0, channel_1, channel_2 = (bot.get_channel(channel_id) for channel_id in CHANNELS) @@ -66,6 +70,7 @@ async def update_names(bot: Bot) -> None: "Updated off-topic channel names to" f" {channel_0_name}, {channel_1_name} and {channel_2_name}" ) + skip_sleep = False class OffTopicNames(Cog): -- cgit v1.2.3 From c524b59941ac2ce3638fba9b941e6c6e0de10ee9 Mon Sep 17 00:00:00 2001 From: Matteo Bertucci Date: Tue, 24 Sep 2019 13:30:50 +0200 Subject: Put import in alphabetical order --- bot/cogs/off_topic_names.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/off_topic_names.py b/bot/cogs/off_topic_names.py index bdece9161..618f9b927 100644 --- a/bot/cogs/off_topic_names.py +++ b/bot/cogs/off_topic_names.py @@ -6,10 +6,10 @@ from datetime import datetime, timedelta from discord import Colour, Embed from discord.ext.commands import BadArgument, Bot, Cog, Context, Converter, group +from bot.api import ResponseCodeError from bot.constants import Channels, MODERATION_ROLES from bot.decorators import with_role from bot.pagination import LinePaginator -from bot.api import ResponseCodeError CHANNELS = (Channels.off_topic_0, Channels.off_topic_1, Channels.off_topic_2) -- cgit v1.2.3 From f2381f8355fbc54a292506a9cbf7b97928f6c421 Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff <33516116+SebastiaanZ@users.noreply.github.com> Date: Tue, 24 Sep 2019 19:25:30 +0200 Subject: Remove repeat logic for off-topic-name api call After a short discussion in the core-dev team, we decided to not use retry logic for a failed API call for new off-topic-names. We may introduce something similar in the future, but we're not sure on the direction we want to take yet. --- bot/cogs/off_topic_names.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/bot/cogs/off_topic_names.py b/bot/cogs/off_topic_names.py index 618f9b927..16717d523 100644 --- a/bot/cogs/off_topic_names.py +++ b/bot/cogs/off_topic_names.py @@ -42,15 +42,13 @@ class OffTopicName(Converter): async def update_names(bot: Bot) -> None: """Background updater task that performs the daily channel name update.""" - skip_sleep = False while True: # 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 - if skip_sleep is False: - await asyncio.sleep(seconds_to_sleep) + await asyncio.sleep(seconds_to_sleep) try: channel_0_name, channel_1_name, channel_2_name = await bot.api_client.get( @@ -58,8 +56,6 @@ async def update_names(bot: Bot) -> None: ) except ResponseCodeError as e: log.error(f"Failed to get new off topic channel names: code {e.response.status}") - skip_sleep = True - await asyncio.sleep(1800) continue channel_0, channel_1, channel_2 = (bot.get_channel(channel_id) for channel_id in CHANNELS) @@ -70,7 +66,6 @@ async def update_names(bot: Bot) -> None: "Updated off-topic channel names to" f" {channel_0_name}, {channel_1_name} and {channel_2_name}" ) - skip_sleep = False class OffTopicNames(Cog): -- cgit v1.2.3