apalis_sql/
config.rs

1use std::time::Duration;
2
3use apalis_core::backend::{
4    poll_strategy::{BackoffConfig, IntervalStrategy, MultiStrategy, StrategyBuilder},
5    queue::Queue,
6};
7
8/// Configuration for the SQL backend
9#[derive(Debug, Clone)]
10pub struct Config {
11    keep_alive: Duration,
12    buffer_size: usize,
13    poll_strategy: MultiStrategy,
14    reenqueue_orphaned_after: Duration,
15    queue: Queue,
16    ack: bool,
17}
18
19impl Default for Config {
20    fn default() -> Self {
21        Self {
22            keep_alive: Duration::from_secs(30),
23            buffer_size: 10,
24            poll_strategy: StrategyBuilder::new()
25                .apply(
26                    IntervalStrategy::new(Duration::from_millis(100))
27                        .with_backoff(BackoffConfig::default()),
28                )
29                .build(),
30            reenqueue_orphaned_after: Duration::from_secs(300), // 5 minutes
31            queue: Queue::from("default"),
32            ack: true,
33        }
34    }
35}
36
37impl Config {
38    /// Create a new config with a jobs queue
39    #[must_use]
40    pub fn new(queue: &str) -> Self {
41        Self {
42            queue: Queue::from(queue),
43            ..Default::default()
44        }
45    }
46
47    /// Interval between database poll queries
48    ///
49    /// Defaults to 100ms
50    #[must_use]
51    pub fn with_poll_interval(mut self, strategy: MultiStrategy) -> Self {
52        self.poll_strategy = strategy;
53        self
54    }
55
56    /// Interval between worker keep-alive database updates
57    ///
58    /// Defaults to 30s
59    #[must_use]
60    pub fn set_keep_alive(mut self, keep_alive: Duration) -> Self {
61        self.keep_alive = keep_alive;
62        self
63    }
64
65    /// Buffer size to use when querying for jobs
66    ///
67    /// Defaults to 10
68    #[must_use]
69    pub fn set_buffer_size(mut self, buffer_size: usize) -> Self {
70        self.buffer_size = buffer_size;
71        self
72    }
73
74    /// Gets a reference to the keep_alive duration.
75    #[must_use]
76    pub fn keep_alive(&self) -> &Duration {
77        &self.keep_alive
78    }
79
80    /// Gets a mutable reference to the keep_alive duration.
81    pub fn keep_alive_mut(&mut self) -> &mut Duration {
82        &mut self.keep_alive
83    }
84
85    /// Gets the buffer size.
86    #[must_use]
87    pub fn buffer_size(&self) -> usize {
88        self.buffer_size
89    }
90
91    /// Gets a reference to the poll_strategy.
92    #[must_use]
93    pub fn poll_strategy(&self) -> &MultiStrategy {
94        &self.poll_strategy
95    }
96
97    /// Gets a mutable reference to the poll_strategy.
98    pub fn poll_strategy_mut(&mut self) -> &mut MultiStrategy {
99        &mut self.poll_strategy
100    }
101
102    /// Gets a reference to the queue.
103    #[must_use]
104    pub fn queue(&self) -> &Queue {
105        &self.queue
106    }
107
108    /// Gets a mutable reference to the queue.
109    pub fn queue_mut(&mut self) -> &mut Queue {
110        &mut self.queue
111    }
112
113    /// Gets the reenqueue_orphaned_after duration.
114    #[must_use]
115    pub fn reenqueue_orphaned_after(&self) -> Duration {
116        self.reenqueue_orphaned_after
117    }
118
119    /// Gets a mutable reference to the reenqueue_orphaned_after.
120    pub fn reenqueue_orphaned_after_mut(&mut self) -> &mut Duration {
121        &mut self.reenqueue_orphaned_after
122    }
123
124    /// Occasionally some workers die, or abandon jobs because of panics.
125    /// This is the time a task takes before its back to the queue
126    ///
127    /// Defaults to 5 minutes
128    #[must_use]
129    pub fn set_reenqueue_orphaned_after(mut self, after: Duration) -> Self {
130        self.reenqueue_orphaned_after = after;
131        self
132    }
133
134    /// Whether to automatically acknowledge jobs after processing
135    /// Defaults to true
136    #[must_use]
137    pub fn ack(&self) -> bool {
138        self.ack
139    }
140
141    /// Set whether to automatically acknowledge jobs after processing
142    #[must_use]
143    pub fn set_ack(mut self, auto_ack: bool) -> Self {
144        self.ack = auto_ack;
145        self
146    }
147}