scheduled_drying.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from datetime import datetime
  2. from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, String, Text, func
  3. from sqlalchemy.orm import Mapped, mapped_column, relationship
  4. from backend.app.core.database import Base
  5. class ScheduledDrying(Base):
  6. """A manual AMS drying run scheduled to start later (#2638).
  7. Dispatched by PrintScheduler._check_scheduled_dryings() when start_after
  8. has passed and the printer is idle. Parameters mirror the immediate
  9. POST /printers/{id}/drying/start endpoint.
  10. """
  11. __tablename__ = "scheduled_dryings"
  12. id: Mapped[int] = mapped_column(primary_key=True)
  13. printer_id: Mapped[int] = mapped_column(ForeignKey("printers.id", ondelete="CASCADE"))
  14. ams_id: Mapped[int] = mapped_column(Integer, default=0)
  15. temp: Mapped[int] = mapped_column(Integer)
  16. duration_hours: Mapped[int] = mapped_column(Integer)
  17. filament: Mapped[str] = mapped_column(String(50), default="")
  18. rotate_tray: Mapped[bool] = mapped_column(Boolean, default=False)
  19. # Earliest start instant, naive UTC (same convention as
  20. # print_queue.scheduled_time). None = start as soon as the printer is idle.
  21. start_after: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  22. # pending / running / completed / cancelled / failed
  23. status: Mapped[str] = mapped_column(String(20), default="pending")
  24. waiting_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
  25. error_message: Mapped[str | None] = mapped_column(Text, nullable=True)
  26. # Why a failed run failed, as a code the UI translates (screen_only /
  27. # unsupported / did_not_start). error_message keeps the English text for the
  28. # API and logs; a row without a code falls back to showing that.
  29. error_code: Mapped[str | None] = mapped_column(String(32), nullable=True)
  30. created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
  31. started_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  32. completed_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  33. created_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
  34. printer: Mapped["Printer"] = relationship()
  35. created_by: Mapped["User | None"] = relationship()
  36. from backend.app.models.printer import Printer # noqa: E402
  37. from backend.app.models.user import User # noqa: E402