File size: 3,035 Bytes
04bb755 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | # Copyright (c) 2026 CrewHub. All rights reserved.
# Proprietary and confidential. See LICENSE for details.
"""Schedule model — recurring execution of tasks, workflows, or crews."""
import uuid
from datetime import datetime
from decimal import Decimal
from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, JSON, Numeric, String, Text, Uuid, func
from sqlalchemy.orm import Mapped, mapped_column, relationship
from src.database import Base
class Schedule(Base):
__tablename__ = "schedules"
id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
owner_id: Mapped[uuid.UUID] = mapped_column(
Uuid, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
)
name: Mapped[str] = mapped_column(String(255), nullable=False)
schedule_type: Mapped[str] = mapped_column(String(20), nullable=False)
target_id: Mapped[uuid.UUID | None] = mapped_column(Uuid, nullable=True)
task_params: Mapped[dict | None] = mapped_column(JSON, nullable=True)
cron_expression: Mapped[str] = mapped_column(String(100), nullable=False)
timezone: Mapped[str] = mapped_column(String(50), nullable=False, default="UTC")
input_message: Mapped[str | None] = mapped_column(Text, nullable=True)
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
next_run_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
last_run_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
run_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
max_runs: Mapped[int | None] = mapped_column(Integer, nullable=True)
consecutive_failures: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
max_consecutive_failures: Mapped[int] = mapped_column(Integer, nullable=False, default=3)
credit_minimum: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
# UCP: Credit-aware execution
credit_budget_per_run: Mapped[Decimal | None] = mapped_column(
Numeric(12, 4), nullable=True
)
pause_reason: Mapped[str | None] = mapped_column(String(50), nullable=True)
last_notified_at: Mapped[datetime | None] = mapped_column(
DateTime(timezone=True), nullable=True
)
last_run_id: Mapped[uuid.UUID | None] = mapped_column(Uuid, nullable=True)
total_runs: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default="0")
successful_runs: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default="0")
failed_runs: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default="0")
total_credits_spent: Mapped[Decimal] = mapped_column(
Numeric(16, 4), nullable=False, default=Decimal("0"), server_default="0"
)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
owner: Mapped["User"] = relationship("User", lazy="selectin")
|