refactor: celery task result endpoint crashes on task failure
When a Celery task fails, `result.ready()` evaluates to `True` but `result.successful()` is `False`. Calling `result.get()` without `propagate=False` on a failed task will re-raise the task's exception, causing the Flask endpoint to crash with a 500 Internal Server Error instead of returning the task's failed status. Additionally, if `result.result` is an Exception object, returning it directly in the dictionary will cause a `TypeError` during JSON serialization. Affected files: views.py Signed-off-by: Tang Vu <vuminhtang2212@gmail.com>
This commit is contained in:
parent
7ef2946fb5
commit
d2e34be0f6
1 changed files with 4 additions and 2 deletions
|
|
@ -11,10 +11,12 @@ bp = Blueprint("tasks", __name__, url_prefix="/tasks")
|
|||
def result(id: str) -> dict[str, object]:
|
||||
result = AsyncResult(id)
|
||||
ready = result.ready()
|
||||
successful = result.successful() if ready else None
|
||||
value = result.get() if successful else result.result
|
||||
return {
|
||||
"ready": ready,
|
||||
"successful": result.successful() if ready else None,
|
||||
"value": result.get() if ready else result.result,
|
||||
"successful": successful,
|
||||
"value": str(value) if isinstance(value, Exception) else value,
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue