Home > OS >  Celery task ran with apply() does not respect countdown
Celery task ran with apply() does not respect countdown

Time:12-13

Is it possible to execute celery task locally with apply() so that it respects countdown passed to retry_kwargs?

I have the following task:

@app.task(
    autoretry_for=(Exception,),
    retry_kwargs={"max_retries": 5, "countdown": 60},
)
def my_task():
    ...

When I run it as follows:

my_task.apply()

I can see it is retried as expected but immediately (not with 60 seconds pauses).

CodePudding user response:

It is not possible to execute a celery task locally with apply() so that it respects the countdown argument passed to retry_kwargs. This is because the apply() method runs the task in the current process and does not use the celery worker to execute the task.

The countdown argument in retry_kwargs specifies the time in seconds to wait before retrying the task, but this is only effective when the task is executed by the celery worker. When a task is run with apply(), the retries are executed immediately in the same process, so the countdown argument is not respected.

To ensure that the countdown argument is respected, you should run the task using the celery worker, either by calling it directly or by using the delay() method. This will cause the celery worker to execute the task, and the retries will be performed with the specified countdown interval.

Here is an example of how you could run the task with the celery worker:

my_task.delay()

Alternatively, you can use the apply_async() method, which allows you to specify the countdown argument directly. This will cause the celery worker to execute the task with the specified countdown interval.

Here is an example of how you could run the task with apply_async():

my_task.apply_async(countdown=60)
  • Related