Created an lock, so another reload task cant be dispatched while another is still running

This commit is contained in:
2024-10-31 09:10:20 +01:00
parent 70a8974e28
commit c8eb3f4014
+62 -50
View File
@@ -14,6 +14,7 @@ class Reload_Command(Base_Cog):
def __init__(self, bot:commands.Bot): def __init__(self, bot:commands.Bot):
self.__bot = bot self.__bot = bot
self.__cached_cog_names:list[str] = None self.__cached_cog_names:list[str] = None
self.__reload_running = False
super().__init__(logging.getLogger("cmds.reload")) super().__init__(logging.getLogger("cmds.reload"))
async def autocomplete_cog(self, ctx: discord.Interaction, cog_name_stw:str): async def autocomplete_cog(self, ctx: discord.Interaction, cog_name_stw:str):
@@ -40,65 +41,76 @@ class Reload_Command(Base_Cog):
@app_commands.command(name = "reload_all", description = "Reloads all cogs") @app_commands.command(name = "reload_all", description = "Reloads all cogs")
@app_commands.check(Maintenance_Command.handle_check) @app_commands.check(Maintenance_Command.handle_check)
async def reload_all(self, ctx: discord.Interaction): async def reload_all(self, ctx: discord.Interaction):
task_start = datetime.now().timestamp() if self.__reload_running:
cog_names = list(self.__bot.extensions.keys()) await ctx.response.send_message("Reload is allready being executed", ephemeral = True)
self._logger.debug(f"Reloading all {len(cog_names)} cogs ...")
try:
embed_cog_stats = "Reloaded the following cogs:\n"
for extension_name in cog_names:
start_reload = datetime.now().timestamp()
await self.__bot.reload_extension(extension_name)
embed_cog_stats += f"- {extension_name.removeprefix('cogs.').capitalize()} (`{get_elapsed_time_milliseconds(datetime.now().timestamp() - start_reload)}`)\n"
except Exception as error:
embed_cog_stats += f"- {extension_name.removeprefix('cogs.').capitalize()} <--"
traceback_str = truncate_message_with_notice(traceback.format_exc(), 1800, "\nSee console for more details on the full traceback")
embed = discord.Embed(
title = "Reloading All Cogs",
description = f"{embed_cog_stats} \n\nDuring the reload the following exception occured:\n```{traceback_str}```",
color = 0xED4337)
raise error
else: else:
elapsed_time = get_elapsed_time_milliseconds(datetime.now().timestamp() - task_start) self.__reload_running = True
embed = discord.Embed( task_start = datetime.now().timestamp()
title = "Reloading All Cogs", cog_names = list(self.__bot.extensions.keys())
description = f"{embed_cog_stats}Total time spend: `{elapsed_time}`", self._logger.debug(f"Reloading all {len(cog_names)} cogs ...")
color = 0x4BB543) try:
embed_cog_stats = "Reloaded the following cogs:\n"
for extension_name in cog_names:
start_reload = datetime.now().timestamp()
await self.__bot.reload_extension(extension_name)
embed_cog_stats += f"- {extension_name.removeprefix('cogs.').capitalize()} (`{get_elapsed_time_milliseconds(datetime.now().timestamp() - start_reload)}`)\n"
self._logger.info(f"Cogs successfully reloaded after {elapsed_time}") except Exception as error:
finally: embed_cog_stats += f"- {extension_name.removeprefix('cogs.').capitalize()} <--"
await ctx.response.send_message(embed = embed, ephemeral = True) traceback_str = truncate_message_with_notice(traceback.format_exc(), 1800, "\nSee console for more details on the full traceback")
embed = discord.Embed(
title = "Reloading All Cogs",
description = f"{embed_cog_stats} \n\nDuring the reload the following exception occured:\n```{traceback_str}```",
color = 0xED4337)
raise error
else:
elapsed_time = get_elapsed_time_milliseconds(datetime.now().timestamp() - task_start)
embed = discord.Embed(
title = "Reloading All Cogs",
description = f"{embed_cog_stats}Total time spend: `{elapsed_time}`",
color = 0x4BB543)
self._logger.info(f"Cogs successfully reloaded after {elapsed_time}")
finally:
await ctx.response.send_message(embed = embed, ephemeral = True)
self.__reload_running = False
@app_commands.command(name = "reload", description = "Reload specific cog") @app_commands.command(name = "reload", description = "Reload specific cog")
@app_commands.check(Maintenance_Command.handle_check) @app_commands.check(Maintenance_Command.handle_check)
@app_commands.autocomplete(cog_name = autocomplete_cog) @app_commands.autocomplete(cog_name = autocomplete_cog)
async def reload(self, ctx: discord.Interaction, cog_name:str): async def reload(self, ctx: discord.Interaction, cog_name:str):
task_start = datetime.now().timestamp() if self.__reload_running:
self._logger.debug(f"Reloading {cog_name} cog ...") await ctx.response.send_message("Reload is allready being executed", ephemeral = True)
try:
await self.__bot.reload_extension(cog_name)
except Exception as error:
traceback_str = truncate_message_with_notice(traceback.format_exc(), 1800, "\nSee console for more details on the full traceback")
embed = discord.Embed(
title = f"Reloading `{cog_name.removeprefix('cogs.').capitalize()}` cog",
description = f"Reload failed with the following exception:\n```{traceback_str}```",
color = 0xED4337)
raise error
else: else:
embed = discord.Embed( self.__reload_running = True
title = f"Reloading `{cog_name.removeprefix('cogs.').capitalize()}` cog", task_start = datetime.now().timestamp()
description = f"Total time spend: `{get_elapsed_time_milliseconds(datetime.now().timestamp() - task_start)}`", self._logger.debug(f"Reloading {cog_name} cog ...")
color = 0x4BB543) try:
await self.__bot.reload_extension(cog_name)
except Exception as error:
traceback_str = truncate_message_with_notice(traceback.format_exc(), 1800, "\nSee console for more details on the full traceback")
embed = discord.Embed(
title = f"Reloading `{cog_name.removeprefix('cogs.').capitalize()}` cog",
description = f"Reload failed with the following exception:\n```{traceback_str}```",
color = 0xED4337)
self._logger.info(f"Cog {cog_name} successfully reloaded after {get_elapsed_time_milliseconds(datetime.now().timestamp() - task_start)}") raise error
finally: else:
await ctx.response.send_message(embed = embed, ephemeral = True) embed = discord.Embed(
title = f"Reloading `{cog_name.removeprefix('cogs.').capitalize()}` cog",
# Clear the cache description = f"Total time spend: `{get_elapsed_time_milliseconds(datetime.now().timestamp() - task_start)}`",
self.__cached_cog_names = None color = 0x4BB543)
self._logger.info(f"Cog {cog_name} successfully reloaded after {get_elapsed_time_milliseconds(datetime.now().timestamp() - task_start)}")
finally:
await ctx.response.send_message(embed = embed, ephemeral = True)
self.__reload_running = False
# Clear the cache
self.__cached_cog_names = None
async def setup(bot:commands.Bot): async def setup(bot:commands.Bot):
await bot.add_cog(Reload_Command(bot)) await bot.add_cog(Reload_Command(bot))