Compare commits

...

2 commits

Author SHA1 Message Date
f2d54ce23b
feat(pterodactyl): added a retry counter and sleep cooldown to the error_callback
All checks were successful
Actions / Lint Code (Ruff & Pylint) (pull_request) Successful in 21s
Actions / Build Documentation (MkDocs) (pull_request) Successful in 25s
2024-03-01 14:42:45 -05:00
b7f5ae644a
fix(pterodactyl): check if websocket_credentials is None 2024-03-01 14:18:44 -05:00
2 changed files with 16 additions and 2 deletions

View file

@ -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")

View file

@ -4,7 +4,6 @@ import re
from logging import getLogger
from typing import Optional, Union
import aiohttp
import discord
import websockets
@ -22,6 +21,8 @@ async def establish_websocket_connection(coginstance: Pterodactyl) -> None:
logger.info("Establishing WebSocket connection")
websocket_credentials = await retrieve_websocket_credentials(coginstance)
if not websocket_credentials:
return logger.error("Failed to retrieve WebSocket credentials. WebSocket connection not established")
async with websockets.connect(websocket_credentials['data']['socket'], origin=base_url, ping_timeout=60, logger=getLogger("red.sea.pterodactyl.websocket")) as websocket:
logger.info("WebSocket connection established")