forked from cswimr/SeaCogs
fix(pterodactyl): fixed retry functionality - no longer blocks the bot
This commit is contained in:
parent
a166168507
commit
1bc1c7a90e
1 changed files with 9 additions and 10 deletions
|
@ -26,20 +26,20 @@ class Pterodactyl(commands.Cog):
|
||||||
|
|
||||||
async def cog_load(self) -> None:
|
async def cog_load(self) -> None:
|
||||||
self.retry_counter = 0
|
self.retry_counter = 0
|
||||||
self.task = await 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
|
self.retry_counter = 0
|
||||||
await self.client._session.close() # pylint: disable=protected-access
|
await self.client._session.close() # pylint: disable=protected-access
|
||||||
|
|
||||||
async def get_task(self) -> asyncio.Task:
|
def get_task(self) -> asyncio.Task:
|
||||||
from pterodactyl.websocket import establish_websocket_connection
|
from pterodactyl.websocket import establish_websocket_connection
|
||||||
task = self.bot.loop.create_task(establish_websocket_connection(self), name="Pterodactyl Websocket Connection")
|
task = self.bot.loop.create_task(establish_websocket_connection(self), name="Pterodactyl Websocket Connection")
|
||||||
task.add_done_callback(self.error_callback)
|
task.add_done_callback(self.error_callback)
|
||||||
return task
|
return task
|
||||||
|
|
||||||
async def error_callback(self, fut) -> None: #NOTE - Thanks flame442 and zephyrkul for helping me figure this out
|
def error_callback(self, fut) -> None: #NOTE - Thanks flame442 and zephyrkul for helping me figure this out
|
||||||
try:
|
try:
|
||||||
fut.result()
|
fut.result()
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
|
@ -50,8 +50,7 @@ class Pterodactyl(commands.Cog):
|
||||||
if self.retry_counter < 5:
|
if self.retry_counter < 5:
|
||||||
self.retry_counter += 1
|
self.retry_counter += 1
|
||||||
logger.info("Retrying in %s seconds...", 5 * self.retry_counter)
|
logger.info("Retrying in %s seconds...", 5 * self.retry_counter)
|
||||||
await asyncio.sleep(5 * self.retry_counter)
|
self.task = self.bot.loop.call_later(5 * self.retry_counter, self.get_task)
|
||||||
self.task = self.get_task()
|
|
||||||
else:
|
else:
|
||||||
logger.info("Retry limit reached. Stopping task.")
|
logger.info("Retry limit reached. Stopping task.")
|
||||||
|
|
||||||
|
@ -66,7 +65,7 @@ class Pterodactyl(commands.Cog):
|
||||||
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.retry_counter = 0
|
||||||
self.task = await 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)
|
||||||
channel = self.bot.get_channel(await config.console_channel())
|
channel = self.bot.get_channel(await config.console_channel())
|
||||||
|
@ -80,7 +79,7 @@ class Pterodactyl(commands.Cog):
|
||||||
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.retry_counter = 0
|
||||||
self.task = await 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:
|
||||||
command: str = await config.chat_command()
|
command: str = await config.chat_command()
|
||||||
|
@ -93,7 +92,7 @@ class Pterodactyl(commands.Cog):
|
||||||
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.retry_counter = 0
|
||||||
self.task = await self.get_task()
|
self.task = self.get_task()
|
||||||
|
|
||||||
@commands.group(autohelp = True, name = "pterodactyl", aliases = ["ptero"])
|
@commands.group(autohelp = True, name = "pterodactyl", aliases = ["ptero"])
|
||||||
async def pterodactyl(self, ctx: commands.Context) -> None:
|
async def pterodactyl(self, ctx: commands.Context) -> None:
|
||||||
|
@ -118,7 +117,7 @@ class Pterodactyl(commands.Cog):
|
||||||
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.retry_counter = 0
|
||||||
self.task = await self.get_task()
|
self.task = self.get_task()
|
||||||
|
|
||||||
@pterodactyl_config.command(name = "serverid")
|
@pterodactyl_config.command(name = "serverid")
|
||||||
async def pterodactyl_config_server_id(self, ctx: commands.Context, *, server_id: str) -> None:
|
async def pterodactyl_config_server_id(self, ctx: commands.Context, *, server_id: str) -> None:
|
||||||
|
@ -128,7 +127,7 @@ class Pterodactyl(commands.Cog):
|
||||||
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.retry_counter = 0
|
||||||
self.task = await self.get_task()
|
self.task = self.get_task()
|
||||||
|
|
||||||
@pterodactyl_config.command(name = "consolechannel")
|
@pterodactyl_config.command(name = "consolechannel")
|
||||||
async def pterodactyl_config_console_channel(self, ctx: commands.Context, channel: discord.TextChannel) -> None:
|
async def pterodactyl_config_console_channel(self, ctx: commands.Context, channel: discord.TextChannel) -> None:
|
||||||
|
|
Loading…
Reference in a new issue