Skip to main content

actionqueue_engine/lease/
model.rs

1//! Lease data model for in-flight run ownership.
2//!
3//! This module defines typed lease primitives used to represent:
4//! - Which run is leased.
5//! - Which worker currently owns that lease.
6//! - When the lease expires.
7
8use actionqueue_core::ids::RunId;
9
10/// Typed identifier for the worker/executor that owns a lease.
11#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
12pub struct LeaseOwner(String);
13
14impl LeaseOwner {
15    /// Creates a lease owner from a worker identity string.
16    ///
17    /// In debug builds, panics if the value is empty.
18    pub fn new(owner: impl Into<String>) -> Self {
19        let value = owner.into();
20        assert!(!value.is_empty(), "LeaseOwner must not be empty");
21        Self(value)
22    }
23
24    /// Returns the worker identity as a string slice.
25    pub fn as_str(&self) -> &str {
26        &self.0
27    }
28}
29
30impl std::fmt::Display for LeaseOwner {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        write!(f, "{}", self.0)
33    }
34}
35
36impl From<String> for LeaseOwner {
37    fn from(value: String) -> Self {
38        Self::new(value)
39    }
40}
41
42impl From<&str> for LeaseOwner {
43    fn from(value: &str) -> Self {
44        Self::new(value.to_owned())
45    }
46}
47
48/// Typed lease-expiry timestamp represented in epoch seconds.
49#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
50pub struct LeaseExpiry {
51    /// Absolute timestamp when the lease is no longer valid.
52    expires_at: u64,
53}
54
55impl LeaseExpiry {
56    /// Creates a typed expiry value from an absolute timestamp.
57    pub const fn at(expires_at: u64) -> Self {
58        Self { expires_at }
59    }
60
61    /// Returns the absolute timestamp when the lease expires.
62    pub const fn expires_at(&self) -> u64 {
63        self.expires_at
64    }
65}
66
67/// Active lease for a specific run.
68#[derive(Debug, Clone, PartialEq, Eq)]
69pub struct Lease {
70    /// Run that this lease belongs to.
71    run_id: RunId,
72
73    /// Worker currently holding the lease.
74    owner: LeaseOwner,
75
76    /// Lease-expiry representation in typed form.
77    expiry: LeaseExpiry,
78}
79
80impl Lease {
81    /// Creates a new lease value.
82    pub fn new(run_id: RunId, owner: LeaseOwner, expiry: LeaseExpiry) -> Self {
83        Self { run_id, owner, expiry }
84    }
85
86    /// Returns the run that this lease belongs to.
87    pub fn run_id(&self) -> RunId {
88        self.run_id
89    }
90
91    /// Returns the worker currently holding the lease.
92    pub fn owner(&self) -> &LeaseOwner {
93        &self.owner
94    }
95
96    /// Returns the lease expiry.
97    pub fn expiry(&self) -> LeaseExpiry {
98        self.expiry
99    }
100}