operaton_task_worker/
settings.rs

1use config::Config;
2
3/// Loads the configuration into a [ConfigParams] struct. The function may panic, but it should not
4/// happen because [ConfigParams] provides default values for all configured entries.
5pub fn load_config_from_env(env_prefix: &str) -> ConfigParams {
6    let settings = Config::builder()
7        .add_source(config::Environment::with_prefix(env_prefix))
8        .build()
9        .unwrap();
10
11    settings.try_deserialize().unwrap()
12}
13
14use serde::{Deserialize, Serialize};
15
16use url::Url;
17
18/// The struct contains all config params for running the task worker
19#[derive(Serialize, Deserialize, Clone, Debug)]
20pub struct ConfigParams {
21    /// The URL where operaton can be found
22    #[serde(default = "default_url")]
23    url: Url,
24
25    /// The username for authenticating with the REST API
26    /// - If empty, no authentication will be used (default).
27    #[serde(default = "String::new")]
28    username: String,
29
30    /// The password for authenticating with the REST API
31    /// - If empty, no authentication will be used (default).
32    #[serde(default = "String::new")]
33    password: String,
34
35    /// The interval in milliseconds for polling the Operaton Task Worker for new tasks
36    #[serde(default = "default_poll_interval")]
37    poll_interval: usize,
38
39    #[serde(default = "default_task_worker_id")]
40    /// The task worker id which will be registered with Operaton
41    id: String,
42
43    /// The lock duration in milliseconds for external task locking
44    #[serde(default = "default_lock_duration")]
45    lock_duration: u64,
46}
47
48impl ConfigParams {
49    pub fn url(&self) -> &Url {
50        &self.url
51    }
52
53    pub fn username(&self) -> &str {
54        &self.username
55    }
56
57    pub fn password(&self) -> &str {
58        &self.password
59    }
60
61    pub fn poll_interval(&self) -> usize {
62        self.poll_interval
63    }
64
65    pub fn id(&self) -> &str { &self.id }
66
67    pub fn lock_duration(&self) -> u64 { self.lock_duration }
68
69    pub fn with_url(self, url: Url) -> Self {
70        let mut cloned_self = self.clone();
71        cloned_self.url = url;
72        cloned_self
73    }
74
75    pub fn with_auth(self, username: String, password: String) -> Self {
76        let mut cloned_self = self.clone();
77        cloned_self.username = username;
78        cloned_self.password = password;
79        cloned_self
80    }
81
82    pub fn with_poll_interval(self, poll_interval: usize) -> Self {
83        let mut cloned_self = self.clone();
84        cloned_self.poll_interval = poll_interval;
85        cloned_self
86    }
87
88    pub fn with_worker_id(self, id: String) -> Self {
89        let mut cloned_self = self.clone();
90        cloned_self.id = id;
91        cloned_self
92    }
93
94    pub fn with_lock_duration(self, lock_duration: u64) -> Self {
95        let mut cloned_self = self.clone();
96        cloned_self.lock_duration = lock_duration;
97        cloned_self
98    }
99}
100
101impl Default for ConfigParams {
102    fn default() -> Self {
103        Self {
104            url: default_url(),
105            username: String::new(),
106            password: String::new(),
107            poll_interval: default_poll_interval(),
108            id: default_task_worker_id(),
109            lock_duration: default_lock_duration(),
110        }
111    }
112}
113
114fn default_url() -> Url {
115    Url::parse("http://localhost:8080").unwrap()
116}
117
118/// The default poll interval in milliseconds
119fn default_poll_interval() -> usize { 500 }
120
121fn default_task_worker_id() -> String { "operaton_task_worker".to_string() }
122
123fn default_lock_duration() -> u64 { 60_000 }
124
125#[cfg(test)]
126mod test {
127    use super::*;
128
129    #[test]
130    fn test_config_params_builder_pattern() {
131        let config = ConfigParams::default()
132            .with_url(Url::parse("http://localhost:8080").unwrap())
133            .with_auth("user".to_string(), "pass".to_string())
134            .with_poll_interval(1000)
135            .with_worker_id("operaton_task_worker".to_string())
136            .with_lock_duration(12_345);
137
138        assert_eq!(config.url(), &Url::parse("http://localhost:8080").unwrap());
139        assert_eq!(config.username(), "user");
140        assert_eq!(config.password(), "pass");
141        assert_eq!(config.poll_interval(), 1000);
142        assert_eq!(config.id(), "operaton_task_worker");
143        assert_eq!(config.lock_duration(), 12_345);
144    }
145
146    #[test]
147    fn test_default_lock_duration() {
148        let cfg = ConfigParams::default();
149        assert_eq!(cfg.lock_duration(), default_lock_duration());
150    }
151}