From e4e925ec64bfb6c32f5199048635499656ab1d2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Voron?= Date: Wed, 16 Oct 2024 10:45:24 +0200 Subject: [PATCH] server/worker: handle term/kill signal in run_worker script --- server/run_worker.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/server/run_worker.py b/server/run_worker.py index 3e64f5a397..6d67755a1d 100644 --- a/server/run_worker.py +++ b/server/run_worker.py @@ -1,7 +1,9 @@ import argparse import multiprocessing import os +import signal from logging import Logger +from types import FrameType import structlog from arq import run_worker as arq_run_worker @@ -63,13 +65,19 @@ def _main(default_worker_num: int = 1, github_worker_num: int = 1) -> None: processes.append(github_worker_process) logger.debug(f"Triggered {github_worker_num} GitHub worker processes") + def handle_signal(signum: int, frame: FrameType | None) -> None: + logger.info(f"Received signal {signum}, shutting down worker processes") + for process in processes: + process.terminate() + + signal.signal(signal.SIGTERM, handle_signal) + signal.signal(signal.SIGINT, handle_signal) + try: for process in processes: process.join() except KeyboardInterrupt: - logger.info("Shutting down worker processes") - for process in processes: - process.terminate() + handle_signal(signal.SIGINT, None) if __name__ == "__main__":