功能增加:新增后端传播任务执行器,支持异步自动传播、传播进度、结果统计、取消/重试状态同步。 功能增加:传播请求支持指定 SAM2.1 tiny/small/base+/large 权重,并记录 seed mask、source annotation 和传播范围。 功能增加:传播逻辑增加 seed 签名,未变化的 mask 二次传播会跳过,已变化的 mask 会先清理旧自动传播结果再重新生成,避免重复重叠。 功能增加:工作区增加传播范围二次选择、传播进度提示、人工/AI 标注帧红色标识、自动传播帧蓝色标识和当前帧双层边框。 功能增加:新增临时提示组件,让工具操作提示自动消失且不阻塞后续操作。 功能增加:补充项目删除、模板删除、任务失败详情、任务取消/重试等前后端联动状态。 功能增加:新增安装部署文档,补充当前需求冻结、设计冻结、接口契约、测试计划和 AGENTS/README 项目说明。 Bugfix:修复自动传播接口 404、传播后看不到任务进度、传播结果重复堆叠和已编辑帧提示不清晰的问题。 Bugfix:修复 AI 分割框选/点选交互、单候选 mask、删除选点、工作区保存与候选 mask 推送相关问题。 Bugfix:修复 Canvas 多边形顶点拖动告警、工具栏提示缺失、项目库 FPS 展示和若干 UI 文案/可用性问题。 测试:补充 AI 分割、Canvas、Dashboard、FrameTimeline、ProjectLibrary、TemplateRegistry、ToolsPalette、VideoWorkspace、API 和后端任务/AI/dashboard 测试。 验证:npm run lint;npm run test:run;python -m pytest backend/tests -q。
141 lines
5.1 KiB
Python
141 lines
5.1 KiB
Python
"""Processing task query endpoints."""
|
||
|
||
import logging
|
||
from datetime import datetime, timezone
|
||
from typing import List
|
||
|
||
from fastapi import APIRouter, Depends, HTTPException, status
|
||
from sqlalchemy.orm import Session
|
||
|
||
from celery_app import celery_app
|
||
from database import get_db
|
||
from models import ProcessingTask, Project
|
||
from progress_events import publish_task_progress_event
|
||
from schemas import ProcessingTaskOut
|
||
from statuses import (
|
||
PROJECT_STATUS_PARSING,
|
||
PROJECT_STATUS_PENDING,
|
||
PROJECT_STATUS_READY,
|
||
TASK_ACTIVE_STATUSES,
|
||
TASK_STATUS_CANCELLED,
|
||
TASK_STATUS_FAILED,
|
||
TASK_STATUS_QUEUED,
|
||
)
|
||
from worker_tasks import parse_project_media, propagate_project_masks
|
||
|
||
router = APIRouter(prefix="/api/tasks", tags=["Tasks"])
|
||
logger = logging.getLogger(__name__)
|
||
|
||
|
||
def _now() -> datetime:
|
||
return datetime.now(timezone.utc)
|
||
|
||
|
||
def _get_task_or_404(task_id: int, db: Session) -> ProcessingTask:
|
||
task = db.query(ProcessingTask).filter(ProcessingTask.id == task_id).first()
|
||
if not task:
|
||
raise HTTPException(status_code=404, detail="Task not found")
|
||
return task
|
||
|
||
|
||
def _project_status_after_stop(project: Project) -> str:
|
||
return PROJECT_STATUS_READY if project.frames else PROJECT_STATUS_PENDING
|
||
|
||
|
||
@router.get("", response_model=List[ProcessingTaskOut], summary="List processing tasks")
|
||
def list_tasks(
|
||
project_id: int | None = None,
|
||
status: str | None = None,
|
||
limit: int = 50,
|
||
db: Session = Depends(get_db),
|
||
) -> List[ProcessingTask]:
|
||
"""Return recent background processing tasks."""
|
||
query = db.query(ProcessingTask)
|
||
if project_id is not None:
|
||
query = query.filter(ProcessingTask.project_id == project_id)
|
||
if status is not None:
|
||
query = query.filter(ProcessingTask.status == status)
|
||
return query.order_by(ProcessingTask.created_at.desc()).limit(limit).all()
|
||
|
||
|
||
@router.get("/{task_id}", response_model=ProcessingTaskOut, summary="Get processing task")
|
||
def get_task(task_id: int, db: Session = Depends(get_db)) -> ProcessingTask:
|
||
"""Return one background task by id."""
|
||
return _get_task_or_404(task_id, db)
|
||
|
||
|
||
@router.post("/{task_id}/cancel", response_model=ProcessingTaskOut, summary="Cancel processing task")
|
||
def cancel_task(task_id: int, db: Session = Depends(get_db)) -> ProcessingTask:
|
||
"""Cancel a queued/running background task and revoke the Celery job when possible."""
|
||
task = _get_task_or_404(task_id, db)
|
||
if task.status not in TASK_ACTIVE_STATUSES:
|
||
raise HTTPException(
|
||
status_code=status.HTTP_409_CONFLICT,
|
||
detail=f"Task is not cancellable in status: {task.status}",
|
||
)
|
||
|
||
if task.celery_task_id:
|
||
try:
|
||
celery_app.control.revoke(task.celery_task_id, terminate=True, signal="SIGTERM")
|
||
except Exception as exc: # noqa: BLE001
|
||
logger.warning("Failed to revoke celery task %s: %s", task.celery_task_id, exc)
|
||
|
||
task.status = TASK_STATUS_CANCELLED
|
||
task.progress = 100
|
||
task.message = "任务已取消"
|
||
task.error = "Cancelled by user"
|
||
task.finished_at = _now()
|
||
if task.project:
|
||
task.project.status = _project_status_after_stop(task.project)
|
||
|
||
db.commit()
|
||
db.refresh(task)
|
||
publish_task_progress_event(task)
|
||
return task
|
||
|
||
|
||
@router.post("/{task_id}/retry", response_model=ProcessingTaskOut, status_code=status.HTTP_202_ACCEPTED, summary="Retry processing task")
|
||
def retry_task(task_id: int, db: Session = Depends(get_db)) -> ProcessingTask:
|
||
"""Create a fresh queued task from a failed or cancelled task."""
|
||
previous = _get_task_or_404(task_id, db)
|
||
if previous.status not in {TASK_STATUS_FAILED, TASK_STATUS_CANCELLED}:
|
||
raise HTTPException(
|
||
status_code=status.HTTP_409_CONFLICT,
|
||
detail=f"Task is not retryable in status: {previous.status}",
|
||
)
|
||
if previous.project_id is None:
|
||
raise HTTPException(status_code=400, detail="Task has no project_id")
|
||
|
||
project = db.query(Project).filter(Project.id == previous.project_id).first()
|
||
if not project:
|
||
raise HTTPException(status_code=404, detail="Project not found")
|
||
is_propagation_task = previous.task_type == "propagate_masks"
|
||
if not is_propagation_task and not project.video_path:
|
||
raise HTTPException(status_code=400, detail="Project has no media uploaded")
|
||
|
||
payload = dict(previous.payload or {})
|
||
payload.setdefault("source_type", project.source_type or "video")
|
||
payload["retry_of"] = previous.id
|
||
|
||
task = ProcessingTask(
|
||
task_type=previous.task_type,
|
||
status=TASK_STATUS_QUEUED,
|
||
progress=0,
|
||
message=f"重试任务已入队(源任务 #{previous.id})",
|
||
project_id=project.id,
|
||
payload=payload,
|
||
)
|
||
if not is_propagation_task:
|
||
project.status = PROJECT_STATUS_PARSING
|
||
db.add(task)
|
||
db.commit()
|
||
db.refresh(task)
|
||
publish_task_progress_event(task)
|
||
|
||
async_result = propagate_project_masks.delay(task.id) if is_propagation_task else parse_project_media.delay(task.id)
|
||
task.celery_task_id = async_result.id
|
||
db.commit()
|
||
db.refresh(task)
|
||
publish_task_progress_event(task)
|
||
return task
|