fix(pterodactyl): asyncio sleep instead of blocking
This commit is contained in:
parent
a0b1773185
commit
8b50c0376d
1 changed files with 10 additions and 11 deletions
|
@ -1,6 +1,5 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
import time
|
|
||||||
from typing import Mapping, Optional
|
from typing import Mapping, Optional
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
|
@ -27,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 = self.get_task()
|
self.task = await 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
|
||||||
|
|
||||||
def get_task(self) -> asyncio.Task:
|
async 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(await self.error_callback)
|
||||||
return task
|
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:
|
try:
|
||||||
fut.result()
|
fut.result()
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
|
@ -51,7 +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)
|
||||||
time.sleep(5 * self.retry_counter)
|
await asyncio.sleep(5 * self.retry_counter)
|
||||||
self.task = self.get_task()
|
self.task = self.get_task()
|
||||||
else:
|
else:
|
||||||
logger.info("Retry limit reached. Stopping task.")
|
logger.info("Retry limit reached. Stopping task.")
|
||||||
|
@ -67,7 +66,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 = self.get_task()
|
self.task = await 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())
|
||||||
|
@ -81,7 +80,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 = self.get_task()
|
self.task = await 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()
|
||||||
|
@ -94,7 +93,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 = self.get_task()
|
self.task = await 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:
|
||||||
|
@ -119,7 +118,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 = self.get_task()
|
self.task = await 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:
|
||||||
|
@ -129,7 +128,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 = self.get_task()
|
self.task = await 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