Skip to main content

Driver

Trait Driver 

Source
pub trait Driver:
    Send
    + Sync
    + 'static {
    type Job: ReservedJob;

    // Required methods
    fn push(
        &self,
        queue: &str,
        payload: String,
        delay: Duration,
    ) -> impl Future<Output = Result<(), Error>> + Send;
    fn reserve(
        &self,
        queues: &[String],
    ) -> impl Future<Output = Result<Option<Self::Job>, Error>> + Send;
    fn clear(
        &self,
        queue: &str,
    ) -> impl Future<Output = Result<u64, Error>> + Send;

    // Provided methods
    fn bulk(
        &self,
        queue: &str,
        payloads: Vec<String>,
    ) -> impl Future<Output = Result<(), Error>> + Send { ... }
    fn sizes(
        &self,
        queue: &str,
    ) -> impl Future<Output = Result<Option<QueueSizes>, Error>> + Send { ... }
}
Expand description

A queue backend, which stores a connection’s jobs and hands each one to a worker.

Laravel includes the database, redis, sqs, and cloud drivers. A package adds another driver by implementing this trait and ReservedJob, then registering a named factory with Drivers::extend:

use std::time::Duration;

use laravel::queue::{Driver, Reservation, ReservedJob};

struct BeanstalkdDriver { /* a client */ }
struct BeanstalkdJob { /* a reserved job's ID, tube and body */ }

impl Driver for BeanstalkdDriver {
    type Job = BeanstalkdJob;

    async fn push(&self, queue: &str, payload: String, delay: Duration) -> laravel::Result<()> {
        // use queue, put 0 delay ttr payload...
    }

    async fn reserve(&self, queues: &[String]) -> laravel::Result<Option<BeanstalkdJob>> {
        // reserve-with-timeout 0 from each tube in turn...
    }

    async fn clear(&self, queue: &str) -> laravel::Result<u64> {
        // delete every job in the tube...
    }
}

impl ReservedJob for BeanstalkdJob {
    fn queue(&self) -> &str { "default" }
    fn payload(&self) -> &str { "{}" }
    fn attempts(&self) -> u32 { 1 }

    async fn renew(&self) -> laravel::Result<Reservation> {
        // touch id...
    }

    async fn delete(&self) -> laravel::Result<Reservation> {
        // delete id...
    }

    async fn release(&self, delay: Duration) -> laravel::Result<Reservation> {
        // release id with the delay...
    }
}

A driver stores payloads unchanged, and hands each payload to one worker at a time. The worker decides what happens to a job, and asks the reserved job to carry out that decision. A reservation lasts LEASE, and the worker renews it every HEARTBEAT while the job runs. When a reservation lapses, the job becomes available again, and the next reservation counts as another attempt.

Required Associated Types§

Source

type Job: ReservedJob

A job this driver reserved for a worker.

Required Methods§

Source

fn push( &self, queue: &str, payload: String, delay: Duration, ) -> impl Future<Output = Result<(), Error>> + Send

Add a job to queue, available once delay has passed.

§Errors

Fails when the job can’t be stored.

Source

fn reserve( &self, queues: &[String], ) -> impl Future<Output = Result<Option<Self::Job>, Error>> + Send

Reserve the next available job for LEASE, from the first of queues that has one.

Returns None when none of the queues has a job. This method may wait for a job to arrive, as a long poll does. When the worker is told to stop, it drops a pending reserve. A job reserved at that moment never runs, and becomes available again once its reservation lapses, as a stopped worker’s job does.

§Errors

Fails when the queues can’t be read.

Source

fn clear(&self, queue: &str) -> impl Future<Output = Result<u64, Error>> + Send

Delete every job on queue, and return the number of jobs deleted, as closely as the backend can count them.

§Errors

Fails when the queue can’t be cleared.

Provided Methods§

Source

fn bulk( &self, queue: &str, payloads: Vec<String>, ) -> impl Future<Output = Result<(), Error>> + Send

Add several jobs to queue, available now, in as few round trips as the backend allows.

By default, this method pushes each payload in turn. A driver whose backend can store many jobs at once overrides this method. The jobs are stored in order, and storing stops at the first job that fails, so the jobs after it aren’t stored.

§Errors

Fails when a job can’t be stored. The jobs before it may have been stored.

Source

fn sizes( &self, queue: &str, ) -> impl Future<Output = Result<Option<QueueSizes>, Error>> + Send

Count the jobs on queue by state, or return None when the backend can’t count them.

By default, this method returns None.

§Errors

Fails when the queue can’t be read.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§