notification_live_activity.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. """Durable ownership of Notify! tiles, including per-print dismissal tombstones."""
  2. from datetime import datetime
  3. from sqlalchemy import DateTime, ForeignKey, Integer, String, Text, UniqueConstraint
  4. from sqlalchemy.orm import Mapped, mapped_column
  5. from backend.app.core.database import Base
  6. class NotificationLiveActivity(Base):
  7. __tablename__ = "notification_live_activities"
  8. __table_args__ = (UniqueConstraint("provider_id", "printer_id", "print_key"),)
  9. id: Mapped[int] = mapped_column(Integer, primary_key=True)
  10. provider_id: Mapped[int] = mapped_column(
  11. Integer, ForeignKey("notification_providers.id", ondelete="CASCADE"), nullable=False, index=True
  12. )
  13. # Keep ownership after printer deletion until the worker ends the remote tile.
  14. printer_id: Mapped[int] = mapped_column(Integer, nullable=False, index=True)
  15. print_key: Mapped[str] = mapped_column(String(200), nullable=False)
  16. job_key: Mapped[str | None] = mapped_column(String(160), nullable=True)
  17. print_name: Mapped[str] = mapped_column(String(255), nullable=False, default="")
  18. eta_seconds: Mapped[int | None] = mapped_column(Integer, nullable=True)
  19. eta_deadline: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  20. credential_key: Mapped[str] = mapped_column(String(64), nullable=False)
  21. activity_id: Mapped[str | None] = mapped_column(String(16), nullable=True)
  22. state: Mapped[str] = mapped_column(String(20), nullable=False, default="pending")
  23. end_reason: Mapped[str | None] = mapped_column(String(40), nullable=True)
  24. failures: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
  25. content: Mapped[str] = mapped_column(Text, nullable=False, default="{}")
  26. expires_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  27. next_attempt_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  28. last_sent_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
  29. created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow)