Search code examples
celerydjango-celerycelery-task

Celery send_task and retry on exception


I want to retry (official doc) a task when it raises an exception. Celery allows this by using the retry in form of self.retry(...)

Now, i can't figure out how to user self since i've a function without any class.

My code is this

.. imports ...
app = Celery('elasticcelery')
@app.task(name='rm_doc')
def rm_doc(schema_id, id):
    es = Elasticsearch(es_ip)
    try:
        res = es.delete(schema_id, 'doc', id)
    except NotFoundError as e:
        <here goes the retry>

and it's called from another service in this way:

app_celery = Celery('celeryelastic')
                app_celery.config_from_object('django.conf:settings')
                app_celery.send_task('rm_doc', kwargs={"schema_id": schema_id, "id": document_id}, )

now, I should add the self.retry but there's no self in my method.

How should I proceed?

PS: I tried adding self as parmeter, but this fails since there's no mapping when the task is called the first time from the remote.


Solution

  • I forgot bind=True in the annotation of the method, now I can add self.