Implement FastAPI Temporal MVP pipeline

This commit is contained in:
Codex
2026-03-27 00:10:28 +08:00
commit cc03da8a94
52 changed files with 3619 additions and 0 deletions

33
app/infra/db/base.py Normal file
View File

@@ -0,0 +1,33 @@
"""Database base declarations."""
from datetime import datetime, timezone
from sqlalchemy import DateTime
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
def utc_now() -> datetime:
"""Return the current UTC timestamp."""
return datetime.now(timezone.utc)
class Base(DeclarativeBase):
"""Shared declarative base."""
class TimestampMixin:
"""Mixin that adds created and updated timestamps."""
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
default=utc_now,
nullable=False,
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
default=utc_now,
onupdate=utc_now,
nullable=False,
)

View File

@@ -0,0 +1,31 @@
"""Asset ORM model."""
from typing import Any
from sqlalchemy import Enum, ForeignKey, Integer, JSON, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.domain.enums import AssetType, WorkflowStepName
from app.infra.db.base import Base, TimestampMixin
class AssetORM(TimestampMixin, Base):
"""Persisted generated asset."""
__tablename__ = "assets"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
order_id: Mapped[int] = mapped_column(ForeignKey("orders.id"), nullable=False, index=True)
asset_type: Mapped[AssetType] = mapped_column(
Enum(AssetType, native_enum=False),
nullable=False,
)
step_name: Mapped[WorkflowStepName | None] = mapped_column(
Enum(WorkflowStepName, native_enum=False),
nullable=True,
)
uri: Mapped[str] = mapped_column(String(500), nullable=False)
metadata_json: Mapped[dict[str, Any] | None] = mapped_column(JSON, nullable=True)
order = relationship("OrderORM", back_populates="assets")

View File

@@ -0,0 +1,38 @@
"""Order ORM model."""
from sqlalchemy import Enum, Integer
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.domain.enums import CustomerLevel, OrderStatus, ServiceMode
from app.infra.db.base import Base, TimestampMixin
class OrderORM(TimestampMixin, Base):
"""Persisted order record."""
__tablename__ = "orders"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
customer_level: Mapped[CustomerLevel] = mapped_column(
Enum(CustomerLevel, native_enum=False),
nullable=False,
)
service_mode: Mapped[ServiceMode] = mapped_column(
Enum(ServiceMode, native_enum=False),
nullable=False,
)
status: Mapped[OrderStatus] = mapped_column(
Enum(OrderStatus, native_enum=False),
nullable=False,
default=OrderStatus.CREATED,
)
model_id: Mapped[int] = mapped_column(Integer, nullable=False)
pose_id: Mapped[int] = mapped_column(Integer, nullable=False)
garment_asset_id: Mapped[int] = mapped_column(Integer, nullable=False)
scene_ref_asset_id: Mapped[int] = mapped_column(Integer, nullable=False)
final_asset_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
assets = relationship("AssetORM", back_populates="order", lazy="selectin")
review_tasks = relationship("ReviewTaskORM", back_populates="order", lazy="selectin")
workflow_runs = relationship("WorkflowRunORM", back_populates="order", lazy="selectin")

View File

@@ -0,0 +1,31 @@
"""Review task ORM model."""
from sqlalchemy import Enum, ForeignKey, Integer, String, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.domain.enums import ReviewDecision, ReviewTaskStatus
from app.infra.db.base import Base, TimestampMixin
class ReviewTaskORM(TimestampMixin, Base):
"""Persisted review task."""
__tablename__ = "review_tasks"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
order_id: Mapped[int] = mapped_column(ForeignKey("orders.id"), nullable=False, index=True)
status: Mapped[ReviewTaskStatus] = mapped_column(
Enum(ReviewTaskStatus, native_enum=False),
nullable=False,
default=ReviewTaskStatus.PENDING,
)
decision: Mapped[ReviewDecision | None] = mapped_column(
Enum(ReviewDecision, native_enum=False),
nullable=True,
)
reviewer_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
selected_asset_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
comment: Mapped[str | None] = mapped_column(Text, nullable=True)
order = relationship("OrderORM", back_populates="review_tasks")

View File

@@ -0,0 +1,36 @@
"""Workflow run ORM model."""
from sqlalchemy import Enum, ForeignKey, Integer, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.domain.enums import OrderStatus, WorkflowStepName
from app.infra.db.base import Base, TimestampMixin
class WorkflowRunORM(TimestampMixin, Base):
"""Persisted workflow execution state."""
__tablename__ = "workflow_runs"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
order_id: Mapped[int] = mapped_column(ForeignKey("orders.id"), nullable=False, index=True)
workflow_id: Mapped[str] = mapped_column(String(255), nullable=False, unique=True)
workflow_type: Mapped[str] = mapped_column(String(255), nullable=False)
status: Mapped[OrderStatus] = mapped_column(
Enum(OrderStatus, native_enum=False),
nullable=False,
default=OrderStatus.CREATED,
)
current_step: Mapped[WorkflowStepName | None] = mapped_column(
Enum(WorkflowStepName, native_enum=False),
nullable=True,
)
order = relationship("OrderORM", back_populates="workflow_runs")
steps = relationship(
"WorkflowStepORM",
back_populates="workflow_run",
lazy="selectin",
order_by="WorkflowStepORM.started_at",
)

