Skip to main content

microsandbox_db/entity/
run.rs

1//! Entity definition for the `run` table.
2
3use sea_orm::entity::prelude::*;
4
5//--------------------------------------------------------------------------------------------------
6// Types
7//--------------------------------------------------------------------------------------------------
8
9/// The status of a sandbox run.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
11#[sea_orm(rs_type = "String", db_type = "Text")]
12pub enum RunStatus {
13    /// The sandbox is running.
14    #[sea_orm(string_value = "Running")]
15    Running,
16
17    /// The run has terminated.
18    #[sea_orm(string_value = "Terminated")]
19    Terminated,
20}
21
22/// The reason a sandbox run terminated.
23#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
24#[sea_orm(rs_type = "String", db_type = "Text")]
25pub enum TerminationReason {
26    /// Sandbox exited cleanly (exit code 0).
27    #[sea_orm(string_value = "Completed")]
28    Completed,
29
30    /// Sandbox exited with non-zero code or was killed by signal.
31    #[sea_orm(string_value = "Failed")]
32    Failed,
33
34    /// Sandbox exceeded `max_duration_secs`.
35    #[sea_orm(string_value = "MaxDurationExceeded")]
36    MaxDurationExceeded,
37
38    /// agentd reported no activity for `idle_timeout_secs`.
39    #[sea_orm(string_value = "IdleTimeout")]
40    IdleTimeout,
41
42    /// agentd stopped reporting fresh heartbeat data.
43    #[sea_orm(string_value = "AgentUnresponsive")]
44    AgentUnresponsive,
45
46    /// User or client requested shutdown explicitly.
47    #[sea_orm(string_value = "ShutdownRequested")]
48    ShutdownRequested,
49
50    /// SIGUSR1 received (explicit drain request).
51    #[sea_orm(string_value = "DrainRequested")]
52    DrainRequested,
53
54    /// SIGTERM/SIGINT received from external source.
55    #[sea_orm(string_value = "Signal")]
56    Signal,
57
58    /// Internal error.
59    #[sea_orm(string_value = "InternalError")]
60    InternalError,
61}
62
63/// A single run of a sandbox.
64#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
65#[sea_orm(table_name = "run")]
66pub struct Model {
67    #[sea_orm(primary_key)]
68    pub id: i32,
69    pub sandbox_id: i32,
70    pub pid: Option<i32>,
71    pub status: RunStatus,
72    pub exit_code: Option<i32>,
73    pub exit_signal: Option<i32>,
74    pub termination_reason: Option<TerminationReason>,
75    pub termination_detail: Option<String>,
76    pub signals_sent: Option<String>,
77    pub started_at: Option<DateTime>,
78    pub terminated_at: Option<DateTime>,
79}
80
81//--------------------------------------------------------------------------------------------------
82// Types: Relations
83//--------------------------------------------------------------------------------------------------
84
85/// Relations for the run entity.
86#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
87pub enum Relation {
88    /// A run belongs to a sandbox.
89    #[sea_orm(
90        belongs_to = "super::sandbox::Entity",
91        from = "Column::SandboxId",
92        to = "super::sandbox::Column::Id",
93        on_delete = "Cascade"
94    )]
95    Sandbox,
96}
97
98impl Related<super::sandbox::Entity> for Entity {
99    fn to() -> RelationDef {
100        Relation::Sandbox.def()
101    }
102}
103
104//--------------------------------------------------------------------------------------------------
105// Trait Implementations
106//--------------------------------------------------------------------------------------------------
107
108impl std::fmt::Display for TerminationReason {
109    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
110        match self {
111            Self::Completed => f.write_str("Completed"),
112            Self::Failed => f.write_str("Failed"),
113            Self::MaxDurationExceeded => f.write_str("MaxDurationExceeded"),
114            Self::IdleTimeout => f.write_str("IdleTimeout"),
115            Self::AgentUnresponsive => f.write_str("AgentUnresponsive"),
116            Self::ShutdownRequested => f.write_str("ShutdownRequested"),
117            Self::DrainRequested => f.write_str("DrainRequested"),
118            Self::Signal => f.write_str("Signal"),
119            Self::InternalError => f.write_str("InternalError"),
120        }
121    }
122}
123
124impl ActiveModelBehavior for ActiveModel {}