Small closing sequence cleanup + useful comments

This commit is contained in:
DevilXD committed 2022-01-16 23:16:33 +01:00
1 parent 3a05e5e104
commit 2fdbf809e7
1 file changed
+10 -5
+10 -5
View File
@@ -161,6 +161,10 @@ class Twitch:
loop.close() loop.close()
def stop(self): def stop(self):
self.stop_watching()
if self._watching_task is not None:
self._watching_task.cancel()
self._watching_task = None
if self._main_task is not None: if self._main_task is not None:
self._main_task.cancel() self._main_task.cancel()
self._main_task = None self._main_task = None
@@ -168,13 +172,12 @@ class Twitch:
async def close(self): async def close(self):
start_time = time() start_time = time()
self.gui.print("Exiting...") self.gui.print("Exiting...")
self.stop_watching() self.stop()
if self._watching_task is not None: # save our cookies
self._watching_task.cancel()
self._watching_task = None
self._session.cookie_jar.save(COOKIES_PATH) # type: ignore self._session.cookie_jar.save(COOKIES_PATH) # type: ignore
await self._session.close() # stop websocket and close session
await self.websocket.stop() await self.websocket.stop()
await self._session.close()
# wait at least one full second + whatever it takes to complete the closing # wait at least one full second + whatever it takes to complete the closing
# this allows aiohttp to safely close the session # this allows aiohttp to safely close the session
await asyncio.sleep(start_time + 1 - time()) await asyncio.sleep(start_time + 1 - time())
@@ -372,6 +375,8 @@ class Twitch:
# cleanup channels every hour # cleanup channels every hour
self.change_state(State.CHANNELS_CLEANUP) self.change_state(State.CHANNELS_CLEANUP)
i = (i + 1) % 3600 i = (i + 1) % 3600
# we use wait_for here to allow an asyncio.sleep that can be ended prematurely,
# without cancelling the containing task
self._watching_restart.clear() self._watching_restart.clear()
try: try:
await asyncio.wait_for( await asyncio.wait_for(