Skip to main content

oris_execution_runtime/
repository.rs

1//! Storage façade for runtime scheduler/lease operations.
2
3use chrono::{DateTime, Utc};
4
5use oris_kernel::event::KernelError;
6use oris_kernel::identity::{RunId, Seq};
7
8use super::models::{AttemptDispatchRecord, LeaseRecord};
9
10/// Runtime repository contract used by scheduler and lease manager.
11///
12/// Implementations are responsible for making dispatch ownership transitions
13/// explicit:
14/// - `list_dispatchable_attempts` must preserve the repository's dispatch order.
15/// - `upsert_lease` must atomically claim only dispatchable attempts and move
16///   the attempt into leased ownership when the claim succeeds.
17/// - `expire_leases_and_requeue` must reclaim only leases whose expiry is older
18///   than the supplied stale cutoff, so callers can apply a heartbeat grace
19///   window before requeueing work.
20pub trait RuntimeRepository: Send + Sync {
21    /// Return attempts eligible for dispatch at the current time.
22    fn list_dispatchable_attempts(
23        &self,
24        now: DateTime<Utc>,
25        limit: usize,
26    ) -> Result<Vec<AttemptDispatchRecord>, KernelError>;
27
28    /// Create or replace a lease for an attempt.
29    fn upsert_lease(
30        &self,
31        attempt_id: &str,
32        worker_id: &str,
33        lease_expires_at: DateTime<Utc>,
34    ) -> Result<LeaseRecord, KernelError>;
35
36    /// Refresh heartbeat for an existing lease.
37    fn heartbeat_lease(
38        &self,
39        lease_id: &str,
40        heartbeat_at: DateTime<Utc>,
41        lease_expires_at: DateTime<Utc>,
42    ) -> Result<(), KernelError>;
43
44    /// Expire stale leases and requeue affected attempts.
45    fn expire_leases_and_requeue(&self, stale_before: DateTime<Utc>) -> Result<u64, KernelError>;
46
47    /// Transition attempts that exceeded their configured execution timeout.
48    fn transition_timed_out_attempts(&self, _now: DateTime<Utc>) -> Result<u64, KernelError> {
49        Ok(0)
50    }
51
52    /// Returns latest persisted sequence for a run (used by replay wiring).
53    fn latest_seq_for_run(&self, run_id: &RunId) -> Result<Seq, KernelError>;
54}