Refactor exit-offline code
This commit is contained in:
parent
f316a24550
commit
4784c80ed9
|
@ -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."""
|
||||
|
||||
|
|
|
@ -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."""
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user