Skip to content

Commit 4d10194

Browse files
victorauvipy
authored andcommitted
Added handle of SIGTERM in BaseTask in celery/task.py to prevent kill the task
1 parent 9d49d90 commit 4d10194

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

celery/app/task.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""Task implementation: request context and the task base class."""
33
from __future__ import absolute_import, unicode_literals
44

5+
import signal
56
import sys
67

78
from billiard.einfo import ExceptionInfo
@@ -20,6 +21,7 @@
2021
from celery.utils import abstract
2122
from celery.utils.functional import mattrgetter, maybe_list
2223
from celery.utils.imports import instantiate
24+
from celery.utils.log import get_logger
2325
from celery.utils.nodenames import gethostname
2426
from celery.utils.serialization import raise_with_context
2527

@@ -386,6 +388,10 @@ def add_around(cls, attr, around):
386388
setattr(cls, attr, meth)
387389

388390
def __call__(self, *args, **kwargs):
391+
logger = get_logger(__name__)
392+
handle_sigterm = lambda signum, frame: \
393+
logger.info('SIGTERM received, waiting till the task finished')
394+
signal.signal(signal.SIGTERM, handle_sigterm)
389395
_task_stack.push(self)
390396
self.push_request(args=args, kwargs=kwargs)
391397
try:

0 commit comments

Comments
 (0)