Skip to main content

agent_queue/
config.rs

1use std::path::PathBuf;
2use std::time::Duration;
3
4/// Configuration for the queue system.
5///
6/// Use [`QueueConfig::builder()`] for ergonomic construction, or
7/// [`QueueConfig::default()`] for sensible defaults (in-memory DB, no cooldown).
8#[derive(Debug, Clone)]
9pub struct QueueConfig {
10    /// Path to SQLite database file. `None` = in-memory database.
11    pub db_path: Option<PathBuf>,
12
13    /// Stable worker identifier used for leases, heartbeats, and diagnostics.
14    pub worker_id: String,
15
16    /// Cooldown duration between job executions (0 = no cooldown).
17    pub cooldown: Duration,
18
19    /// Maximum consecutive jobs before a forced cooldown (0 = unlimited).
20    pub max_consecutive: u32,
21
22    /// Polling interval for checking pending jobs.
23    pub poll_interval: Duration,
24
25    /// Interval between lease heartbeats while a job is running.
26    pub heartbeat_interval: Duration,
27
28    /// Visibility timeout / stale lease threshold for reclaiming jobs.
29    pub stale_after: Duration,
30
31    /// Maximum retry attempts for retryable failures.
32    pub max_retries: u32,
33}
34
35impl Default for QueueConfig {
36    fn default() -> Self {
37        Self {
38            db_path: None,
39            worker_id: format!("worker-{}", uuid::Uuid::new_v4()),
40            cooldown: Duration::from_secs(0),
41            max_consecutive: 0,
42            poll_interval: Duration::from_secs(3),
43            heartbeat_interval: Duration::from_secs(10),
44            stale_after: Duration::from_secs(300),
45            max_retries: 3,
46        }
47    }
48}
49
50impl QueueConfig {
51    /// Start building a config with the builder pattern.
52    pub fn builder() -> QueueConfigBuilder {
53        QueueConfigBuilder::default()
54    }
55}
56
57/// Builder for [`QueueConfig`].
58#[derive(Default)]
59pub struct QueueConfigBuilder {
60    config: QueueConfig,
61}
62
63impl QueueConfigBuilder {
64    /// Set the SQLite database path for persistence. Omit for in-memory.
65    pub fn with_db_path(mut self, path: PathBuf) -> Self {
66        self.config.db_path = Some(path);
67        self
68    }
69
70    /// Set the worker identifier used for job leases and heartbeats.
71    pub fn with_worker_id(mut self, worker_id: impl Into<String>) -> Self {
72        self.config.worker_id = worker_id.into();
73        self
74    }
75
76    /// Set the cooldown duration between consecutive job executions.
77    pub fn with_cooldown(mut self, duration: Duration) -> Self {
78        self.config.cooldown = duration;
79        self
80    }
81
82    /// Set the maximum consecutive jobs before a forced cooldown.
83    pub fn with_max_consecutive(mut self, max: u32) -> Self {
84        self.config.max_consecutive = max;
85        self
86    }
87
88    /// Set the polling interval for checking pending jobs.
89    pub fn with_poll_interval(mut self, interval: Duration) -> Self {
90        self.config.poll_interval = interval;
91        self
92    }
93
94    /// Set the heartbeat interval for running jobs.
95    pub fn with_heartbeat_interval(mut self, interval: Duration) -> Self {
96        self.config.heartbeat_interval = interval;
97        self
98    }
99
100    /// Set the stale lease threshold for reclaiming abandoned jobs.
101    pub fn with_stale_after(mut self, duration: Duration) -> Self {
102        self.config.stale_after = duration;
103        self
104    }
105
106    /// Set the maximum retry attempts for retryable failures.
107    pub fn with_max_retries(mut self, max_retries: u32) -> Self {
108        self.config.max_retries = max_retries;
109        self
110    }
111
112    /// Build the final [`QueueConfig`].
113    pub fn build(self) -> QueueConfig {
114        self.config
115    }
116}