Skip to main content

Batch

Struct Batch 

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

A group of jobs dispatched together and followed as one: how many are still pending, how many failed, whether the group was cancelled, plus callbacks for its outcome. Dispatch it with dispatch or dispatch_in (or the same methods on Queue: dispatch_batch, dispatch_batch_in).

use navree_queue::{Batch, Queue};

let chunks = vec![vec![1, 2], vec![3, 4]];
let batch = Batch::new("recipients.import")
    .add(chunks.into_iter().map(|rows| ImportChunk { rows }))
    .then(FinishImport { id: 7 })
    .catch(ReportImportFailure { id: 7 })
    .dispatch_in(queue, uow)
    .await?;
println!("batch {} has {} jobs", batch.id, batch.total_jobs);
  • The batch finishes when each of its jobs has succeeded, failed (used its attempts) or been skipped.
  • then runs once it finishes with no failed job and without being cancelled; catch runs as soon as the first job fails; finally runs once it finishes, whatever happened.
  • The first failed job cancels the batch, unless it allow_failures: the jobs that have not started yet are then skipped. Queue::cancel_batch cancels it on demand; running jobs see it through ctx.is_cancelled() and ctx.progress(..).
  • Jobs in a batch are never unique (unique_for_secs is ignored), so every job counts.

Implementations§

Source§

impl Batch

Source

pub fn new(name: impl Into<String>) -> Self

An empty batch named name (recipients.import): what logs, queue:batches and JobBatch::name show.

Source

pub fn on_queue(self, queue: impl Into<String>) -> Self

Puts every job of the batch, and its callbacks, on queue instead of the queue each job type names (JobOptions::queue): jobs added before and after the call.

Source

pub fn add<J: Job>(self, jobs: impl IntoIterator<Item = J>) -> Self

Adds jobs to the batch.

Source

pub fn push<J: Job>(self, job: J) -> Self

Adds one job to the batch.

Source

pub fn then<J: Job>(self, job: J) -> Self

The job to dispatch once the batch finishes with no failed job and without being cancelled.

Source

pub fn catch<J: Job>(self, job: J) -> Self

The job to dispatch as soon as one of the batch’s jobs fails.

Source

pub fn finally<J: Job>(self, job: J) -> Self

The job to dispatch once the batch finishes, whatever happened.

Source

pub fn allow_failures(self) -> Self

Keeps running the other jobs when one fails (by default the first failure cancels the batch). then still runs only when no job failed.

Source

pub fn operation(self, id: Uuid) -> Self

Ties the batch to the operation id (see Operations): its progress becomes the operation’s, its jobs and callbacks report into it, and the operation finishes when the batch and its callbacks have run. Operations::dispatch_batch sets it for you.

Source

pub fn len(&self) -> usize

How many jobs the batch has.

Source

pub fn is_empty(&self) -> bool

Whether the batch has no jobs (it finishes as soon as it is dispatched).

Source

pub fn name(&self) -> &str

The batch’s name.

Source

pub async fn dispatch(self, queue: &Queue) -> Result<JobBatch, QueueError>

Dispatches the batch on queue: the fluent form of Queue::dispatch_batch (it joins the current unit of work the same way).

Source

pub async fn dispatch_in( self, queue: &Queue, uow: &Uow, ) -> Result<JobBatch, QueueError>

Dispatches the batch on queue as part of uow: the fluent form of Queue::dispatch_batch_in. Nothing is stored if uow rolls back.

Trait Implementations§

Source§

impl Clone for Batch

Source§

fn clone(&self) -> Self

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
Source§

impl Debug for Batch

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Forget for Batch

§

impl Freeze for Batch

§

impl Move for Batch

§

impl RefUnwindSafe for Batch

§

impl Send for Batch

§

impl Sync for Batch

§

impl Unpin for Batch

§

impl UnsafeUnpin for Batch

§

impl UnwindSafe for Batch

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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 = !

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.
Source§

impl<T> ValueFor<Option<T>> for T

Source§

fn into_value_for(self) -> Option<T>

The value as the column’s type.
Source§

impl<T> ValueFor<T> for T

Source§

fn into_value_for(self) -> T

The value as the column’s type.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more