6

I've launched a lot of tasks, but some of then hasn't finished (763 tasks), are in a PENDING state, but the system isn't processing anything... It's possible to retry this tasks giving celery the task_id?

Suresh Pattu
  • 6,083
  • 16
  • 59
  • 91
Antonio Beamud
  • 2,281
  • 1
  • 15
  • 26

2 Answers2

11

You can't. You can retry a task only from inside itself, you can't do it from outside.

The best thing to do in this case is to run again the task type with the same args, in this way you will do the same JOB but with a new PID that identify your process/task.

Remember also that the celery PENDING state not means only that the task is waiting for execution, but maybe that is unknown.

http://celeryq.org/docs/userguide/tasks.html#pending

I hope this could help

Mauro Rocco
  • 4,980
  • 1
  • 26
  • 40
  • 1
    Is it still True in 2017? If yes, why? – David Dahan Mar 27 '17 at 13:51
  • Yes I think so. Because by design once a task is complete in whatever status than you need to reschedule a new instance of it with a new id. – Mauro Rocco Apr 30 '17 at 20:13
  • They are working on a feature for flower, the frontend GUI, seems that it won't be long now: [https://github.com/mher/flower/pull/158](https://github.com/mher/flower/pull/158) – Jurrian Nov 20 '17 at 12:41
  • 1
    What about passing to the same task the same arguments and the same uuid? `my_task.apply_async(args, task_id=uuid)` – SS_Rebelious Apr 08 '18 at 22:58
  • @SamuelGóngora, I don't remember the exact approach that I implemented for this purpose. You should just try it out and let us know here. – SS_Rebelious Feb 17 '20 at 19:28
  • @SS_Rebelious I can confirm this works `my_task.apply_async(kwargs=kwargs, queue=queue, task_id=task_id)` and you can get kwargs, queue, and task_id from example something like `app.backend.get_task_meta(task_id)` where app is your celery app. This is useful if for example your user has a failed task in their UI and you just want to allow them to re-run it and then get the hopefully successful result back with the same task id etc. – Alex L Apr 02 '22 at 22:46
1

This works now after setting celery.conf.update(result_extended=True) which persists the arguments passed to the task:

def retry_task(task_id):    
    meta=celery.backend.get_task_meta(task_id)
    task = celery.tasks[meta['name']]
    task.apply_async(args=meta['args'], kwargs=meta['kwargs']) #specify any other parameters you might be passing
Ajay Gupta
  • 1,775
  • 1
  • 10
  • 22