Skip to main content

Queue

Struct Queue 

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

The job queue: provided and exported by QueueModule, which also registers it as the app’s dyn Dispatcher.

Jobs are stored by the configured driver until a worker (work, command queue:work) runs them with the handler a module registered (m.job::<J>()).

Queue is a cheap handle: clones share the same driver.

Implementations§

Source§

impl Queue

Source

pub fn options(&self) -> &QueueOptions

The options the queue runs with.

Source

pub async fn dispatch<J: Job>(&self, job: J) -> Result<String, QueueError>

Queues job to run as soon as a worker is free; returns its id.

Inside a unit of work’s scope (Uow::current: a request handler, Db::transaction) the job joins it, as with dispatch_in: it is queued only if that unit of work commits, and workers see it after the commit. (On SQLite this is also what keeps the insert from waiting for the transaction’s write lock.) Elsewhere, or once the current unit of work has finished, the job is stored at once. The same holds for later, push and dyn Dispatcher.

Source

pub async fn dispatch_in<J: Job>( &self, uow: &Uow, job: J, ) -> Result<String, QueueError>

Queues job as part of the unit of work uow: with the database driver the row is written in its transaction, otherwise the job is queued once it commits. Either way a rolled-back unit of work queues nothing, and workers never see the job before the commit. Returns the id the job gets.

Source

pub async fn later<J: Job>( &self, job: J, delay: Duration, ) -> Result<String, QueueError>

Queues job to run no earlier than delay from now (millisecond precision); returns its id. Joins the current unit of work like dispatch.

Source

pub async fn push(&self, envelope: JobEnvelope) -> Result<String, QueueError>

Queues a serialized job (what dyn Dispatcher does), honouring its delay_secs; returns its id. Joins the current unit of work like dispatch.

Source

pub async fn push_in( &self, uow: &Uow, envelope: JobEnvelope, ) -> Result<String, QueueError>

Queues a serialized job as part of uow (see dispatch_in); returns its id.

With the memory and Redis drivers, a unique job is deduplicated when the unit of work commits, so the id returned here is the new job’s id even if a pending duplicate later takes its place.

Source

pub async fn push_observed( &self, uow: &Uow, envelope: JobEnvelope, ) -> Result<String, QueueError>

Queues a serialized job from inside a ChangeObserver, as part of the commit it observes: uow is the unit of work the observer is given, which runs statements in the transaction being committed. With the database driver the row is written through it; otherwise the job is queued once uow has committed. A commit that fails queues nothing. Returns the id the job gets.

Source

pub async fn failed(&self) -> Result<Vec<FailedJob>, QueueError>

Every failed job, newest first.

Source

pub async fn failed_summary( &self, since: DateTime<Utc>, limit: usize, ) -> Result<FailedSummary, QueueError>

How many jobs failed, how many of them at or after since, and the newest limit, without their payloads: what to read on a page polled every few seconds, where failed would read the whole table.

Source

pub async fn retry( &self, target: impl Into<RetryTarget>, ) -> Result<u64, QueueError>

Puts failed jobs back on their queues, to run as soon as possible with their attempts starting over (same id and options, not unique); returns how many. retry("all") or RetryTarget::All retries every failed job; an unknown id is QueueError::FailedJobNotFound.

Source

pub async fn flush_failed(&self) -> Result<u64, QueueError>

Deletes every failed job; returns how many.

Source

pub async fn pending_count(&self, queue: &str) -> Result<u64, QueueError>

How many jobs of queue are not finished yet: waiting, delayed or running.

Source

pub async fn failed_job( &self, id: &str, ) -> Result<Option<FailedJob>, QueueError>

The failed job with id id, if there is one.

Source

pub async fn forget(&self, id: &str) -> Result<(), QueueError>

Deletes one failed job (it is not retried); QueueError::FailedJobNotFound when there is none with that id.

Source

pub async fn status(&self, queue: &str) -> Result<QueueStatus, QueueError>

