Skip to main content

QueueStore

Trait QueueStore 

Source
pub trait QueueStore:
    Send
    + Sync
    + 'static {
    // Required methods
    fn insert(&self, job: NewJob) -> BoxFuture<'_, Result<String, QueueError>>;
    fn reserve<'a>(
        &'a self,
        queue: &'a str,
        worker: &'a str,
        now: i64,
    ) -> BoxFuture<'a, Result<Option<ReservedJob>, QueueError>>;
    fn complete<'a>(
        &'a self,
        job: &'a ReservedJob,
    ) -> BoxFuture<'a, Result<(), QueueError>>;
    fn release<'a>(
        &'a self,
        job: &'a ReservedJob,
        worker: &'a str,
        available_at: i64,
    ) -> BoxFuture<'a, Result<bool, QueueError>>;
    fn fail<'a>(
        &'a self,
        job: &'a ReservedJob,
        worker: &'a str,
        error: &'a str,
        now: i64,
    ) -> BoxFuture<'a, Result<bool, QueueError>>;
    fn failed(&self) -> BoxFuture<'_, Result<Vec<FailedJob>, QueueError>>;
    fn retry<'a>(
        &'a self,
        target: &'a RetryTarget,
        now: i64,
    ) -> BoxFuture<'a, Result<u64, QueueError>>;
    fn flush_failed(&self) -> BoxFuture<'_, Result<u64, QueueError>>;
    fn pending_count<'a>(
        &'a self,
        queue: &'a str,
    ) -> BoxFuture<'a, Result<u64, QueueError>>;

    // Provided method
    fn failed_summary(
        &self,
        since: DateTime<Utc>,
        limit: usize,
    ) -> BoxFuture<'_, Result<FailedSummary, QueueError>> { ... }
}
Expand description

A queue driver implemented outside this crate (navree-redis’s Redis driver is one): where jobs wait, are reserved by workers and end up when they fail. Times are Unix milliseconds from the app’s clock, passed in by the queue; a store never reads its own clock to decide what is due.

The built-in database and memory drivers have the same semantics, which a store must keep:

  • Order. reserve takes the job of the queue with the smallest (available_at, id) among those a worker may take: unreserved jobs whose available_at has come, and reserved jobs whose reservation is older than NewJob::stale_after_ms (a crashed worker’s). Ids are UUID v7, so equal times run in dispatch order.
  • Atomic reservations. Two workers never reserve the same job at the same time. Reserving counts an attempt and records the worker.
  • Ownership. release and fail act only while worker still holds the reservation, and return false otherwise (the job was taken over).
  • Unique jobs. A job with a unique_key is not stored while a pending job with the same key blocks it (NewJob::blocked_by); insert returns the pending job’s id. An older pending job that no longer blocks loses its key.
  • Failed jobs are kept until retry (which puts them back with FailedJob::requeued) or flush_failed.
  • Membership. A job’s membership (its operation) is stored with it and handed back on the ReservedJob. Batches need the database or memory driver: dispatching one on another driver is an error.

Select a store with queue.driver: redis resolves the provider of dyn QueueStore named redis. Jobs dispatched inside a unit of work are inserted after it commits.

Required Methods§

Source

fn insert(&self, job: NewJob) -> BoxFuture<'_, Result<String, QueueError>>

Stores job; returns its id, or the id of the pending unique job that takes its place.

Source

fn reserve<'a>( &'a self, queue: &'a str, worker: &'a str, now: i64, ) -> BoxFuture<'a, Result<Option<ReservedJob>, QueueError>>

Reserves the next job of queue for worker at now: increments its attempts and records the reservation. None when no job is available.

Source

fn complete<'a>( &'a self, job: &'a ReservedJob, ) -> BoxFuture<'a, Result<(), QueueError>>

Deletes job, which finished.

Source

fn release<'a>( &'a self, job: &'a ReservedJob, worker: &'a str, available_at: i64, ) -> BoxFuture<'a, Result<bool, QueueError>>

Unreserves job, available again at available_at. false when worker no longer holds the reservation.

Source

fn fail<'a>( &'a self, job: &'a ReservedJob, worker: &'a str, error: &'a str, now: i64, ) -> BoxFuture<'a, Result<bool, QueueError>>

Moves job to the failed jobs with error, failed at now. false when worker no longer holds the reservation.

Source

fn failed(&self) -> BoxFuture<'_, Result<Vec<FailedJob>, QueueError>>

Every failed job, newest first (by failed_at, then id).

Source

fn retry<'a>( &'a self, target: &'a RetryTarget, now: i64, ) -> BoxFuture<'a, Result<u64, QueueError>>

Moves the targeted failed jobs back to their queues at now (FailedJob::requeued); returns how many (0 for an unknown id).

Source

fn flush_failed(&self) -> BoxFuture<'_, Result<u64, QueueError>>

Deletes every failed job; returns how many.

Source

fn pending_count<'a>( &'a self, queue: &'a str, ) -> BoxFuture<'a, Result<u64, QueueError>>

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

Provided Methods§

Source

fn failed_summary( &self, since: DateTime<Utc>, limit: usize, ) -> BoxFuture<'_, Result<FailedSummary, QueueError>>

How many jobs failed, how many of them at or after since, and the newest limit. By default it reads failed; a store that can count without reading every job should.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§