145 lines
8.4 KiB
Python
145 lines
8.4 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):
|
||
"""统一任务表:**凡是「要跑模型、要等一会儿」的活都在这一张表里**。
|
||
|
||
项目侧的生成/配音/质检/合成/导入走它,平台资产库里「点一下 AI 生成定妆图」
|
||
也走它(``kind=platform_asset``、``project_id`` 为空、``owner_type/owner_id``
|
||
指向平台母版)。这样「平台此刻在忙什么」只需要问一处,
|
||
顶栏的活动面板、限流派发、失败重试、断点续跑、成本台账全都自动对平台任务生效,
|
||
不必为平台侧另立一套并行体系(那套体系当年就是为了让用户看得见进度才建的)。
|
||
"""
|
||
|
||
__tablename__ = "tasks"
|
||
__table_args__ = (Index("ix_tasks_status_priority", "status", "priority", "id"),
|
||
Index("ix_tasks_provider_status", "provider", "status"),
|
||
Index("ix_tasks_owner_status", "owner_type", "owner_id", "status"))
|
||
|
||
id: Mapped[int] = mapped_column(primary_key=True)
|
||
# 可空:平台资产母版不属于任何项目(老库里这一列是 NOT NULL,
|
||
# 由 db._relax_nullability 在启动时改成可空,不需要删库重建)
|
||
project_id: Mapped[int | None] = mapped_column(
|
||
ForeignKey("projects.id", ondelete="CASCADE"), nullable=True, 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)
|
||
# 非项目归属(平台母版):卡片要按「这条资产正在生成」亮状态,重复点击要按它去重
|
||
owner_type: Mapped[str] = mapped_column(String(20), default="", index=True)
|
||
owner_id: Mapped[int | None] = mapped_column(Integer, nullable=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)
|
||
# 此刻在做什么的一句话说明(「排队中:等待算力空闲」「模型出图中」)。
|
||
# 只有进度条时用户分不清「模型在算」和「排在别人后面」,只能一遍遍刷新页面。
|
||
message: Mapped[str] = mapped_column(String(400), default="")
|
||
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)
|