diff --git a/src/backend/backend.py b/src/backend/backend.py index c07a680e..3d903340 100644 --- a/src/backend/backend.py +++ b/src/backend/backend.py @@ -198,16 +198,6 @@ class Backend: )) - async def logoff_all(self) -> None: - """Stop syncing and end all clients registered.""" - - await asyncio.gather(*[ - client.set_presence("offline", save=False) - for client in self.clients.values() - if client._presence != "offline" - ]) - - async def logout_client(self, user_id: str) -> None: """Log a `MatrixClient` out and unregister it from our models.""" @@ -231,6 +221,14 @@ class Backend: await self.saved_accounts.delete(user_id) + async def terminate_clients(self) -> None: + """Call every `MatrixClient`'s `terminate()` method.""" + + log.info("Setting clients offline...") + tasks = [client.terminate() for client in self.clients.values()] + await asyncio.gather(*tasks) + + async def get_client(self, user_id: str) -> MatrixClient: """Wait until a `MatrixClient` is registered in model and return it.""" diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index 45965e7d..f1b80701 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -305,6 +305,17 @@ class MatrixClient(nio.AsyncClient): await self.close() + async def terminate(self) -> None: + """Stop tasks, Set our presence offline and close HTTP connections.""" + + await self._stop() + + if self._presence != "offline": + await self.set_presence("offline", save=False) + + await self.close() + log.info("%s termined", self.user_id) + @property def syncing(self) -> bool: """Return whether this client is currently syncing with the server.""" diff --git a/src/backend/qml_bridge.py b/src/backend/qml_bridge.py index 074c54e7..5a92b6ee 100644 --- a/src/backend/qml_bridge.py +++ b/src/backend/qml_bridge.py @@ -146,14 +146,10 @@ class QMLBridge: remote_pdb.RemotePdb("127.0.0.1", 4444).set_trace() - def exit_mirage(self) -> None: - """Used to end some tasks before closing""" - + def exit(self) -> None: try: - # Set accounts presence to offline asyncio.run_coroutine_threadsafe( - self.backend.logoff_all(), - self._loop, + self.backend.terminate_clients(), self._loop, ).result() except Exception as e: print(e) @@ -179,4 +175,4 @@ except ValueError: BRIDGE = QMLBridge() -pyotherside.atexit(BRIDGE.exit_mirage) +pyotherside.atexit(BRIDGE.exit)