128 lines
7.0 KiB
Python
128 lines
7.0 KiB
Python
"""统一任务编排:任务队列、生成记录、成本统计、事件日志(断点续跑的持久化基础)。"""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import JSON, DateTime, Float, ForeignKey, Integer, String, Text, Index
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.constants import TaskKind, TaskStatus
|
|
from app.models.base import TimestampMixin, now_utc
|
|
from app.db import Base
|
|
|
|
|
|
class Task(TimestampMixin, Base):
|
|
"""统一任务表:所有异步工作(生成/配音/质检/合成/导入)都走同一编排。"""
|
|
|
|
__tablename__ = "tasks"
|
|
__table_args__ = (Index("ix_tasks_status_priority", "status", "priority", "id"),
|
|
Index("ix_tasks_provider_status", "provider", "status"))
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
project_id: Mapped[int] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"), index=True)
|
|
shot_id: Mapped[int | None] = mapped_column(ForeignKey("shots.id", ondelete="CASCADE"), nullable=True, index=True)
|
|
timeline_id: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True)
|
|
kind: Mapped[str] = mapped_column(String(30), default=TaskKind.GENERATE_SHOT, index=True)
|
|
name: Mapped[str] = mapped_column(String(200), default="")
|
|
stage: Mapped[str] = mapped_column(String(40), default="dispatch")
|
|
|
|
status: Mapped[str] = mapped_column(String(30), default=TaskStatus.QUEUED, index=True)
|
|
priority: Mapped[int] = mapped_column(Integer, default=5)
|
|
progress: Mapped[float] = mapped_column(Float, default=0.0)
|
|
|
|
provider: Mapped[str] = mapped_column(String(60), default="")
|
|
model: Mapped[str] = mapped_column(String(120), default="")
|
|
mode: Mapped[str] = mapped_column(String(20), default="")
|
|
idempotency_key: Mapped[str] = mapped_column(String(160), default="", index=True)
|
|
celery_task_id: Mapped[str] = mapped_column(String(120), default="")
|
|
execution_backend: Mapped[str] = mapped_column(String(20), default="celery")
|
|
worker_id: Mapped[str] = mapped_column(String(120), default="")
|
|
|
|
payload_json: Mapped[dict] = mapped_column(JSON, default=dict)
|
|
checkpoint_json: Mapped[dict] = mapped_column(JSON, default=dict) # 断点续跑上下文
|
|
result_json: Mapped[dict] = mapped_column(JSON, default=dict)
|
|
output_path: Mapped[str | None] = mapped_column(String(600), nullable=True)
|
|
|
|
attempts: Mapped[int] = mapped_column(Integer, default=0)
|
|
max_attempts: Mapped[int] = mapped_column(Integer, default=3)
|
|
next_retry_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
error_code: Mapped[str] = mapped_column(String(60), default="")
|
|
error_message: Mapped[str] = mapped_column(Text, default="")
|
|
error_history: Mapped[list] = mapped_column(JSON, default=list)
|
|
|
|
cost: Mapped[float] = mapped_column(Float, default=0.0)
|
|
cost_currency: Mapped[str] = mapped_column(String(10), default="CNY")
|
|
|
|
planned_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
queued_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
started_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
heartbeat_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
finished_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
|
|
project: Mapped["Project"] = relationship() # noqa: F821
|
|
|
|
|
|
class GenerationRecord(Base):
|
|
"""生成记录:每次成功调用的供应商/模型/时长/成本/产物快照(可审计、可复现)。"""
|
|
|
|
__tablename__ = "generation_records"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
project_id: Mapped[int] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"), index=True)
|
|
shot_id: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True)
|
|
task_id: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True)
|
|
provider: Mapped[str] = mapped_column(String(60), default="")
|
|
model: Mapped[str] = mapped_column(String(120), default="")
|
|
kind: Mapped[str] = mapped_column(String(30), default=TaskKind.GENERATE_SHOT)
|
|
prompt: Mapped[str] = mapped_column(Text, default="")
|
|
params_json: Mapped[dict] = mapped_column(JSON, default=dict)
|
|
provider_job_id: Mapped[str] = mapped_column(String(200), default="")
|
|
asset_paths: Mapped[list] = mapped_column(JSON, default=list) # 使用的关键帧/定妆图等
|
|
output_path: Mapped[str] = mapped_column(String(600), default="")
|
|
duration_seconds: Mapped[float] = mapped_column(Float, default=0.0)
|
|
cost: Mapped[float] = mapped_column(Float, default=0.0)
|
|
currency: Mapped[str] = mapped_column(String(10), default="CNY")
|
|
attempts: Mapped[int] = mapped_column(Integer, default=1)
|
|
metadata_json: Mapped[dict] = mapped_column(JSON, default=dict)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=now_utc, index=True)
|
|
|
|
|
|
class CostRecord(Base):
|
|
"""成本统计明细。"""
|
|
|
|
__tablename__ = "cost_records"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
project_id: Mapped[int] = mapped_column(ForeignKey("projects.id", ondelete="CASCADE"), index=True)
|
|
shot_id: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True)
|
|
task_id: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True)
|
|
provider: Mapped[str] = mapped_column(String(60), default="")
|
|
model: Mapped[str] = mapped_column(String(120), default="")
|
|
kind: Mapped[str] = mapped_column(String(30), default="")
|
|
quantity: Mapped[float] = mapped_column(Float, default=0.0)
|
|
unit: Mapped[str] = mapped_column(String(30), default="second")
|
|
unit_price: Mapped[float] = mapped_column(Float, default=0.0)
|
|
cost: Mapped[float] = mapped_column(Float, default=0.0)
|
|
currency: Mapped[str] = mapped_column(String(10), default="CNY")
|
|
note: Mapped[str] = mapped_column(Text, default="")
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=now_utc, index=True)
|
|
|
|
|
|
class EventLog(Base):
|
|
"""状态跟踪 / 审计事件流。"""
|
|
|
|
__tablename__ = "event_logs"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
project_id: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True)
|
|
shot_id: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True)
|
|
task_id: Mapped[int | None] = mapped_column(Integer, nullable=True, index=True)
|
|
level: Mapped[str] = mapped_column(String(10), default="info")
|
|
action: Mapped[str] = mapped_column(String(60), default="", index=True)
|
|
entity_type: Mapped[str] = mapped_column(String(40), default="")
|
|
entity_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
|
message: Mapped[str] = mapped_column(Text, default="")
|
|
data_json: Mapped[dict] = mapped_column(JSON, default=dict)
|
|
actor: Mapped[str] = mapped_column(String(80), default="system")
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=now_utc, index=True)
|