Skip to content

Commit

Permalink
feat(pool): support re-adding nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ooliver1 committed Jun 2, 2023
1 parent 5c93a2d commit d2b35da
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
9 changes: 9 additions & 0 deletions mafic/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,16 @@ async def close(self) -> None:
_log.debug("Connection task cancelled.", extra={"label": self._label})

_log.info("Node %s is now closed.", self._label, extra={"label": self._label})
self.cleanup()

def cleanup(self) -> None:
"""Cleanup the node, as if it was never connected."""
self._available = False
self._ws = None
self._ws_task = None
self._connect_task = None
self.__session = None
self._ready.clear()

async def _ws_listener(self) -> None:
"""Listen for messages from the websocket."""
Expand Down
24 changes: 21 additions & 3 deletions mafic/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,25 @@ async def create_node(
resuming_session_id=resuming_session_id,
)

await self.add_node(node)
return node

async def add_node(self, node: Node[ClientT]) -> None:
"""Add an existing node to this pool.
.. note::
You generally do not want this, use :meth:`create_node` instead.
This is used for after running :meth:`remove_node` to re-add the node
if it has been restarted.
.. versionadded:: 2.7
Parameters
----------
node:
The node to add.
"""
# Add to dictionaries, creating a set or extending it if needed.
if node.regions:
for region in node.regions:
Expand All @@ -202,11 +221,10 @@ async def create_node(
*self._node_shards.get(shard_id, set()),
}

_log.info("Created node, connecting it...", extra={"label": label})
_log.info("Created node, connecting it...", extra={"label": node.label})
await node.connect()

self._nodes[label] = node
return node
self._nodes[node.label] = node

async def remove_node(
self, node: Node[ClientT] | str, *, transfer_players: bool = True
Expand Down

0 comments on commit d2b35da

Please sign in to comment.