improve loop cancellation handling

This commit is contained in:
LBlend committed 2024-04-01 20:28:25 +02:00
1 parent e8919a12af
commit 6505b953d5
4 files changed
+33 -28

No files matched your search

+3 -10
View File
@@ -81,17 +81,10 @@ class Birthday(commands.Cog):
else:
self.bot.logger.warning(f"Could not find user with ID {birthday[0]}")
@birthday_check.before_loop
async def before_birthday_check(self):
"""
Syncs loop with the time of day
"""
self.bot.logger.info("Postponing birthday check until midnight")
await discord_utils.sleep_until_midnight(self.bot)
def cog_unload(self):
async def cog_unload(self):
self.bot.logger.info("Unloading cog")
self.birthday_check.cancel()
self.cursor.close()
def fetch_user_birthday(self, user_id: int) -> datetime | None:
"""
+7 -2
View File
@@ -132,10 +132,15 @@ class SocialCredit(commands.Cog):
@fuck_uwu.before_loop
async def before_fuck_uwu(self):
"""
Syncs loop with the time of day
Wait until bot cache is ready
"""
await discord_utils.sleep_until_midnight(self.bot)
await self.bot.wait_until_ready()
async def cog_unload(self):
self.bot.logger.info("Unloading cog")
self.fuck_uwu.cancel()
self.cursor.close()
@add_new_citizen
async def social_punishment(self, user_id: int, points: int, reason: str):
+17 -15
View File
@@ -50,11 +50,7 @@ class Streak(commands.Cog):
)
async def cog_unload(self):
"""
Stop the loops and close the database connection
"""
await self.insert_cache()
self.bot.logger.info("Unloading cog")
self.insert_cache_loop.cancel()
self.streak_check.cancel()
self.cursor.close()
@@ -110,11 +106,19 @@ class Streak(commands.Cog):
await self.insert_cache()
@insert_cache_loop.after_loop
async def on_insert_cache_loop_cancel(self):
self.bot.logger.info("Insert cache loop cancelled")
if self.insert_cache_loop.is_being_cancelled() and self.streak_cache:
await self.insert_cache()
async def insert_cache(self):
"""
Inserts cache into the database
"""
self.bot.logger.info("Inserting cache into database")
# Insert into database
for user_id, user_data in self.streak_cache.items():
self.cursor.execute(
@@ -133,14 +137,15 @@ class Streak(commands.Cog):
),
)
@tasks.loop(hours=24)
@tasks.loop(time=misc_utils.MIDNIGHT)
async def streak_check(self):
"""
Check if anyone has lost their streak
"""
# Clear cache to make sure all streaks are up to date
await self.insert_cache()
if self.streak_cache:
await self.insert_cache()
self.cursor.execute(
"""
@@ -161,14 +166,11 @@ class Streak(commands.Cog):
(streak[1],),
)
@streak_check.before_loop
async def before_streak_check(self):
"""
Syncs loop to the time of day
"""
self.bot.logger.info("Postponing streak check until midnight")
await discord_utils.sleep_until_midnight(self.bot)
@streak_check.after_loop
async def on_streak_check_cancel(self):
self.bot.logger.info("Streak check loop cancelled")
if self.streak_check.is_being_cancelled():
await self.streak_check()
streak_group = app_commands.Group(name="streak", description="Snapchat streaks, men for Discord")
+6 -1
View File
@@ -80,7 +80,7 @@ class WordCloud(commands.Cog):
Insert cache to db, stop tasks and close the database connection on cog unload
"""
await self.insert_cache()
self.bot.logger.info("Unloading cog")
self.insert_cache_loop.cancel()
self.cursor.close()
@@ -141,6 +141,11 @@ class WordCloud(commands.Cog):
self.bot.logger.info("Dumping word cloud cache to database...")
await self.insert_cache()
@insert_cache_loop.after_loop
async def on_insert_cache_loop_cancel(self):
if self.insert_cache_loop.is_being_cancelled() and self.word_freq_cache:
await self.insert_cache()
@commands.Cog.listener("on_message")
async def word_freq_listener(self, message: discord.Message):
"""