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/// Primary-key value of the install-exclusive lease row.
19pub const INSTALL_EXCLUSIVE: &str = "install_exclusive";
20
21//--------------------------------------------------------------------------------------------------
22// Types
23//--------------------------------------------------------------------------------------------------
24
25/// The maintenance-lease entity model.
26#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
27#[sea_orm(table_name = "maintenance_lease")]
28pub struct Model {
29 /// Lease name. Acts as the primary key; there is one row per kind of
30 /// maintenance work (for example [`SANDBOX_LIFECYCLE_MAINTENANCE`] or
31 /// [`INSTALL_EXCLUSIVE`]).
32 #[sea_orm(primary_key, auto_increment = false)]
33 pub name: String,
34
35 /// PID of the runtime that currently holds the lease, for diagnostics.
36 pub holder_pid: Option<i32>,
37
38 /// When the current lease expires. A runtime may claim the lease once
39 /// this time has passed, even if the prior holder never released it.
40 pub lease_expires_at: DateTime,
41
42 /// When maintenance last completed successfully. Used to read-gate the
43 /// lease so runtimes skip the write entirely between sweep windows.
44 pub last_completed_at: Option<DateTime>,
45}
46
47//--------------------------------------------------------------------------------------------------
48// Types: Relations
49//--------------------------------------------------------------------------------------------------
50
51/// Relations for the maintenance-lease entity (none).
52#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
53pub enum Relation {}
54
55//--------------------------------------------------------------------------------------------------
56// Trait Implementations
57//--------------------------------------------------------------------------------------------------
58
59impl ActiveModelBehavior for ActiveModel {}