Suppose i have this task definition:
def some_other_foo(input)
raise Exception('This is not handled!')
return input
@app.task(
bind=True,
max_retries=5,
soft_time_limit=20)
def some_foo(self, someInput={}):
response=""
try:
response = some_other_foo(someInput)
except Exception as exc:
self.retry(countdown=5, exc=exc)
response="error"
return response
I have a problem that exception is not handled in some_foo, I get error instead of response="error", task is crashed and i get Traceback that indicates an Exception was raised.
Is it possible to return regular response, but to set celery task as failed, so result in flower will be failed ?
I am using:
Celery 4.1
AMPQ as broker
Celery Flower as monitoring
Try \ except
works fine. Your task will always be unsuccessful because you calledself.retry
beforereturn
. Let's make a little test:Run celery app and call our task. You will see in celery logs something like this:
How it works. You set for the task
max_retries=5
. When you calledself.retry(countdown=5, exc=exc)
Celery interrupts processing of task and try to restart task withcountdown
(in our case = 5). After 5 attempts(max_retries
) Celery won't rerun task.Now, let's change our
try \ except
block to:Restart Celery and run our task. Let's check log:
As you can see handler works fine.
So, summarize. If you call
self.retry
, Celery will interrupt processing of task and try to restart a current task.