Files

79 lines
4.6 KiB
Python

"""持久化后台任务:AI 套图、单张编辑共用的父任务与逐图子任务。"""
from __future__ import annotations
import uuid
from datetime import datetime
from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, String, Text, Uuid, func
from sqlalchemy.orm import Mapped, mapped_column
from db import Base
from models.types import JSONType
class BackgroundJob(Base):
"""一个用户可见的后台操作:套图、单张编辑或批量水印。"""
__tablename__ = "background_jobs"
id: Mapped[uuid.UUID] = mapped_column(Uuid(as_uuid=True), primary_key=True, default=uuid.uuid4)
# 商品删除后保留任务诊断记录,但 Worker 不会再把结果回写到不存在的商品。
product_id: Mapped[uuid.UUID | None] = mapped_column(
Uuid(as_uuid=True), ForeignKey("products.id", ondelete="SET NULL"), nullable=True, index=True
)
kind: Mapped[str] = mapped_column(String(32), index=True) # image_suite | image_edit | watermark_batch
status: Mapped[str] = mapped_column(String(24), default="queued", index=True)
priority: Mapped[int] = mapped_column(Integer, default=0, index=True)
provider: Mapped[str] = mapped_column(String(32), default="")
model: Mapped[str | None] = mapped_column(String(128), nullable=True)
platform: Mapped[str] = mapped_column(String(16), default="ozon")
lang: Mapped[str] = mapped_column(String(16), default="ru")
ratio: Mapped[str] = mapped_column(String(16), default="3:4")
style_set: Mapped[int] = mapped_column(Integer, default=1)
# 原始请求的不可变快照:商品上下文、参考图池、风格、要求、水印等。
request_payload: Mapped[dict] = mapped_column(JSONType, default=dict)
total: Mapped[int] = mapped_column(Integer, default=0)
completed: Mapped[int] = mapped_column(Integer, default=0)
failed: Mapped[int] = mapped_column(Integer, default=0)
cancel_requested: Mapped[bool] = mapped_column(Boolean, default=False, index=True)
error: Mapped[str | None] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), index=True)
started_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
completed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), index=True
)
class BackgroundJobItem(Base):
"""任务中的一个可调度工作单元:套图的一张图或单张编辑。"""
__tablename__ = "background_job_items"
id: Mapped[uuid.UUID] = mapped_column(Uuid(as_uuid=True), primary_key=True, default=uuid.uuid4)
job_id: Mapped[uuid.UUID] = mapped_column(
Uuid(as_uuid=True), ForeignKey("background_jobs.id", ondelete="CASCADE"), index=True
)
position: Mapped[int] = mapped_column(Integer, default=0)
status: Mapped[str] = mapped_column(String(24), default="queued", index=True)
payload: Mapped[dict] = mapped_column(JSONType, default=dict)
# 供应商异步任务 ID 与类型(pcore 还需 edits/generations);重启后继续轮询,不重新提交。
provider_task_id: Mapped[str | None] = mapped_column(String(512), nullable=True, index=True)
provider_task_kind: Mapped[str | None] = mapped_column(String(64), nullable=True)
attempts: Mapped[int] = mapped_column(Integer, default=0)
max_attempts: Mapped[int] = mapped_column(Integer, default=3)
result_url: Mapped[str | None] = mapped_column(Text, nullable=True)
source_url: Mapped[str | None] = mapped_column(Text, nullable=True)
watermark_config: Mapped[dict | None] = mapped_column(JSONType, nullable=True)
generated_asset_id: Mapped[uuid.UUID | None] = mapped_column(Uuid(as_uuid=True), nullable=True)
error: Mapped[str | None] = mapped_column(Text, nullable=True)
next_run_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True, index=True)
lease_owner: Mapped[str | None] = mapped_column(String(128), nullable=True)
lease_expires_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True, index=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
started_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
completed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), index=True
)