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
+12
View File
@@ -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,6 +41,10 @@ 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):
if self.__reload_running:
await ctx.response.send_message("Reload is allready being executed", ephemeral = True)
else:
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 ...")
@@ -70,11 +75,17 @@ class Reload_Command(Base_Cog):
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.check(Maintenance_Command.handle_check)
@app_commands.autocomplete(cog_name = autocomplete_cog)
async def reload(self, ctx: discord.Interaction, cog_name:str):
if self.__reload_running:
await ctx.response.send_message("Reload is allready being executed", ephemeral = True)
else:
self.__reload_running = True
task_start = datetime.now().timestamp()
self._logger.debug(f"Reloading {cog_name} cog ...")
try:
@@ -96,6 +107,7 @@ class Reload_Command(Base_Cog):
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