修复get_all_tasks: 改用_to_flat_response(不递归children)彻底消除MissingGreenlet

This commit is contained in:
2026-08-06 11:19:19 +08:00
parent 9846dd6c8f
commit ef9e5bbae5

View File

@ -91,6 +91,37 @@ async def _get_task_with_children_recursive(db: AsyncSession, task_id: uuid.UUID
return task
def _to_flat_response(task: Task) -> TaskResponse:
"""扁平序列化,不递归 children避免 MissingGreenlet"""
product_sn = ""
product_material = ""
try:
if task.product:
product_sn = task.product.serial_number or ""
product_material = (task.product.material_name or task.product.material_id or "")
except Exception:
pass
return TaskResponse(
id=task.id,
product_id=task.product_id,
product_sn=product_sn,
product_material=product_material,
parent_task_id=task.parent_task_id,
task_name=task.task_name,
assignee_id=task.assignee_id,
status=task.status,
notify_parent_on_complete=task.notify_parent_on_complete,
is_rework=task.is_rework,
remark=task.remark,
reject_reason=task.reject_reason,
received_at=task.received_at,
completed_at=task.completed_at,
created_at=task.created_at,
child_tasks=[],
records=[TaskRecordResponse.model_validate(r) for r in (task.records or [])],
)
def _to_response(task: Task) -> TaskResponse:
"""将 Task ORM 对象转为递归 TaskResponse"""
product_sn = ""
@ -237,9 +268,9 @@ async def get_all_tasks(
result = await db.execute(stmt)
tasks = result.scalars().all()
# 返回所有匹配的任务(含子任务),不再过滤 parent_task_id
all_tasks = [_to_response(t) for t in tasks]
return TaskListResponse(tasks=all_tasks, total=len(all_tasks))
# 返回扁平列表(不递归 children避免 MissingGreenlet
flat_tasks = [_to_flat_response(t) for t in tasks]
return TaskListResponse(tasks=flat_tasks, total=len(flat_tasks))
# ============================================================