从任务恢复失败超过max_retries [英] Recover from task failed beyond max_retries
问题描述
我正在尝试异步地使用Web服务,因为它需要长达45秒才能返回。不幸的是,这个Web服务也有些不可靠,可能会导致错误。我已经设置了 django-celery
并让我的任务执行,这可以正常工作,直到任务失败超过 max_retries
。
I am attempting to asynchronously consume a web service because it takes up to 45 seconds to return. Unfortunately, this web service is also somewhat unreliable and can throw errors. I have set up django-celery
and have my tasks executing, which works fine until the task fails beyond max_retries
.
这是我到目前为止所有的:
Here is what I have so far:
@task(default_retry_delay=5, max_retries=10)
def request(xml):
try:
server = Client('https://www.whatever.net/RealTimeService.asmx?wsdl')
xml = server.service.RunRealTimeXML(
username=settings.WS_USERNAME,
password=settings.WS_PASSWORD,
xml=xml
)
except Exception, e:
result = Result(celery_id=request.request.id, details=e.reason, status="i")
result.save()
try:
return request.retry(exc=e)
except MaxRetriesExceededError, e:
result = Result(celery_id=request.request.id, details="Max Retries Exceeded", status="f")
result.save()
raise
result = Result(celery_id=request.request.id, details=xml, status="s")
result.save()
return result
不幸的是, MaxRetriesExceededError
没有被 retry()
抛出,所以我不确定如何处理这个任务的失败。 Django已经将HTML返回给客户端,我正在通过AJAX检查 Result
的内容,这是永远不会完全失败的。 f
状态。
Unfortunately, MaxRetriesExceededError
is not being thrown by retry()
, so I'm not sure how to handle the failure of this task. Django has already returned HTML to the client, and I am checking the contents of Result
via AJAX, which is never getting to a full fail f
status.
所以问题是:当Celery任务超过 max_retries
?时,如何更新我的数据库? >
So the question is: How can I update my database when the Celery task has exceeded max_retries
?
推荐答案
您可以覆盖芹菜任务类的after_return方法,该方法在执行任务后调用任何ret状态( SUCCESS,FAILED,RETRY)
You can override the after_return method of the celery task class, this method is called after the execution of the task whatever is the ret status (SUCCESS,FAILED,RETRY)
class MyTask(celery.task.Task)
def run(self, xml, **kwargs)
#Your stuffs here
def after_return(self, status, retval, task_id, args, kwargs, einfo=None):
if self.max_retries == int(kwargs['task_retries']):
#If max retries are equals to task retries do something
if status == "FAILURE":
#You can do also something if the tasks fail instead of check the retries
这篇关于从任务恢复失败超过max_retries的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!