queue now: how many jobs wait (available now), are delayed (available later) and run (reserved by a worker), and since when its oldest waiting job is available, so oldest_wait is how far the queue is behind. One query with the database driver.

Source

pub async fn pending_jobs( &self, query: &PendingQuery, ) -> Result<Vec<PendingJob>, QueueError>

The jobs that are not finished yet, oldest first (by availability): waiting, delayed and running, filtered by query.

Source

pub async fn pending_job( &self, id: &str, ) -> Result<Option<PendingJob>, QueueError>

The unfinished job with id id, if there is one.

Source

pub async fn dispatch_batch(&self, batch: Batch) -> Result<JobBatch, QueueError>

Dispatches batch: stores the batch and queues its jobs, all at once; returns the stored batch. Joins the current unit of work like dispatch. A batch with no jobs finishes at once (its then and finally are queued with it).

Every job type in the batch, callbacks included, must be registered (QueueError::UnknownJob otherwise, and nothing is queued).

Source

pub async fn dispatch_batch_in( &self, uow: &Uow, batch: Batch, ) -> Result<JobBatch, QueueError>

Dispatches batch as part of the unit of work uow (see dispatch_in): nothing is stored if it rolls back.

Source

pub async fn find_batch(&self, id: &str) -> Result<Option<JobBatch>, QueueError>

The batch id, with its counts as they are now.

Source

pub async fn batches(&self, limit: usize) -> Result<Vec<JobBatch>, QueueError>

The most recent batches, newest first, at most limit.

Source

pub async fn cancel_batch( &self, id: &str, ) -> Result<Option<JobBatch>, QueueError>

Cancels batch id: its jobs that have not started are skipped, and running ones see it through ctx.is_cancelled() and ctx.progress(..). finally still runs once every job has ended; then does not. A finished batch stays as it is. Returns the batch afterwards (None when there is none).

Source

pub async fn prune_batches( &self, older_than: Duration, ) -> Result<u64, QueueError>

Deletes the batches whose jobs and callbacks all finished more than older_than ago; returns how many.

Source§

impl Queue

Source

pub async fn run_pending(&self) -> Result<WorkStats, QueueError>

Runs every job available now, one at a time in queue order, and returns when none is left (jobs released for a later retry and delayed jobs stay queued). For tests: dispatch, then run_pending, then assert.

Source

pub async fn work_supervised( &self, opts: WorkOptions, supervision: Supervision, ) -> Result<WorkStats, QueueError>

work under a supervisor (navree-fleet’s workers): with Supervision::workers concurrent workers, stopped by Supervision::stop as well as by the app’s shutdown, asking the hooks before each reservation (a paused supervisor holds its workers) and telling them every attempt.

Source

pub async fn work(&self, opts: WorkOptions) -> Result<WorkStats, QueueError>

Runs jobs with workers concurrent workers (one with once) until the app shuts down, max_jobs jobs ran, or, with stop_when_empty or once, no job is available. A running job is always finished first.

Each job runs with the handler registered for its type, bounded by its timeout_secs; a panic counts as a failed attempt.

Errors of the driver end the call when it would stop on an empty queue; otherwise they are logged and the worker polls again.

A job’s outcome is stored for as long as its worker holds it, whatever the database says for now. Past that, another worker may take the job and it runs again: the worker logs an error naming it, and a call limited by once, stop_when_empty or max_jobs returns the error, never Ok.

A QueueFake queue runs nothing and returns at once.

Trait Implementations§

Source§

impl Clone for Queue

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 Queue

Source§

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

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

impl Dispatcher for Queue

Source§

fn dispatch_envelope( &self, env: JobEnvelope, ) -> BoxFuture<'_, Result<String, BoxError>>

Queues a serialized job and returns its id.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Queue

§

impl !UnwindSafe for Queue

§

impl Forget for Queue

§

impl Freeze for Queue

§

impl Move for Queue

§

impl Send for Queue

§

impl Sync for Queue

§

impl Unpin for Queue

§

impl UnsafeUnpin for Queue

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