from datetime import datetime from sqlalchemy import DateTime, Integer, String, Text from sqlalchemy.dialects.sqlite import JSON from sqlalchemy.orm import Mapped, mapped_column from core_db import AuditMixin, Base, TenantMixin, VersionMixin from core_shared import JSONValue class ScheduledJob(TenantMixin, AuditMixin, VersionMixin, Base): __tablename__ = "scheduled_job" job_type: Mapped[str] = mapped_column(String(32), index=True) status: Mapped[str] = mapped_column(String(32), default="scheduled", index=True) name: Mapped[str] = mapped_column(String(128)) description: Mapped[str | None] = mapped_column(Text, nullable=True) target_service: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True) target_url: Mapped[str | None] = mapped_column(Text, nullable=True) method: Mapped[str | None] = mapped_column(String(16), nullable=True) payload_json: Mapped[dict[str, JSONValue]] = mapped_column(JSON, default=dict) schedule_time: Mapped[datetime] = mapped_column(DateTime, index=True) lease_expire_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) claimed_by: Mapped[str | None] = mapped_column(String(128), nullable=True, index=True) claimed_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) completed_time: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) attempt_count: Mapped[int] = mapped_column(Integer, default=0) max_attempts: Mapped[int] = mapped_column(Integer, default=3) last_error_message: Mapped[str | None] = mapped_column(Text, nullable=True) metadata_json: Mapped[dict[str, JSONValue]] = mapped_column(JSON, default=dict)