Skip to main content

bunqueue_client/
queue.rs

1use rmpv::Value;
2
3use crate::wire::{Map, command, get, map};
4use crate::{BulkEntry, Connection, ConnectionOptions, Job, JobOptions, Result};
5
6#[derive(Clone)]
7pub struct Queue {
8    pub name: String,
9    pub(crate) connection: Connection,
10    owns_connection: bool,
11}
12
13impl Queue {
14    pub fn new(name: impl Into<String>, options: ConnectionOptions) -> Self {
15        Self {
16            name: name.into(),
17            connection: Connection::new(options),
18            owns_connection: true,
19        }
20    }
21
22    pub fn with_connection(name: impl Into<String>, connection: Connection) -> Self {
23        Self {
24            name: name.into(),
25            connection,
26            owns_connection: false,
27        }
28    }
29
30    pub fn add(&self, name: impl Into<String>, data: Value, options: JobOptions) -> Result<Job> {
31        let name = name.into();
32        let payload = job_payload(&name, data);
33        let mut fields = map([
34            ("queue", Value::from(self.name.clone())),
35            ("data", payload.clone()),
36        ]);
37        fields.extend(options.to_wire(false));
38        let response = self.call(command("PUSH", fields))?;
39        let raw = map([
40            ("id", get(&response, "id").cloned().unwrap_or(Value::Nil)),
41            ("queue", Value::from(self.name.clone())),
42            ("data", payload),
43        ]);
44        Ok(Job::new(raw, self.connection.clone(), None))
45    }
46
47    pub fn add_bulk(&self, entries: Vec<BulkEntry>) -> Result<Vec<String>> {
48        let jobs = entries
49            .into_iter()
50            .map(|entry| {
51                let mut fields = map([("data", job_payload(&entry.name, entry.data))]);
52                fields.extend(entry.options.to_wire(true));
53                Value::Map(fields)
54            })
55            .collect();
56        let response = self.call(command(
57            "PUSHB",
58            map([
59                ("queue", Value::from(self.name.clone())),
60                ("jobs", Value::Array(jobs)),
61            ]),
62        ))?;
63        Ok(get(&response, "ids")
64            .and_then(Value::as_array)
65            .map(|ids| {
66                ids.iter()
67                    .filter_map(crate::wire::id_string)
68                    .collect::<Vec<_>>()
69            })
70            .unwrap_or_default())
71    }
72
73    pub fn close(&self) {
74        if self.owns_connection {
75            self.connection.close();
76        }
77    }
78
79    pub(crate) fn call(&self, command: Map) -> Result<Map> {
80        self.connection.call(command)
81    }
82}
83
84pub(crate) fn job_payload(name: &str, data: Value) -> Value {
85    match data {
86        Value::Map(mut entries) => {
87            let mut out = vec![(Value::from("name"), Value::from(name))];
88            out.append(&mut entries);
89            Value::Map(out)
90        }
91        Value::Nil => Value::Map(map([("name", Value::from(name))])),
92        other => Value::Map(map([("name", Value::from(name)), ("payload", other)])),
93    }
94}