Skip to main content

ProcessError

Struct ProcessError 

Source
pub struct ProcessError { /* private fields */ }
Expand description

A processor failure. Mark it unrecoverable to skip retries and enter DLQ.

Implementations§

Source§

impl ProcessError

Source

pub fn retryable(message: impl Into<String>) -> Self

Examples found in repository?
examples/conformance_driver.rs (line 160)
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    }
205}
206
207fn options(value: &Json) -> JobOptions {
208    JobOptions {
209        delay: value["delay"].as_i64(),
210        attempts: value["attempts"].as_i64(),
211        backoff: value["backoff"]
212            .as_i64()
213            .map(bunqueue_client::Backoff::Milliseconds),
214        job_id: value["jobId"].as_str().map(str::to_owned),
215        ..Default::default()
216    }
217}
218
219fn job_view(job: Option<Job>) -> Json {
220    job.map(|job| {
221        json!({
222            "id": job.id(),
223            "name": job.name(),
224            "data": value_to_json(&job.data()),
225            "stacktrace": job.stacktrace()
226        })
227    })
228    .unwrap_or(Json::Null)
229}
230
231fn deep_error(depth: usize) -> Result<Value, ProcessError> {
232    if depth == 0 {
233        Err(ProcessError::retryable("BOOM-CONFORMANCE"))
234    } else {
235        deep_error(depth - 1)
236    }
237}
Source

pub fn unrecoverable(message: impl Into<String>) -> Self

Examples found in repository?
examples/conformance_driver.rs (line 155)
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 message(&self) -> &str

Source

pub fn is_unrecoverable(&self) -> bool

Trait Implementations§

Source§

impl Debug for ProcessError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for ProcessError

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for ProcessError

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Replaced by Error::source.
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. 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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.