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    /// SIGUSR1 received (explicit drain request).
43    #[sea_orm(string_value = "DrainRequested")]
44    DrainRequested,
45
46    /// SIGTERM/SIGINT received from external source.
47    #[sea_orm(string_value = "Signal")]
48    Signal,
49
50    /// Internal error.
51    #[sea_orm(string_value = "InternalError")]
52    InternalError,
53}
54
55/// A single run of a sandbox.
56#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
57#[sea_orm(table_name = "run")]
58pub struct Model {
59    #[sea_orm(primary_key)]
60    pub id: i32,
61    pub sandbox_id: i32,
62    pub pid: Option<i32>,
63    pub status: RunStatus,
64    pub exit_code: Option<i32>,
65    pub exit_signal: Option<i32>,
66    pub termination_reason: Option<TerminationReason>,
67    pub termination_detail: Option<String>,
68    pub signals_sent: Option<String>,
69    pub started_at: Option<DateTime>,
70    pub terminated_at: Option<DateTime>,
71}
72
73//--------------------------------------------------------------------------------------------------
74// Types: Relations
75//--------------------------------------------------------------------------------------------------
76
77/// Relations for the run entity.
78#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
79pub enum Relation {
80    /// A run belongs to a sandbox.
81    #[sea_orm(
82        belongs_to = "super::sandbox::Entity",
83        from = "Column::SandboxId",
84        to = "super::sandbox::Column::Id",
85        on_delete = "Cascade"
86    )]
87    Sandbox,
88}
89
90impl Related<super::sandbox::Entity> for Entity {
91    fn to() -> RelationDef {
92        Relation::Sandbox.def()
93    }
94}
95
96//--------------------------------------------------------------------------------------------------
97// Trait Implementations
98//--------------------------------------------------------------------------------------------------
99
100impl std::fmt::Display for TerminationReason {
101    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102        match self {
103            Self::Completed => f.write_str("Completed"),
104            Self::Failed => f.write_str("Failed"),
105            Self::MaxDurationExceeded => f.write_str("MaxDurationExceeded"),
106            Self::IdleTimeout => f.write_str("IdleTimeout"),
107            Self::DrainRequested => f.write_str("DrainRequested"),
108            Self::Signal => f.write_str("Signal"),
109            Self::InternalError => f.write_str("InternalError"),
110        }
111    }
112}
113
114impl ActiveModelBehavior for ActiveModel {}