Skip to main content

Worker

Struct Worker 

Source
pub struct Worker { /* private fields */ }

Implementations§

Source§

impl Worker

Source

pub fn new( queue: impl Into<String>, processor: impl Fn(Job) -> Result<Value, ProcessError> + Send + Sync + 'static, options: WorkerOptions, ) -> Self

Examples found in repository?
examples/conformance_driver.rs (lines 167-176)
148    fn process_until(&mut self, request: &Json) -> bunqueue_client::Result<()> {
149        let queue_name = text(request, "queue").to_owned();
150        let behavior = text(request, "behavior").to_owned();
151        let result = json_to_value(request["result"].clone());
152        let failed_once = Arc::new(Mutex::new(HashSet::new()));
153        let seen = failed_once.clone();
154        let processor = move |job: Job| match behavior.as_str() {
155            "unrecoverable" => Err(ProcessError::unrecoverable("conformance poison")),
156            "deepThrow" => deep_error(25),
157            "failOnce" => {
158                let mut seen = seen.lock().expect("seen mutex poisoned");
159                if seen.insert(job.id()) {
160                    Err(ProcessError::retryable("conformance transient"))
161                } else {
162                    Ok(result.clone())
163                }
164            }
165            _ => Ok(result.clone()),
166        };
167        let worker = Worker::new(
168            queue_name.clone(),
169            processor,
170            WorkerOptions {
171                connection: self.options.clone(),
172                batch_size: number(request, "batchSize", 10) as usize,
173                poll_timeout_ms: 300,
174                ..Default::default()
175            },
176        );
177        let runner = worker.clone();
178        let handle = thread::spawn(move || runner.run());
179        let deadline =
180            Instant::now() + Duration::from_millis(number(request, "timeoutMs", 20_000) as u64);
181        let until = &request["until"];
182        while Instant::now() < deadline {
183            let counts = self.queue(&queue_name).get_job_counts().unwrap_or_default();
184            let completed = count(&counts, "completed") >= until["completed"].as_i64().unwrap_or(0);
185            let failed = count(&counts, "failed") >= until["failed"].as_i64().unwrap_or(0);
186            let dlq = self
187                .queue(&queue_name)
188                .get_dlq(None)
189                .map(|v| v.len())
190                .unwrap_or(0)
191                >= until["dlq"].as_u64().unwrap_or(0) as usize;
192            if completed && failed && dlq {
193                worker.stop();
194                let _ = handle.join();
195                return Ok(());
196            }
197            thread::sleep(Duration::from_millis(100));
198        }
199        worker.stop();
200        let _ = handle.join();
201        Err(bunqueue_client::Error::Timeout(
202            "until condition not reached".into(),
203        ))
204    }
Source

pub fn batch_size(&self) -> usize

Source

pub fn run(&self) -> Result<()>

Examples found in repository?
examples/conformance_driver.rs (line 178)
148    fn process_until(&mut self, request: &Json) -> bunqueue_client::Result<()> {
149        let queue_name = text(request, "queue").to_owned();
150        let behavior = text(request, "behavior").to_owned();
151        let result = json_to_value(request["result"].clone());
152        let failed_once = Arc::new(Mutex::new(HashSet::new()));
153        let seen = failed_once.clone();
154        let processor = move |job: Job| match behavior.as_str() {
155            "unrecoverable" => Err(ProcessError::unrecoverable("conformance poison")),
156            "deepThrow" => deep_error(25),
157            "failOnce" => {
158                let mut seen = seen.lock().expect("seen mutex poisoned");
159                if seen.insert(job.id()) {
160                    Err(ProcessError::retryable("conformance transient"))
161                } else {
162                    Ok(result.clone())
163                }
164            }
165            _ => Ok(result.clone()),
166        };
167        let worker = Worker::new(
168            queue_name.clone(),
169            processor,
170            WorkerOptions {
171                connection: self.options.clone(),
172                batch_size: number(request, "batchSize", 10) as usize,
173                poll_timeout_ms: 300,
174                ..Default::default()
175            },
176        );
177        let runner = worker.clone();
178        let handle = thread::spawn(move || runner.run());
179        let deadline =
180            Instant::now() + Duration::from_millis(number(request, "timeoutMs", 20_000) as u64);
181        let until = &request["until"];
182        while Instant::now() < deadline {
183            let counts = self.queue(&queue_name).get_job_counts().unwrap_or_default();
184            let completed = count(&counts, "completed") >= until["completed"].as_i64().unwrap_or(0);
185            let failed = count(&counts, "failed") >= until["failed"].as_i64().unwrap_or(0);
186            let dlq = self
187                .queue(&queue_name)
188                .get_dlq(None)
189                .map(|v| v.len())
190                .unwrap_or(0)
191                >= until["dlq"].as_u64().unwrap_or(0) as usize;
192            if completed && failed && dlq {
193                worker.stop();
194                let _ = handle.join();
195                return Ok(());
196            }
197            thread::sleep(Duration::from_millis(100));
198        }
199        worker.stop();
200        let _ = handle.join();
201        Err(bunqueue_client::Error::Timeout(
202            "until condition not reached".into(),
203        ))
204    }
Source

pub fn run_once(&self) -> Result<usize>

Source

pub fn stop(&self)

Examples found in repository?
examples/conformance_driver.rs (line 193)
148    fn process_until(&mut self, request: &Json) -> bunqueue_client::Result<()> {
149        let queue_name = text(request, "queue").to_owned();
150        let behavior = text(request, "behavior").to_owned();
151        let result = json_to_value(request["result"].clone());
152        let failed_once = Arc::new(Mutex::new(HashSet::new()));
153        let seen = failed_once.clone();
154        let processor = move |job: Job| match behavior.as_str() {
155            "unrecoverable" => Err(ProcessError::unrecoverable("conformance poison")),
156            "deepThrow" => deep_error(25),
157            "failOnce" => {
158                let mut seen = seen.lock().expect("seen mutex poisoned");
159                if seen.insert(job.id()) {
160                    Err(ProcessError::retryable("conformance transient"))
161                } else {
162                    Ok(result.clone())
163                }
164            }
165            _ => Ok(result.clone()),
166        };
167        let worker = Worker::new(
168            queue_name.clone(),
169            processor,
170            WorkerOptions {
171                connection: self.options.clone(),
172                batch_size: number(request, "batchSize", 10) as usize,
173                poll_timeout_ms: 300,
174                ..Default::default()
175            },
176        );
177        let runner = worker.clone();
178        let handle = thread::spawn(move || runner.run());
179        let deadline =
180            Instant::now() + Duration::from_millis(number(request, "timeoutMs", 20_000) as u64);
181        let until = &request["until"];
182        while Instant::now() < deadline {
183            let counts = self.queue(&queue_name).get_job_counts().unwrap_or_default();
184            let completed = count(&counts, "completed") >= until["completed"].as_i64().unwrap_or(0);
185            let failed = count(&counts, "failed") >= until["failed"].as_i64().unwrap_or(0);
186            let dlq = self
187                .queue(&queue_name)
188                .get_dlq(None)
189                .map(|v| v.len())
190                .unwrap_or(0)
191                >= until["dlq"].as_u64().unwrap_or(0) as usize;
192            if completed && failed && dlq {
193                worker.stop();
194                let _ = handle.join();
195                return Ok(());
196            }
197            thread::sleep(Duration::from_millis(100));
198        }
199        worker.stop();
200        let _ = handle.join();
201        Err(bunqueue_client::Error::Timeout(
202            "until condition not reached".into(),
203        ))
204    }
Source

pub fn close(&self)

Trait Implementations§

Source§

impl Clone for Worker

Source§

fn clone(&self) -> Worker

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.