0

I am new to Celery (5.1.xxx) and met someone else's code using celery task doing something in database (db) and retrying when it fails.

@app.task(bind=True, max_retries=3)
def build_my_task(self, xxx)
   try:
      state = do_something_in_db()
   except Exception as err:
      raise self.retry(exc=err, countdown=RETRY_COUNTDOWN_SEC)

Now, to my understanding, timeouts in the task are related to task itself only and retry will only send signal to celery and it will start another task with restarted timeout (am I right?) , but I need to create "greater" timeout, which will fail including time of task retries (defined in task' decorator line) with maybe additional condition - say if db returned some specific state... Ho should I realize that?

Can't say I tried something, more thinking of, is Celery has some facilities for that problem and if I can get somehow time that my task will take in general (including its retries) and if I can get somehow current counter of retry attempt from inside of the task

jabba
  • 21
  • 2
  • To explain some of them: countdown (The time between the current and new task run), autoretry_for (For known exceptions -> No need for a try except block), max_retries (How often is it allowed to retry?) and you can also raise them manually either via raise self.retry(*args, **kwargs) or via self.retry(*args, **kwargs) that depends on your coding style. Note that all your decorations can be overwritten inside the apply_async/delay and also within the raise self.retry. So you can specify it situation-wise. *Retry create a new task to answer your question – mika Mar 10 '23 at 09:25

0 Answers0