From 303d312effdc072b0be3b1a3839d0cc5849502b3 Mon Sep 17 00:00:00 2001 From: Nikita Karetnikov Date: Sun, 17 Dec 2023 05:27:19 +0100 Subject: [PATCH] Terminate worker tasks on SIGINT Fixes #707. --- .../conda_store_server/worker/tasks.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/conda-store-server/conda_store_server/worker/tasks.py b/conda-store-server/conda_store_server/worker/tasks.py index e23df23a2..f76d5ce84 100644 --- a/conda-store-server/conda_store_server/worker/tasks.py +++ b/conda-store-server/conda_store_server/worker/tasks.py @@ -1,9 +1,10 @@ import datetime import os import shutil +import sys import yaml -from celery import Task, shared_task +from celery import Task, platforms, shared_task from celery.execute import send_task from celery.signals import worker_ready from conda_store_server import api, environment, schema, utils @@ -43,6 +44,17 @@ def worker(self): self._worker.initialize() + # Installs a signal handler that terminates the process on Ctrl-C + # (SIGINT) + # Note: the following would not be enough to terminate beat and other + # tasks, which is why the code below explicitly calls sys.exit + # > from celery.apps.worker import install_worker_term_hard_handler + # > install_worker_term_hard_handler(self._worker, sig='SIGINT') + def _shutdown(*args, **kwargs): + return sys.exit(1) + + platforms.signals["INT"] = _shutdown + return self._worker