feat(pterodactyl): added a retry counter and sleep cooldown to the error_callback

This commit is contained in:
Seaswimmer 2024-03-01 14:42:45 -05:00
parent b7f5ae644a
commit f2d54ce23b
Signed by untrusted user: cswimr
GPG key ID: B8953EC01E5C4063

View file

@ -21,13 +21,16 @@ class Pterodactyl(commands.Cog):
self.client: Optional[PterodactylClient] = None self.client: Optional[PterodactylClient] = None
self.task: Optional[asyncio.Task] = None self.task: Optional[asyncio.Task] = None
self.websocket: Optional[websockets.WebSocketClientProtocol] = None self.websocket: Optional[websockets.WebSocketClientProtocol] = None
self.retry_counter: int = 0
register_config(config) register_config(config)
async def cog_load(self) -> None: async def cog_load(self) -> None:
self.retry_counter = 0
self.task = self.get_task() self.task = self.get_task()
async def cog_unload(self) -> None: async def cog_unload(self) -> None:
self.task.cancel() self.task.cancel()
self.retry_counter = 0
await self.client._session.close() # pylint: disable=protected-access await self.client._session.close() # pylint: disable=protected-access
def get_task(self) -> asyncio.Task: def get_task(self) -> asyncio.Task:
@ -44,7 +47,12 @@ class Pterodactyl(commands.Cog):
except Exception as e: # pylint: disable=broad-exception-caught except Exception as e: # pylint: disable=broad-exception-caught
logger.error("WebSocket task has failed: %s", e, exc_info=e) logger.error("WebSocket task has failed: %s", e, exc_info=e)
self.task.cancel() self.task.cancel()
if self.retry_counter < 5:
self.retry_counter += 1
asyncio.sleep(5 * self.retry_counter)
self.task = self.get_task() self.task = self.get_task()
else:
logger.info("Retry limit reached. Stopping task.")
@commands.Cog.listener() @commands.Cog.listener()
async def on_message_without_command(self, message: discord.Message) -> None: 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: except websockets.exceptions.ConnectionClosed as e:
logger.error("WebSocket connection closed: %s", e) logger.error("WebSocket connection closed: %s", e)
self.task.cancel() self.task.cancel()
self.retry_counter = 0
self.task = self.get_task() self.task = self.get_task()
if message.channel.id == await config.chat_channel() and message.author.bot is False: 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) 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: except websockets.exceptions.ConnectionClosed as e:
logger.error("WebSocket connection closed: %s", e) logger.error("WebSocket connection closed: %s", e)
self.task.cancel() self.task.cancel()
self.retry_counter = 0
self.task = self.get_task() self.task = self.get_task()
async def get_chat_command(self, username: str, message: str, color: discord.Color) -> str: 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": if service_name == "pterodactyl":
logger.info("Configuration value set: api_key\nRestarting task...") logger.info("Configuration value set: api_key\nRestarting task...")
self.task.cancel() self.task.cancel()
self.retry_counter = 0
self.task = self.get_task() self.task = self.get_task()
@commands.group(autohelp = True, name = "pterodactyl", aliases = ["ptero"]) @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}") await ctx.send(f"Base URL set to {base_url}")
logger.info("Configuration value set: base_url = %s\nRestarting task...", base_url) logger.info("Configuration value set: base_url = %s\nRestarting task...", base_url)
self.task.cancel() self.task.cancel()
self.retry_counter = 0
self.task = self.get_task() self.task = self.get_task()
@pterodactyl_config.command(name = "serverid") @pterodactyl_config.command(name = "serverid")
@ -114,6 +126,7 @@ class Pterodactyl(commands.Cog):
await ctx.send(f"Server ID set to {server_id}") await ctx.send(f"Server ID set to {server_id}")
logger.info("Configuration value set: server_id = %s\nRestarting task...", server_id) logger.info("Configuration value set: server_id = %s\nRestarting task...", server_id)
self.task.cancel() self.task.cancel()
self.retry_counter = 0
self.task = self.get_task() self.task = self.get_task()
@pterodactyl_config.command(name = "consolechannel") @pterodactyl_config.command(name = "consolechannel")