Refactor get_status and create_archive_task error handling

Raise exceptions instead of returning error messages from the worker in
create_arvive_task. This ensures consistency in how the errors are
presented on the task result: the Exception will be the result instead
of *maybe* being wrapped in an object like {error: Exception}.

This lets us simplify error handling in get_status so we have only one
try/except block where the error can be returned to the client.
This commit is contained in:
Lilia Kai
2023-09-06 13:54:32 +02:00
parent 00201770ba
commit f20dd05928
2 changed files with 19 additions and 16 deletions

View File

@@ -103,29 +103,29 @@ def archive_tasks(archive:schemas.ArchiveCreate, email = Depends(get_bearer_auth
@app.get("/tasks/{task_id}")
def get_status(task_id, email = Depends(get_bearer_auth)):
logger.info(f"status check for user {email}")
logger.info(f"status check for user {email} task {task_id}")
task = AsyncResult(task_id, app=celery)
response = {
"id": task_id,
"status": task.status,
"result": task.result
}
try:
if task.result and "error" in task.result:
response["status"] = "FAILURE"
except Exception as e:
logger.error(e)
logger.error(traceback.format_exc())
response["status"] = "FAILURE"
try:
if task.status == "FAILURE":
# *FAILURE* The task raised an exception, or has exceeded the retry limit.
# The :attr:`result` attribute then contains the exception raised by the task.
# https://docs.celeryq.dev/en/stable/_modules/celery/result.html#AsyncResult
raise task.result
response = {
"id": task_id,
"status": task.status,
"result": task.result
}
return JSONResponse(jsonable_encoder(response, exclude_unset=True))
except Exception as e:
logger.error(e)
logger.error(traceback.format_exc())
return JSONResponse({
"id": task_id,
"status": "FAILURE",
"result": {"error": e}
"result": {"error": str(e)}
})
@app.delete("/tasks/{task_id}")