32 lines
1.4 KiB
Python
32 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
from sqlalchemy import DateTime, ForeignKey, String, Text, JSON, text
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
from sqlalchemy.sql import func
|
|
|
|
from .database import Base
|
|
|
|
|
|
class Task(Base):
|
|
__tablename__ = "tasks"
|
|
|
|
id: Mapped[str] = mapped_column(String, primary_key=True)
|
|
action: Mapped[str] = mapped_column(String, nullable=False)
|
|
target: Mapped[str] = mapped_column(String, nullable=False)
|
|
status: Mapped[str] = mapped_column(String, nullable=False, server_default=text("'pending'"))
|
|
playbook: Mapped[Optional[str]] = mapped_column(String)
|
|
started_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True))
|
|
completed_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True))
|
|
error_message: Mapped[Optional[str]] = mapped_column(Text)
|
|
result_data: Mapped[Optional[dict]] = mapped_column(JSON)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
|
|
|
|
schedule_runs: Mapped[list["ScheduleRun"]] = relationship("ScheduleRun", back_populates="task")
|
|
logs: Mapped[list["Log"]] = relationship("Log", back_populates="task")
|
|
|
|
def __repr__(self) -> str: # pragma: no cover - debug helper
|
|
return f"<Task id={self.id} action={self.action} target={self.target} status={self.status}>"
|