Enhanced server error handling

Add signal handling for when the process is killed
This commit is contained in:
kale4eat
2024-03-06 12:13:27 +09:00
parent 45c6bde2e7
commit ed90af1b87
2 changed files with 16 additions and 0 deletions

View File

@@ -54,6 +54,7 @@ def initialize(port: int = WOKER_PORT):
import socket
import sys
import atexit
import signal
logger.debug("initialize")
global WORKER_CLIENT
@@ -100,6 +101,15 @@ def initialize(port: int = WOKER_PORT):
WORKER_CLIENT = client
atexit.register(terminate)
# when the process is killed
def signal_handler(signum, frame):
with open("signal_handler.txt", mode="w") as f:
pass
terminate()
signal.signal(signal.SIGTERM, signal_handler)
# top-level declaration
def terminate():

View File

@@ -86,6 +86,12 @@ class WorkerServer:
self.client_count -= 1
logger.info("close connection")
continue
except Exception as e:
sock.close()
sockets.remove(sock)
self.client_count -= 1
logger.error(e)
continue
logger.trace(f"server received request: {request}")