View File

@@ -0,0 +1,42 @@
"""Workflow step ORM model."""
from datetime import datetime
from typing import Any
from sqlalchemy import DateTime, Enum, ForeignKey, Integer, JSON, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.domain.enums import StepStatus, WorkflowStepName
from app.infra.db.base import Base, utc_now
class WorkflowStepORM(Base):
"""Persisted workflow step execution record."""
__tablename__ = "workflow_steps"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
workflow_run_id: Mapped[int] = mapped_column(
ForeignKey("workflow_runs.id"),
nullable=False,
index=True,
)
step_name: Mapped[WorkflowStepName] = mapped_column(
Enum(WorkflowStepName, native_enum=False),
nullable=False,
)
step_status: Mapped[StepStatus] = mapped_column(
Enum(StepStatus, native_enum=False),
nullable=False,
)
input_json: Mapped[dict[str, Any] | None] = mapped_column(JSON, nullable=True)
output_json: Mapped[dict[str, Any] | None] = mapped_column(JSON, nullable=True)
error_message: Mapped[str | None] = mapped_column(Text, nullable=True)
started_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
default=utc_now,
nullable=False,
)
ended_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
workflow_run = relationship("WorkflowRunORM", back_populates="steps")

65
app/infra/db/session.py Normal file
View File

@@ -0,0 +1,65 @@
"""Async database engine and session helpers."""
from collections.abc import AsyncGenerator
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker, create_async_engine
from app.config.settings import get_settings
from app.infra.db.base import Base
_engine: AsyncEngine | None = None
_session_factory: async_sessionmaker[AsyncSession] | None = None
def get_async_engine() -> AsyncEngine:
"""Return the lazily created async SQLAlchemy engine."""
global _engine
if _engine is None:
_engine = create_async_engine(
get_settings().database_url,
future=True,
echo=False,
)
return _engine
def get_session_factory() -> async_sessionmaker[AsyncSession]:
"""Return the lazily created async session factory."""
global _session_factory
if _session_factory is None:
_session_factory = async_sessionmaker(get_async_engine(), expire_on_commit=False)
return _session_factory
async def get_db_session() -> AsyncGenerator[AsyncSession, None]:
"""Yield a database session for FastAPI dependencies."""
async with get_session_factory()() as session:
yield session
async def init_database() -> None:
"""Create database tables when running the MVP without migrations."""
from app.infra.db.models.asset import AssetORM
from app.infra.db.models.order import OrderORM
from app.infra.db.models.review_task import ReviewTaskORM
from app.infra.db.models.workflow_run import WorkflowRunORM
from app.infra.db.models.workflow_step import WorkflowStepORM
del AssetORM, OrderORM, ReviewTaskORM, WorkflowRunORM, WorkflowStepORM
async with get_async_engine().begin() as connection:
await connection.run_sync(Base.metadata.create_all)
async def dispose_database() -> None:
"""Dispose the active engine and clear cached session objects."""
global _engine, _session_factory
if _engine is not None:
await _engine.dispose()
_engine = None
_session_factory = None

View File

@@ -0,0 +1,35 @@
"""Temporal client helpers."""
import asyncio
from temporalio.client import Client
from app.config.settings import get_settings
_client: Client | None = None
_client_lock = asyncio.Lock()
async def get_temporal_client() -> Client:
"""Return a cached Temporal client."""
global _client
if _client is not None:
return _client
async with _client_lock:
if _client is None:
settings = get_settings()
_client = await Client.connect(
settings.temporal_address,
namespace=settings.temporal_namespace,
)
return _client
def set_temporal_client(client: Client | None) -> None:
"""Override the cached Temporal client, primarily for tests."""
global _client
_client = client

View File

@@ -0,0 +1,8 @@
"""Temporal task queue names."""
IMAGE_PIPELINE_CONTROL_TASK_QUEUE = "image-pipeline-control"
IMAGE_PIPELINE_IMAGE_GEN_TASK_QUEUE = "image-pipeline-image-gen"
IMAGE_PIPELINE_POST_PROCESS_TASK_QUEUE = "image-pipeline-post-process"
IMAGE_PIPELINE_QC_TASK_QUEUE = "image-pipeline-qc"
IMAGE_PIPELINE_EXPORT_TASK_QUEUE = "image-pipeline-export"