【问题标题】:How to log exception using autoretry in Celery tasks如何在 Celery 任务中使用自动重试记录异常
【发布时间】:2020-08-17 15:39:12
【问题描述】:

来自 celery 文档,

If you want to automatically retry on any error, simply use:

@app.task(autoretry_for=(Exception,))
def x():
    ...

我们如何记录它重试的异常? 如果我们有一个try-except,那么添加日志很容易,例如

@app.task(bind=True, default_retry_delay=30 * 60)  # retry in 30 minutes.
def add(self, x, y):
    try:
        something_raising()
    except Exception as exc:
        logger.info('Retry for exception %s', exc)
        # overrides the default delay to retry after 1 minute
        raise self.retry(exc=exc, countdown=60)

但我想使用自动重试,这样我就可以利用自动重试附带的 celery 重试退避,而不是使用 try-except 实现我自己的倒计时。请帮忙。

【问题讨论】:

    标签: python python-3.x celery django-celery celery-task


    【解决方案1】:

    在声明任务时添加 throws 参数,这将在屏幕上弹出异常,并且可以将其更改为您想要抛出的任何预期异常。比如RequestException、TimeoutException

    @app.task(bind=True, throws=(Exception,), default_retry_delay=30 * 60)  # retry in 30 minutes.
    def add(self, x, y):
        ...
    

    【讨论】:

      猜你喜欢
      • 2014-05-22
      • 1970-01-01
      • 2011-06-19
      • 2019-01-11
      • 1970-01-01
      • 2015-01-17
      • 1970-01-01
      • 1970-01-01
      • 2015-12-25
      相关资源
      最近更新 更多