Created an lock, so another reload task cant be dispatched while another is still running
This commit is contained in:
+61
-49
@@ -14,6 +14,7 @@ class Reload_Command(Base_Cog):
|
||||
def __init__(self, bot:commands.Bot):
|
||||
self.__bot = bot
|
||||
self.__cached_cog_names:list[str] = None
|
||||
self.__reload_running = False
|
||||
super().__init__(logging.getLogger("cmds.reload"))
|
||||
|
||||
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.check(Maintenance_Command.handle_check)
|
||||
async def reload_all(self, ctx: discord.Interaction):
|
||||
task_start = datetime.now().timestamp()
|
||||
cog_names = list(self.__bot.extensions.keys())
|
||||
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
|
||||
if self.__reload_running:
|
||||
await ctx.response.send_message("Reload is allready being executed", ephemeral = True)
|
||||
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.__reload_running = True
|
||||
task_start = datetime.now().timestamp()
|
||||
cog_names = list(self.__bot.extensions.keys())
|
||||
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:
|
||||
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
|
||||
|
||||
self._logger.info(f"Cogs successfully reloaded after {elapsed_time}")
|
||||
finally:
|
||||
await ctx.response.send_message(embed = embed, ephemeral = True)
|
||||
|
||||
@app_commands.command(name = "reload", description = "Reload specific cog")
|
||||
@app_commands.check(Maintenance_Command.handle_check)
|
||||
@app_commands.autocomplete(cog_name = autocomplete_cog)
|
||||
async def reload(self, ctx: discord.Interaction, cog_name:str):
|
||||
task_start = datetime.now().timestamp()
|
||||
self._logger.debug(f"Reloading {cog_name} cog ...")
|
||||
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
|
||||
if self.__reload_running:
|
||||
await ctx.response.send_message("Reload is allready being executed", ephemeral = True)
|
||||
else:
|
||||
embed = discord.Embed(
|
||||
title = f"Reloading `{cog_name.removeprefix('cogs.').capitalize()}` cog",
|
||||
description = f"Total time spend: `{get_elapsed_time_milliseconds(datetime.now().timestamp() - task_start)}`",
|
||||
color = 0x4BB543)
|
||||
self.__reload_running = True
|
||||
task_start = datetime.now().timestamp()
|
||||
self._logger.debug(f"Reloading {cog_name} cog ...")
|
||||
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)}")
|
||||
finally:
|
||||
await ctx.response.send_message(embed = embed, ephemeral = True)
|
||||
raise error
|
||||
else:
|
||||
embed = discord.Embed(
|
||||
title = f"Reloading `{cog_name.removeprefix('cogs.').capitalize()}` cog",
|
||||
description = f"Total time spend: `{get_elapsed_time_milliseconds(datetime.now().timestamp() - task_start)}`",
|
||||
color = 0x4BB543)
|
||||
|
||||
# Clear the cache
|
||||
self.__cached_cog_names = None
|
||||
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):
|
||||
await bot.add_cog(Reload_Command(bot))
|
||||
Reference in New Issue
Block a user