Skip to content

Commit

Permalink
revert task callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Drapersniper committed Mar 18, 2022
1 parent 44b4ce8 commit 5980294
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 52 deletions.
4 changes: 2 additions & 2 deletions lavalink/lavalink.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import discord
from discord.ext.commands import Bot

from . import enums, log, node, player_manager, utils, errors
from . import enums, log, node, player_manager, errors

__all__ = [
"initialize",
Expand Down Expand Up @@ -344,7 +344,7 @@ def dispatch(op: enums.LavalinkIncomingOp, data, raw_data: dict):
return

for coro in listeners:
_loop.create_task(coro(*args)).add_done_callback(utils.task_callback_trace)
_loop.create_task(coro(*args))


async def close(bot):
Expand Down
23 changes: 4 additions & 19 deletions lavalink/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@
)
from .player_manager import PlayerManager
from .rest_api import Track
from .utils import (
task_callback_exception,
task_callback_debug,
task_callback_verbose,
task_callback_trace,
)
from .errors import AbortingNodeConnection, NodeNotReady, NodeNotFound

__all__ = [
Expand Down Expand Up @@ -347,10 +341,7 @@ async def _multi_try_connect(self, uri):
ws_ll_log.debug("Creating Lavalink WS listener.")
if self._is_shutdown is False:
self._listener_task = self.loop.create_task(self.listener())
self._listener_task.add_done_callback(task_callback_exception)
self.loop.create_task(self._configure_resume()).add_done_callback(
task_callback_verbose
)
self.loop.create_task(self._configure_resume())
if self._queue:
temp = self._queue.copy()
self._queue.clear()
Expand All @@ -375,7 +366,6 @@ async def listener(self):
self.reconnect_task = self.loop.create_task(
self._reconnect(self._is_shutdown)
)
self.reconnect_task.add_done_callback(task_callback_debug)
return
else:
ws_ll_log.info("[NODE] | Listener closing: %s", msg.extra)
Expand All @@ -388,9 +378,7 @@ async def listener(self):
ws_ll_log.verbose("[NODE] | Received unknown op: %s", data)
else:
ws_ll_log.trace("[NODE] | Received known op: %s", data)
self.loop.create_task(self._handle_op(op, data)).add_done_callback(
task_callback_trace
)
self.loop.create_task(self._handle_op(op, data))
elif msg.type == aiohttp.WSMsgType.ERROR:
exc = self._ws.exception()
ws_ll_log.warning(
Expand All @@ -412,7 +400,6 @@ async def listener(self):
self.reconnect_task.cancel()
self.update_state(NodeState.RECONNECTING)
self.reconnect_task = self.loop.create_task(self._reconnect(self._is_shutdown))
self.reconnect_task.add_done_callback(task_callback_debug)

async def _handle_op(self, op: LavalinkIncomingOp, data):
if op == LavalinkIncomingOp.EVENT:
Expand Down Expand Up @@ -459,7 +446,7 @@ async def _reconnect(self, shutdown: bool = False):
attempt += 1
if attempt > 10:
ws_ll_log.info("[NODE] | Failed reconnection attempt too many times, aborting ...")
asyncio.create_task(self.disconnect()).add_done_callback(task_callback_debug)
asyncio.create_task(self.disconnect())
return
try:
await self.connect(shutdown=shutdown)
Expand Down Expand Up @@ -503,9 +490,7 @@ def update_state(self, next_state: NodeState):
ws_ll_log.debug("Event loop closed, not notifying state handlers.")
return
for handler in self._state_handlers:
self.loop.create_task(handler(next_state, old_state)).add_done_callback(
task_callback_trace
)
self.loop.create_task(handler(next_state, old_state))

def register_state_handler(self, func):
if not asyncio.iscoroutinefunction(func):
Expand Down
32 changes: 1 addition & 31 deletions lavalink/utils.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,6 @@
import asyncio
import contextlib

from .log import log


def format_time(time):
"""Formats the given time into HH:MM:SS"""
h, r = divmod(time / 1000, 3600)
m, s = divmod(r, 60)

return "%02d:%02d:%02d" % (h, m, s)


def task_callback_exception(task: asyncio.Task) -> None:
with contextlib.suppress(asyncio.CancelledError, asyncio.InvalidStateError):
if exc := task.exception():
log.exception("%s raised an Exception", task.get_name(), exc_info=exc)


def task_callback_debug(task: asyncio.Task) -> None:
with contextlib.suppress(asyncio.CancelledError, asyncio.InvalidStateError):
if exc := task.exception():
log.debug("%s raised an Exception", task.get_name(), exc_info=exc)


def task_callback_verbose(task: asyncio.Task) -> None:
with contextlib.suppress(asyncio.CancelledError, asyncio.InvalidStateError):
if exc := task.exception():
log.verbose("%s raised an Exception", task.get_name(), exc_info=exc)


def task_callback_trace(task: asyncio.Task) -> None:
with contextlib.suppress(asyncio.CancelledError, asyncio.InvalidStateError):
if exc := task.exception():
log.trace("%s raised an Exception", task.get_name(), exc_info=exc)
return f"{h:02d}:{m:02d}:{s:02d}"

0 comments on commit 5980294

Please sign in to comment.