Merge pull request #13 from bellingcat/get_status

This commit is contained in:
Miguel Sozinho Ramalho
2023-09-22 10:30:29 +01:00
committed by GitHub
2 changed files with 22 additions and 20 deletions

View File

@@ -102,30 +102,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}")
task_result = AsyncResult(task_id, app=celery)
result = {
"id": task_id,
"status": task_result.status,
"result": task_result.result
}
logger.info(f"status check for user {email} task {task_id}")
task = AsyncResult(task_id, app=celery)
try:
if task_result.result and "error" in task_result.result:
result["status"] = "FAILURE"
except Exception as e:
logger.error(e)
logger.error(traceback.format_exc())
result["status"] = "FAILURE"
try:
json_result = jsonable_encoder(result, exclude_unset=True)
return JSONResponse(json_result)
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}")