Skip to main content

bunqueue_client/
options.rs

1use rmpv::Value;
2
3use crate::wire::{Map, map};
4
5#[derive(Clone, Debug)]
6pub enum Backoff {
7    Milliseconds(i64),
8    Strategy {
9        kind: String,
10        delay: i64,
11        max_delay: Option<i64>,
12    },
13}
14
15impl Backoff {
16    fn to_value(&self) -> Value {
17        match self {
18            Self::Milliseconds(delay) => Value::from(*delay),
19            Self::Strategy {
20                kind,
21                delay,
22                max_delay,
23            } => Value::Map(map([
24                ("type", Value::from(kind.clone())),
25                ("delay", Value::from(*delay)),
26                ("maxDelay", max_delay.map(Value::from).unwrap_or(Value::Nil)),
27            ])),
28        }
29    }
30}
31
32#[derive(Clone, Debug, Default)]
33pub struct Deduplication {
34    pub id: String,
35    pub ttl: Option<i64>,
36    pub extend: Option<bool>,
37    pub replace: Option<bool>,
38}
39
40/// Per-job options. Static fields make silently ignored options impossible.
41#[derive(Clone, Debug, Default)]
42pub struct JobOptions {
43    pub priority: Option<i64>,
44    pub delay: Option<i64>,
45    pub attempts: Option<i64>,
46    pub backoff: Option<Backoff>,
47    pub ttl: Option<i64>,
48    pub timeout: Option<i64>,
49    pub job_id: Option<String>,
50    pub unique_key: Option<String>,
51    pub deduplication: Option<Deduplication>,
52    pub depends_on: Vec<String>,
53    pub parent_id: Option<String>,
54    pub children_ids: Vec<String>,
55    pub tags: Vec<String>,
56    pub group_id: Option<String>,
57    pub lifo: Option<bool>,
58    pub remove_on_complete: Option<bool>,
59    pub remove_on_fail: Option<bool>,
60    pub stall_timeout: Option<i64>,
61    pub durable: Option<bool>,
62    pub repeat: Option<Value>,
63    pub debounce_id: Option<String>,
64    pub debounce_ttl: Option<i64>,
65    pub stack_trace_limit: Option<i64>,
66    pub keep_logs: Option<i64>,
67    pub size_limit: Option<i64>,
68    pub timestamp: Option<i64>,
69    pub fail_parent_on_failure: Option<bool>,
70    pub remove_dependency_on_failure: Option<bool>,
71    pub ignore_dependency_on_failure: Option<bool>,
72    pub continue_parent_on_failure: Option<bool>,
73}
74
75impl JobOptions {
76    pub(crate) fn to_wire(&self, bulk: bool) -> Map {
77        let dedup = self.deduplication.as_ref();
78        let unique_key = self
79            .unique_key
80            .clone()
81            .or_else(|| dedup.map(|value| value.id.clone()));
82        let dedup_value = dedup.map(|value| {
83            Value::Map(map([
84                ("ttl", value.ttl.map(Value::from).unwrap_or(Value::Nil)),
85                (
86                    "extend",
87                    value.extend.map(Value::from).unwrap_or(Value::Nil),
88                ),
89                (
90                    "replace",
91                    value.replace.map(Value::from).unwrap_or(Value::Nil),
92                ),
93            ]))
94        });
95        let custom_key = if bulk { "customId" } else { "jobId" };
96        map([
97            (
98                "priority",
99                self.priority.map(Value::from).unwrap_or(Value::Nil),
100            ),
101            ("delay", self.delay.map(Value::from).unwrap_or(Value::Nil)),
102            (
103                "maxAttempts",
104                self.attempts.map(Value::from).unwrap_or(Value::Nil),
105            ),
106            (
107                "backoff",
108                self.backoff
109                    .as_ref()
110                    .map(Backoff::to_value)
111                    .unwrap_or(Value::Nil),
112            ),
113            ("ttl", self.ttl.map(Value::from).unwrap_or(Value::Nil)),
114            (
115                "timeout",
116                self.timeout.map(Value::from).unwrap_or(Value::Nil),
117            ),
118            (
119                custom_key,
120                self.job_id.clone().map(Value::from).unwrap_or(Value::Nil),
121            ),
122            (
123                "uniqueKey",
124                unique_key.map(Value::from).unwrap_or(Value::Nil),
125            ),
126            ("dedup", dedup_value.unwrap_or(Value::Nil)),
127            ("dependsOn", string_array(&self.depends_on)),
128            (
129                "parentId",
130                self.parent_id
131                    .clone()
132                    .map(Value::from)
133                    .unwrap_or(Value::Nil),
134            ),
135            ("childrenIds", string_array(&self.children_ids)),
136            ("tags", string_array(&self.tags)),
137            (
138                "groupId",
139                self.group_id.clone().map(Value::from).unwrap_or(Value::Nil),
140            ),
141            ("lifo", self.lifo.map(Value::from).unwrap_or(Value::Nil)),
142            (
143                "removeOnComplete",
144                self.remove_on_complete
145                    .map(Value::from)
146                    .unwrap_or(Value::Nil),
147            ),
148            (
149                "removeOnFail",
150                self.remove_on_fail.map(Value::from).unwrap_or(Value::Nil),
151            ),
152            (
153                "stallTimeout",
154                self.stall_timeout.map(Value::from).unwrap_or(Value::Nil),
155            ),
156            (
157                "durable",
158                self.durable.map(Value::from).unwrap_or(Value::Nil),
159            ),
160            ("repeat", self.repeat.clone().unwrap_or(Value::Nil)),
161            (
162                "debounceId",
163                self.debounce_id
164                    .clone()
165                    .map(Value::from)
166                    .unwrap_or(Value::Nil),
167            ),
168            (
169                "debounceTtl",
170                self.debounce_ttl.map(Value::from).unwrap_or(Value::Nil),
171            ),
172            (
173                "stackTraceLimit",
174                self.stack_trace_limit
175                    .map(Value::from)
176                    .unwrap_or(Value::Nil),
177            ),
178            (
179                "keepLogs",
180                self.keep_logs.map(Value::from).unwrap_or(Value::Nil),
181            ),
182            (
183                "sizeLimit",
184                self.size_limit.map(Value::from).unwrap_or(Value::Nil),
185            ),
186            (
187                "timestamp",
188                self.timestamp.map(Value::from).unwrap_or(Value::Nil),
189            ),
190            (
191                "failParentOnFailure",
192                self.fail_parent_on_failure
193                    .map(Value::from)
194                    .unwrap_or(Value::Nil),
195            ),
196            (
197                "removeDependencyOnFailure",
198                self.remove_dependency_on_failure
199                    .map(Value::from)
200                    .unwrap_or(Value::Nil),
201            ),
202            (
203                "ignoreDependencyOnFailure",
204                self.ignore_dependency_on_failure
205                    .map(Value::from)
206                    .unwrap_or(Value::Nil),
207            ),
208            (
209                "continueParentOnFailure",
210                self.continue_parent_on_failure
211                    .map(Value::from)
212                    .unwrap_or(Value::Nil),
213            ),
214        ])
215    }
216
217    pub(crate) fn cron_options(&self) -> Value {
218        Value::Map(map([
219            (
220                "maxAttempts",
221                self.attempts.map(Value::from).unwrap_or(Value::Nil),
222            ),
223            (
224                "backoff",
225                self.backoff
226                    .as_ref()
227                    .map(Backoff::to_value)
228                    .unwrap_or(Value::Nil),
229            ),
230            (
231                "timeout",
232                self.timeout.map(Value::from).unwrap_or(Value::Nil),
233            ),
234            ("delay", self.delay.map(Value::from).unwrap_or(Value::Nil)),
235            (
236                "stallTimeout",
237                self.stall_timeout.map(Value::from).unwrap_or(Value::Nil),
238            ),
239            (
240                "removeOnComplete",
241                self.remove_on_complete
242                    .map(Value::from)
243                    .unwrap_or(Value::Nil),
244            ),
245            (
246                "removeOnFail",
247                self.remove_on_fail.map(Value::from).unwrap_or(Value::Nil),
248            ),
249        ]))
250    }
251}
252
253fn string_array(items: &[String]) -> Value {
254    if items.is_empty() {
255        Value::Nil
256    } else {
257        Value::Array(items.iter().cloned().map(Value::from).collect())
258    }
259}
260
261#[derive(Clone, Debug)]
262pub struct BulkEntry {
263    pub name: String,
264    pub data: Value,
265    pub options: JobOptions,
266}