mirror of
https://github.com/matrix-org/pantalaimon.git
synced 2024-10-01 03:35:38 -04:00
client: Fix the gracefull shutdown of a client.
This commit is contained in:
parent
5001f2fc12
commit
5151095740
@ -25,7 +25,7 @@ class PantaClient(AsyncClient):
|
||||
super().__init__(homeserver, user, device_id, store_path, config,
|
||||
ssl, proxy)
|
||||
|
||||
self.loop_running = False
|
||||
self.task = None
|
||||
self.loop_stopped = asyncio.Event()
|
||||
self.synced = asyncio.Event()
|
||||
|
||||
@ -40,17 +40,24 @@ class PantaClient(AsyncClient):
|
||||
"user {}".format(device.id, user_id))
|
||||
self.verify_device(device)
|
||||
|
||||
async def loop(self):
|
||||
def start_loop(self):
|
||||
"""Start a loop that runs forever and keeps on syncing with the server.
|
||||
|
||||
The loop can be stopped with the stop_loop() method.
|
||||
"""
|
||||
loop = asyncio.get_event_loop()
|
||||
task = loop.create_task(self.loop())
|
||||
self.task = task
|
||||
return task
|
||||
|
||||
async def loop(self):
|
||||
self.loop_running = True
|
||||
self.loop_stopped.clear()
|
||||
|
||||
logger.info(f"Starting sync loop for {self.user_id}")
|
||||
|
||||
while self.loop_running:
|
||||
try:
|
||||
while True:
|
||||
if not self.logged_in:
|
||||
# TODO login
|
||||
pass
|
||||
@ -73,18 +80,17 @@ class PantaClient(AsyncClient):
|
||||
self.synced.set()
|
||||
self.synced.clear()
|
||||
|
||||
except asyncio.CancelledError:
|
||||
logger.info("Stopping the sync loop")
|
||||
self.loop_running = False
|
||||
self.loop_stopped.set()
|
||||
|
||||
async def loop_stop(self):
|
||||
"""Stop the client loop.
|
||||
"""Stop the client loop."""
|
||||
if not self.task:
|
||||
return
|
||||
|
||||
Raises LocalProtocolError if the loop isn't running.
|
||||
"""
|
||||
if not self.loop_running:
|
||||
raise LocalProtocolError("Loop is not running")
|
||||
|
||||
self.loop_running = False
|
||||
self.task.cancel()
|
||||
await self.loop_stopped.wait()
|
||||
|
||||
async def encrypt(self, room_id, msgtype, content):
|
||||
|
@ -152,8 +152,7 @@ class ProxyDaemon:
|
||||
|
||||
self.panta_clients[user_id] = panta_client
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.create_task(panta_client.loop())
|
||||
panta_client.start_loop()
|
||||
|
||||
async def login(self, request):
|
||||
try:
|
||||
|
Loading…
Reference in New Issue
Block a user