microsandbox_db/entity/maintenance_lease.rs
1//! Entity definition for the `maintenance_lease` table.
2//!
3//! A single-row coordination primitive used by host-runtime sandbox
4//! lifecycle maintenance. Each `msb sandbox` process performs a cheap
5//! read-gated lease attempt on startup; only the runtime that wins the
6//! lease runs the bounded maintenance sweep, so a burst of sandbox starts
7//! does not turn into N concurrent full scans.
8
9use sea_orm::entity::prelude::*;
10
11//--------------------------------------------------------------------------------------------------
12// Constants
13//--------------------------------------------------------------------------------------------------
14
15/// Primary-key value of the single lifecycle-maintenance lease row.
16pub const SANDBOX_LIFECYCLE_MAINTENANCE: &str = "sandbox_lifecycle_maintenance";
17
18//--------------------------------------------------------------------------------------------------
19// Types
20//--------------------------------------------------------------------------------------------------
21
22/// The maintenance-lease entity model.
23#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
24#[sea_orm(table_name = "maintenance_lease")]
25pub struct Model {
26 /// Lease name. Acts as the primary key; there is one row per kind of
27 /// maintenance work (today only [`SANDBOX_LIFECYCLE_MAINTENANCE`]).
28 #[sea_orm(primary_key, auto_increment = false)]
29 pub name: String,
30
31 /// PID of the runtime that currently holds the lease, for diagnostics.
32 pub holder_pid: Option<i32>,
33
34 /// When the current lease expires. A runtime may claim the lease once
35 /// this time has passed, even if the prior holder never released it.
36 pub lease_expires_at: DateTime,
37
38 /// When maintenance last completed successfully. Used to read-gate the
39 /// lease so runtimes skip the write entirely between sweep windows.
40 pub last_completed_at: Option<DateTime>,
41}
42
43//--------------------------------------------------------------------------------------------------
44// Types: Relations
45//--------------------------------------------------------------------------------------------------
46
47/// Relations for the maintenance-lease entity (none).
48#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
49pub enum Relation {}
50
51//--------------------------------------------------------------------------------------------------
52// Trait Implementations
53//--------------------------------------------------------------------------------------------------
54
55impl ActiveModelBehavior for ActiveModel {}