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
impl Queue
Sourcepub fn options(&self) -> &QueueOptions
pub fn options(&self) -> &QueueOptions
The options the queue runs with.
Sourcepub async fn dispatch<J: Job>(&self, job: J) -> Result<String, QueueError>
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.
Sourcepub async fn dispatch_in<J: Job>(
&self,
uow: &Uow,
job: J,
) -> Result<String, QueueError>
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.
Sourcepub async fn later<J: Job>(
&self,
job: J,
delay: Duration,
) -> Result<String, QueueError>
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.
Sourcepub async fn push(&self, envelope: JobEnvelope) -> Result<String, QueueError>
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.
Sourcepub async fn push_in(
&self,
uow: &Uow,
envelope: JobEnvelope,
) -> Result<String, QueueError>
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.
Sourcepub async fn push_observed(
&self,
uow: &Uow,
envelope: JobEnvelope,
) -> Result<String, QueueError>
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.
Sourcepub async fn failed(&self) -> Result<Vec<FailedJob>, QueueError>
pub async fn failed(&self) -> Result<Vec<FailedJob>, QueueError>
Every failed job, newest first.
Sourcepub async fn failed_summary(
&self,
since: DateTime<Utc>,
limit: usize,
) -> Result<FailedSummary, QueueError>
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.
Sourcepub async fn retry(
&self,
target: impl Into<RetryTarget>,
) -> Result<u64, QueueError>
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.
Sourcepub async fn flush_failed(&self) -> Result<u64, QueueError>
pub async fn flush_failed(&self) -> Result<u64, QueueError>
Deletes every failed job; returns how many.
Sourcepub async fn pending_count(&self, queue: &str) -> Result<u64, QueueError>
pub async fn pending_count(&self, queue: &str) -> Result<u64, QueueError>
How many jobs of queue are not finished yet: waiting, delayed or
running.
Sourcepub async fn failed_job(
&self,
id: &str,
) -> Result<Option<FailedJob>, QueueError>
pub async fn failed_job( &self, id: &str, ) -> Result<Option<FailedJob>, QueueError>
The failed job with id id, if there is one.
Sourcepub async fn forget(&self, id: &str) -> Result<(), QueueError>
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.
Sourcepub async fn status(&self, queue: &str) -> Result<QueueStatus, QueueError>
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.
Sourcepub async fn pending_jobs(
&self,
query: &PendingQuery,
) -> Result<Vec<PendingJob>, QueueError>
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.
Sourcepub async fn pending_job(
&self,
id: &str,
) -> Result<Option<PendingJob>, QueueError>
pub async fn pending_job( &self, id: &str, ) -> Result<Option<PendingJob>, QueueError>
The unfinished job with id id, if there is one.
Sourcepub async fn dispatch_batch(&self, batch: Batch) -> Result<JobBatch, QueueError>
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).
Sourcepub async fn dispatch_batch_in(
&self,
uow: &Uow,
batch: Batch,
) -> Result<JobBatch, QueueError>
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.
Sourcepub async fn find_batch(&self, id: &str) -> Result<Option<JobBatch>, QueueError>
pub async fn find_batch(&self, id: &str) -> Result<Option<JobBatch>, QueueError>
The batch id, with its counts as they are now.
Sourcepub async fn batches(&self, limit: usize) -> Result<Vec<JobBatch>, QueueError>
pub async fn batches(&self, limit: usize) -> Result<Vec<JobBatch>, QueueError>
The most recent batches, newest first, at most limit.
Sourcepub async fn cancel_batch(
&self,
id: &str,
) -> Result<Option<JobBatch>, QueueError>
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).
Sourcepub async fn prune_batches(
&self,
older_than: Duration,
) -> Result<u64, QueueError>
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
impl Queue
Sourcepub async fn run_pending(&self) -> Result<WorkStats, QueueError>
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.
Sourcepub async fn work_supervised(
&self,
opts: WorkOptions,
supervision: Supervision,
) -> Result<WorkStats, QueueError>
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.
Sourcepub async fn work(&self, opts: WorkOptions) -> Result<WorkStats, QueueError>
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 Dispatcher for Queue
impl Dispatcher for Queue
Source§fn dispatch_envelope(
&self,
env: JobEnvelope,
) -> BoxFuture<'_, Result<String, BoxError>>
fn dispatch_envelope( &self, env: JobEnvelope, ) -> BoxFuture<'_, Result<String, BoxError>>
Auto Trait Implementations§
impl !RefUnwindSafe for Queue
impl !UnwindSafe for Queue
impl Forget for Queue
impl Freeze for Queue
impl Send for Queue
impl Sync for Queue
impl Unpin for Queue
impl UnsafeUnpin for Queue
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