feat: SIGINT and SIGTERM shutdown handlers as part of dockerization

This commit is contained in:
17ms 2024-07-07 17:16:14 +03:00
parent 13f453768c
commit 2c99bcbb0a
Signed by untrusted user who does not match committer: ae
GPG Key ID: 995EFD5C1B532B3E

23
main.py
View File

@ -4,6 +4,7 @@ import os
import asyncio import asyncio
import threading import threading
import logging import logging
import signal
import aioprocessing import aioprocessing
from dotenv import load_dotenv from dotenv import load_dotenv
@ -89,16 +90,28 @@ def main():
ws_thread.start() ws_thread.start()
qp_thread.start() qp_thread.start()
try: def handle_exit():
ws_thread.join() logging.info("Shutdown procedure initialized")
qp_thread.join()
except KeyboardInterrupt:
logging.info("Keyboard interrupt received, shutting down threads.")
shutdown_event.set() shutdown_event.set()
shutdown_loop.run_until_complete(shutdown(shutdown_loop)) shutdown_loop.run_until_complete(shutdown(shutdown_loop))
ws_thread.join() ws_thread.join()
qp_thread.join() qp_thread.join()
export_thread.join() export_thread.join()
def handle_signal(signal, _frame):
logging.info("Received signal '%s', shutting down...", signal)
handle_exit()
# SIGINT and SIGTERM signal handler (mainly for Docker)
signal.signal(signal.SIGINT, handle_signal)
signal.signal(signal.SIGTERM, handle_signal)
try:
ws_thread.join()
qp_thread.join()
except KeyboardInterrupt:
logging.info("Keyboard interrupt received, shutting down threads.")
handle_exit()
finally: finally:
export_loop.stop() export_loop.stop()
export_loop.close() export_loop.close()