Created
March 28, 2018 14:30
-
-
Save lovemyliwu/af5112de25b594205a76c3bfd00b9340 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CustomTask(Task): | |
def on_failure(self, exc, task_id, args, kwargs, einfo): | |
info = '[{0}] failed: {1}'.format(task_id, exc) | |
logger.exception(info, exc_info=exc) | |
super(CustomTask, self).on_failure(exc, task_id, args, kwargs, einfo) | |
def on_success(self, retval, task_id, args, kwargs): | |
global app | |
if app.is_warm_shutdown: | |
app.consumer.connection._default_channel.do_restore = False | |
# monkey patch Task to log exception | |
app.Task = CustomTask | |
app.config_from_object('conf.celery_settings') | |
app.autodiscover_tasks() | |
app.is_warm_shutdown = False | |
def mark_app_warm_shutdown(*args, **kwargs): | |
global app | |
logger.info('hook warm shutdown') | |
app.is_warm_shutdown = True | |
app.origin_signal_handler(*args, **kwargs) | |
@receiver(worker_ready) | |
def on_celery_worker_ready(sender, **kwargs): | |
global app | |
logger.info('hook work ready') | |
app.consumer = sender | |
app.origin_signal_handler = platforms.signals["TERM"] | |
platforms.signals["TERM"] = mark_app_warm_shutdown |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment