1use sea_orm::entity::prelude::*;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, EnumIter, DeriveActiveEnum)]
7#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "task_status")]
8pub enum TaskStatus {
9 #[sea_orm(string_value = "PENDING")]
10 Pending,
11 #[sea_orm(string_value = "RUNNING")]
12 Running,
13 #[sea_orm(string_value = "COMPLETED")]
14 Completed,
15 #[sea_orm(string_value = "FAILED")]
16 Failed,
17 #[sea_orm(string_value = "PAUSED")]
18 Paused,
19 #[sea_orm(string_value = "CANCELLED")]
20 Cancelled,
21}
22
23impl std::fmt::Display for TaskStatus {
24 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25 match self {
26 TaskStatus::Pending => write!(f, "PENDING"),
27 TaskStatus::Running => write!(f, "RUNNING"),
28 TaskStatus::Completed => write!(f, "COMPLETED"),
29 TaskStatus::Failed => write!(f, "FAILED"),
30 TaskStatus::Paused => write!(f, "PAUSED"),
31 TaskStatus::Cancelled => write!(f, "CANCELLED"),
32 }
33 }
34}
35
36impl std::str::FromStr for TaskStatus {
37 type Err = String;
38
39 fn from_str(s: &str) -> Result<Self, Self::Err> {
40 match s {
41 "PENDING" => Ok(TaskStatus::Pending),
42 "RUNNING" => Ok(TaskStatus::Running),
43 "COMPLETED" => Ok(TaskStatus::Completed),
44 "FAILED" => Ok(TaskStatus::Failed),
45 "PAUSED" => Ok(TaskStatus::Paused),
46 "CANCELLED" => Ok(TaskStatus::Cancelled),
47 _ => Err(format!("unknown task status: {s}")),
48 }
49 }
50}
51
52#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
53#[sea_orm(schema_name = "durable", table_name = "task")]
54pub struct Model {
55 #[sea_orm(primary_key, auto_increment = false)]
56 pub id: Uuid,
57 pub parent_id: Option<Uuid>,
58 pub sequence: Option<i32>,
59 #[sea_orm(column_type = "Text")]
60 pub name: String,
61 pub status: TaskStatus,
62 pub kind: String,
63 #[sea_orm(column_type = "JsonBinary", nullable)]
64 pub input: Option<Json>,
65 #[sea_orm(column_type = "JsonBinary", nullable)]
66 pub output: Option<Json>,
67 #[sea_orm(column_type = "Text", nullable)]
68 pub error: Option<String>,
69 pub max_retries: i32,
70 pub retry_count: i32,
71 #[sea_orm(column_type = "Text", nullable)]
72 pub cron: Option<String>,
73 pub next_run_at: Option<DateTimeWithTimeZone>,
74 #[sea_orm(column_type = "Text", nullable)]
75 pub queue_name: Option<String>,
76 #[sea_orm(column_type = "Text", nullable)]
77 pub executor_id: Option<String>,
78 #[sea_orm(column_type = "Text", nullable)]
79 pub app_version: Option<String>,
80 pub timeout_ms: Option<i64>,
81 pub deadline_epoch_ms: Option<i64>,
82 pub created_at: DateTimeWithTimeZone,
83 pub started_at: Option<DateTimeWithTimeZone>,
84 pub completed_at: Option<DateTimeWithTimeZone>,
85}
86
87#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
88pub enum Relation {
89 #[sea_orm(
90 belongs_to = "Entity",
91 from = "Column::ParentId",
92 to = "Column::Id",
93 on_update = "NoAction",
94 on_delete = "Cascade"
95 )]
96 SelfRef,
97 #[sea_orm(
98 belongs_to = "super::task_queue::Entity",
99 from = "Column::QueueName",
100 to = "super::task_queue::Column::Name",
101 on_update = "NoAction",
102 on_delete = "NoAction"
103 )]
104 TaskQueue,
105}
106
107impl Related<super::task_queue::Entity> for Entity {
108 fn to() -> RelationDef {
109 Relation::TaskQueue.def()
110 }
111}
112
113impl ActiveModelBehavior for ActiveModel {}