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.
thenruns once it finishes with no failed job and without being cancelled;catchruns as soon as the first job fails;finallyruns 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_batchcancels it on demand; running jobs see it throughctx.is_cancelled()andctx.progress(..). - Jobs in a batch are never unique (
unique_for_secsis ignored), so every job counts.
Implementations§
Source§impl Batch
impl Batch
Sourcepub fn new(name: impl Into<String>) -> Self
pub fn new(name: impl Into<String>) -> Self
An empty batch named name (recipients.import): what logs,
queue:batches and JobBatch::name show.
Sourcepub fn on_queue(self, queue: impl Into<String>) -> Self
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.
Sourcepub fn add<J: Job>(self, jobs: impl IntoIterator<Item = J>) -> Self
pub fn add<J: Job>(self, jobs: impl IntoIterator<Item = J>) -> Self
Adds jobs to the batch.
Sourcepub fn then<J: Job>(self, job: J) -> Self
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.
Sourcepub fn catch<J: Job>(self, job: J) -> Self
pub fn catch<J: Job>(self, job: J) -> Self
The job to dispatch as soon as one of the batch’s jobs fails.
Sourcepub fn finally<J: Job>(self, job: J) -> Self
pub fn finally<J: Job>(self, job: J) -> Self
The job to dispatch once the batch finishes, whatever happened.
Sourcepub fn allow_failures(self) -> Self
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.
Sourcepub fn operation(self, id: Uuid) -> Self
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.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether the batch has no jobs (it finishes as soon as it is dispatched).
Sourcepub async fn dispatch(self, queue: &Queue) -> Result<JobBatch, QueueError>
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).
Sourcepub async fn dispatch_in(
self,
queue: &Queue,
uow: &Uow,
) -> Result<JobBatch, QueueError>
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§
Auto Trait Implementations§
impl Forget for Batch
impl Freeze 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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