From 8b50c0376dd93c6f9e4b706540f010439d24f599 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Fri, 1 Mar 2024 14:59:20 -0500 Subject: [PATCH] fix(pterodactyl): asyncio sleep instead of blocking --- pterodactyl/pterodactyl.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/pterodactyl/pterodactyl.py b/pterodactyl/pterodactyl.py index 7a4399f..8b35634 100644 --- a/pterodactyl/pterodactyl.py +++ b/pterodactyl/pterodactyl.py @@ -1,6 +1,5 @@ import asyncio import json -import time from typing import Mapping, Optional import discord @@ -27,20 +26,20 @@ class Pterodactyl(commands.Cog): async def cog_load(self) -> None: self.retry_counter = 0 - self.task = self.get_task() + self.task = await 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: + async def get_task(self) -> asyncio.Task: from pterodactyl.websocket import establish_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(await self.error_callback) return task - def error_callback(self, fut) -> None: #NOTE - Thanks flame442 and zephyrkul for helping me figure this out + async def error_callback(self, fut) -> None: #NOTE - Thanks flame442 and zephyrkul for helping me figure this out try: fut.result() except asyncio.CancelledError: @@ -51,7 +50,7 @@ class Pterodactyl(commands.Cog): if self.retry_counter < 5: self.retry_counter += 1 logger.info("Retrying in %s seconds...", 5 * self.retry_counter) - time.sleep(5 * self.retry_counter) + await asyncio.sleep(5 * self.retry_counter) self.task = self.get_task() else: logger.info("Retry limit reached. Stopping task.") @@ -67,7 +66,7 @@ class Pterodactyl(commands.Cog): logger.error("WebSocket connection closed: %s", e) self.task.cancel() self.retry_counter = 0 - self.task = self.get_task() + self.task = await 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) channel = self.bot.get_channel(await config.console_channel()) @@ -81,7 +80,7 @@ class Pterodactyl(commands.Cog): logger.error("WebSocket connection closed: %s", e) self.task.cancel() self.retry_counter = 0 - self.task = self.get_task() + self.task = await self.get_task() async def get_chat_command(self, username: str, message: str, color: discord.Color) -> str: command: str = await config.chat_command() @@ -94,7 +93,7 @@ class Pterodactyl(commands.Cog): logger.info("Configuration value set: api_key\nRestarting task...") self.task.cancel() self.retry_counter = 0 - self.task = self.get_task() + self.task = await self.get_task() @commands.group(autohelp = True, name = "pterodactyl", aliases = ["ptero"]) async def pterodactyl(self, ctx: commands.Context) -> None: @@ -119,7 +118,7 @@ class Pterodactyl(commands.Cog): logger.info("Configuration value set: base_url = %s\nRestarting task...", base_url) self.task.cancel() self.retry_counter = 0 - self.task = self.get_task() + self.task = await self.get_task() @pterodactyl_config.command(name = "serverid") async def pterodactyl_config_server_id(self, ctx: commands.Context, *, server_id: str) -> None: @@ -129,7 +128,7 @@ class Pterodactyl(commands.Cog): logger.info("Configuration value set: server_id = %s\nRestarting task...", server_id) self.task.cancel() self.retry_counter = 0 - self.task = self.get_task() + self.task = await self.get_task() @pterodactyl_config.command(name = "consolechannel") async def pterodactyl_config_console_channel(self, ctx: commands.Context, channel: discord.TextChannel) -> None: