How to implement autoretry for Celery tasks

后端 未结 4 2105
醉酒成梦
醉酒成梦 2021-02-13 13:06

In Celery, you can retry any task in case of exception. You can do it like so:

@task(max_retries=5)
def div(a, b):
    try:
        return a / b
            


        
4条回答
  •  猫巷女王i
    2021-02-13 14:07

    I've modified your answer to work with the existing Celery API (currently 3.1.17)

    class MyCelery(Celery):
        def task(self, *args_task, **opts_task):
            def real_decorator(func):
                sup = super(MyCelery, self).task
    
                @sup(*args_task, **opts_task)
                @functools.wraps(func)
                def wrapper(*args, **kwargs):
                    try:
                        func(*args, **kwargs)
                    except opts_task.get('autoretry_on', Exception) as exc:
                        logger.info('Yo! We did it!')
                        wrapper.retry(exc=exc, args=args, kwargs=kwargs)
                return wrapper
            return real_decorator
    

    Then, in your tasks

    app = MyCelery()
    app.config_from_object('django.conf:settings')
    app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
    
    @app.task(autoretry_on=Exception)
    def mytask():
        raise Exception('Retrying!')
    

    This allows you to add the autoretry_on functionality to your tasks without having to use a separate decorator to define tasks.

提交回复
热议问题