Celery任务状态报错一例

Celery 中定义FAILURE状态时,报错:KeyError: 'exc_type',完整内容大致如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
>>> print(task.state)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.6/site-packages/celery/result.py", line 471, in state
return self._get_task_meta()['status']
File "/usr/local/lib/python3.6/site-packages/celery/result.py", line 410, in _get_task_meta
return self._maybe_set_cache(self.backend.get_task_meta(self.id))
File "/usr/local/lib/python3.6/site-packages/celery/backends/base.py", line 359, in get_task_meta
meta = self._get_task_meta_for(task_id)
File "/usr/local/lib/python3.6/site-packages/celery/backends/base.py", line 674, in _get_task_meta_for
return self.decode_result(meta)
File "/usr/local/lib/python3.6/site-packages/celery/backends/base.py", line 278, in decode_result
return self.meta_from_decoded(self.decode(payload))
File "/usr/local/lib/python3.6/site-packages/celery/backends/base.py", line 274, in meta_from_decoded
meta['result'] = self.exception_to_python(meta['result'])
File "/usr/local/lib/python3.6/site-packages/celery/backends/base.py", line 248, in exception_to_python
from_utf8(exc['exc_type']), __name__)
KeyError: 'exc_type'

这个报错主要是meta字段中缺少exc_typeexc_message关键字导致的,在meta中添加这两个关键字即可消除报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
@app.task(bind=True)
def task(self):
try:
raise ValueError('Some error')
except Exception as ex:
self.update_state(
state=states.FAILURE,
meta={
'exc_type': type(ex).__name__,
'exc_message': traceback.format_exc().split('\n')
'custom': '...'
})
raise Ignore()

需要注意的是,在使用返回response时就不能再使用task.info来返回内容了,需要使用如下文件返回:

1
2
3
4
5
6
7
8
9
>>> import tasks
>>> task = tasks.task.s().delay()
>>> print(task.state)
'FAILURE'
>>> print(task.info)
ValueError('Traceback (most recent call last):', ' File "/app/tasks.py", line 16, in task', " raise ValueError('some exception')", 'ValueError: some exception', '')
>>> print(task.backend.get(task.backend.get_key_for_task(task.id)))
b'{"status": "FAILURE", "result": {"exc_type": "ValueError", "exc_message": ["Traceback (most recent call last):", " File \\"/app/tasks.py\\", line 16, in task", " raise ValueError(\'some exception\')", "ValueError: some exception", ""], "custom": "..."}, "traceback": null, "children": [], "task_id": "d2f60111-aec6-4c58-83a7-24f0edb7ac5f"}'
Custom state