From f2d54ce23b38c647d23945378396bace9d58d6a5 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Fri, 1 Mar 2024 14:42:45 -0500 Subject: [PATCH] feat(pterodactyl): added a retry counter and sleep cooldown to the error_callback --- pterodactyl/pterodactyl.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pterodactyl/pterodactyl.py b/pterodactyl/pterodactyl.py index 9330fc9..ce5000e 100644 --- a/pterodactyl/pterodactyl.py +++ b/pterodactyl/pterodactyl.py @@ -21,13 +21,16 @@ class Pterodactyl(commands.Cog): self.client: Optional[PterodactylClient] = None self.task: Optional[asyncio.Task] = None self.websocket: Optional[websockets.WebSocketClientProtocol] = None + self.retry_counter: int = 0 register_config(config) async def cog_load(self) -> None: + self.retry_counter = 0 self.task = self.get_task() async def cog_unload(self) -> None: self.task.cancel() + self.retry_counter = 0 await self.client._session.close() # pylint: disable=protected-access def get_task(self) -> asyncio.Task: @@ -44,7 +47,12 @@ class Pterodactyl(commands.Cog): except Exception as e: # pylint: disable=broad-exception-caught logger.error("WebSocket task has failed: %s", e, exc_info=e) self.task.cancel() - self.task = self.get_task() + if self.retry_counter < 5: + self.retry_counter += 1 + asyncio.sleep(5 * self.retry_counter) + self.task = self.get_task() + else: + logger.info("Retry limit reached. Stopping task.") @commands.Cog.listener() async def on_message_without_command(self, message: discord.Message) -> None: @@ -56,6 +64,7 @@ class Pterodactyl(commands.Cog): except websockets.exceptions.ConnectionClosed as e: logger.error("WebSocket connection closed: %s", e) self.task.cancel() + self.retry_counter = 0 self.task = self.get_task() if message.channel.id == await config.chat_channel() and message.author.bot is False: logger.debug("Received chat message from %s: %s", message.author.id, message.content) @@ -69,6 +78,7 @@ class Pterodactyl(commands.Cog): except websockets.exceptions.ConnectionClosed as e: logger.error("WebSocket connection closed: %s", e) self.task.cancel() + self.retry_counter = 0 self.task = self.get_task() async def get_chat_command(self, username: str, message: str, color: discord.Color) -> str: @@ -81,6 +91,7 @@ class Pterodactyl(commands.Cog): if service_name == "pterodactyl": logger.info("Configuration value set: api_key\nRestarting task...") self.task.cancel() + self.retry_counter = 0 self.task = self.get_task() @commands.group(autohelp = True, name = "pterodactyl", aliases = ["ptero"]) @@ -105,6 +116,7 @@ class Pterodactyl(commands.Cog): await ctx.send(f"Base URL set to {base_url}") logger.info("Configuration value set: base_url = %s\nRestarting task...", base_url) self.task.cancel() + self.retry_counter = 0 self.task = self.get_task() @pterodactyl_config.command(name = "serverid") @@ -114,6 +126,7 @@ class Pterodactyl(commands.Cog): await ctx.send(f"Server ID set to {server_id}") logger.info("Configuration value set: server_id = %s\nRestarting task...", server_id) self.task.cancel() + self.retry_counter = 0 self.task = self.get_task() @pterodactyl_config.command(name = "consolechannel")