Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save peterfarrell/702de44a4cc445271308e62a4108a6ac to your computer and use it in GitHub Desktop.
Save peterfarrell/702de44a4cc445271308e62a4108a6ac to your computer and use it in GitHub Desktop.
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