Skip to content

Commit

Permalink
improve exit logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dmunozv04 committed Dec 15, 2024
1 parent d9ab2cd commit dcf53dc
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/iSponsorBlockTV/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ async def skip(self, time_to, position, uuids):
await asyncio.create_task(self.api_helper.mark_viewed_segments(uuids))
await asyncio.create_task(self.lounge_controller.seek_to(position))

# Stops the connection to the device
async def cancel(self):
self.cancelled = True
try:
Expand All @@ -140,10 +139,14 @@ async def cancel(self):
pass


async def finish(devices):
for i in devices:
await i.cancel()
async def finish(devices, web_session, tcp_connector):
for device in devices:
await device.cancel()
await web_session.close()
await tcp_connector.close()

def handle_signal(signum, frame):
raise KeyboardInterrupt()

def main(config, debug):
loop = asyncio.get_event_loop_policy().get_event_loop()
Expand All @@ -160,11 +163,15 @@ def main(config, debug):
devices.append(device)
tasks.append(loop.create_task(device.loop()))
tasks.append(loop.create_task(device.refresh_auth_loop()))
signal(SIGINT, lambda s, f: loop.stop())
signal(SIGTERM, lambda s, f: loop.stop())
loop.run_forever()
print("Cancelling tasks and exiting...")
loop.run_until_complete(finish(devices))
loop.run_until_complete(web_session.close())
loop.run_until_complete(tcp_connector.close())
loop.close()
signal(SIGTERM, handle_signal)
signal(SIGINT, handle_signal)
try:
loop.run_forever()
except KeyboardInterrupt:
print("Cancelling tasks and exiting...")
for task in tasks:
task.cancel()
loop.run_until_complete(asyncio.gather(*tasks, return_exceptions=True))
loop.run_until_complete(finish(devices, web_session, tcp_connector))
finally:
loop.close()

0 comments on commit dcf53dc

Please sign in to comment.