diff --git a/docs/api/events.rst b/docs/api/events.rst index 15323b4..9728439 100644 --- a/docs/api/events.rst +++ b/docs/api/events.rst @@ -72,3 +72,17 @@ Callbacks :param node: The node that sent the statistics. :type node: :class:`Node` + +.. function:: on_node_ready() + + Called when Lavalink node is ready. + + :param node: The node that was ready. + :type node: :class:`Node` + +.. function:: on_node_unavailable() + + Called when Lavalink node becomes unavailable. + + :param node: The node that became unavailable. + :type node: :class:`Node` diff --git a/mafic/node.py b/mafic/node.py index bf1c9e6..9e6a81d 100644 --- a/mafic/node.py +++ b/mafic/node.py @@ -759,7 +759,9 @@ async def _ws_listener(self) -> None: if _type is aiohttp.WSMsgType.CLOSED: self._available = False + self._client.dispatch("node_unavailable", self) close_code = self._ws.close_code + self._ready.clear() self._ws = None wait_time = backoff.delay() diff --git a/mafic/pool.py b/mafic/pool.py index 69af457..8ecfebf 100644 --- a/mafic/pool.py +++ b/mafic/pool.py @@ -91,8 +91,8 @@ def label_to_node(cls) -> dict[str, Node[ClientT]]: @classproperty def nodes(cls) -> list[Node[ClientT]]: - """Get the list of all nodes.""" - return list(cls._nodes.values()) + """Get the list of all available nodes.""" + return list(filter(lambda n: n.available, cls._nodes.values())) async def create_node( self, @@ -366,7 +366,10 @@ def get_random_node(cls) -> Node[ClientT]: ValueError If there are no nodes. """ - if node := choice(list(cls._nodes.values())): + # It is a classproperty. + nodes = cast(list[Node[ClientT]], cls.nodes) # pyright: ignore # noqa: PGH003 + + if node := choice(nodes): return node raise NoNodesAvailable