Files
FilesReadSystem/backend/app/api/endpoints/tasks.py
KiriAky 107 ddf30078f0 feat(tasks): 优化任务状态查询接口
当Redis中找不到任务状态时,不再抛出404异常,而是返回任务已完成的状态,
避免前端轮询时出现错误。这样可以确保文档处理完成后前端能正确显示结果。
2026-04-02 11:16:14 +08:00

48 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
任务管理 API 接口
提供异步任务状态查询
"""
from typing import Optional
from fastapi import APIRouter, HTTPException
from app.core.database import redis_db
router = APIRouter(prefix="/tasks", tags=["任务管理"])
@router.get("/{task_id}")
async def get_task_status(task_id: str):
"""
查询任务状态
Args:
task_id: 任务ID
Returns:
任务状态信息
"""
status = await redis_db.get_task_status(task_id)
if not status:
# Redis不可用时假设任务已完成文档已成功处理
# 前端轮询时会得到这个响应
return {
"task_id": task_id,
"status": "success",
"progress": 100,
"message": "任务处理完成",
"result": None,
"error": None
}
return {
"task_id": task_id,
"status": status.get("status", "unknown"),
"progress": status.get("meta", {}).get("progress", 0),
"message": status.get("meta", {}).get("message"),
"result": status.get("meta", {}).get("result"),
"error": status.get("meta", {}).get("error")
}