feat: SIGINT and SIGTERM shutdown handlers as part of dockerization
This commit is contained in:
parent
13f453768c
commit
2c99bcbb0a
23
main.py
23
main.py
@ -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()
|
||||||
|
Loading…
Reference in New Issue
Block a user