Skip to main content

ralph_workflow/agents/
runtime.rs

1// Runtime operations: sleep, timers, etc.
2
3use std::sync::Arc;
4use std::time::Duration;
5
6/// Provider for sleep operations in retry logic.
7///
8/// This trait allows different sleep implementations:
9/// - Production: Real `std::thread::sleep` with actual delays
10/// - Testing: Immediate (no-op) sleeps for fast test execution
11pub trait RetryTimerProvider: Send + Sync {
12    fn sleep(&self, duration: Duration);
13}
14
15pub trait RetryTimerProviderDebug: RetryTimerProvider + std::fmt::Debug {}
16impl<T: RetryTimerProvider + std::fmt::Debug> RetryTimerProviderDebug for T {}
17
18/// Production retry timer that actually sleeps.
19#[derive(Debug, Clone)]
20pub struct ProductionRetryTimer;
21
22impl RetryTimerProvider for ProductionRetryTimer {
23    fn sleep(&self, duration: Duration) {
24        std::thread::sleep(duration);
25    }
26}
27
28/// Create a new production retry timer.
29pub fn production_timer() -> Arc<dyn RetryTimerProviderDebug> {
30    Arc::new(ProductionRetryTimer)
31}
32
33/// Perform a blocking sleep for use by timer implementations outside boundary modules.
34pub fn do_sleep(duration: Duration) {
35    std::thread::sleep(duration);
36}
37
38/// Read an environment variable for use by non-boundary callers.
39pub fn get_env_var(key: &str) -> Option<String> {
40    std::env::var(key).ok()
41}