Skip to main content

Claim

Trait Claim 

Source
pub trait Claim<DB: Database>: InboxRow {
    // Required method
    fn claim(
        conn: &mut DB::Connection,
        queue: &str,
        limit: i64,
    ) -> impl Future<Output = Result<Vec<Self::Id>, Error>> + Send;
}
Expand description

Claims up to limit rows of the queue queue and returns their ids, inside the claim’s transaction.

The dialect builds it from the table’s description; a table names claim as its own to take it over (custom(claim), or own::Claim by hand), for a database without a built-in dialect or a claim of its own. The crate then reads the rows with the service’s Fetch, or with its own fetch by a list of ids, which Postgres alone runs: on MySQL and SQLite a claim of the service’s own comes with a Fetch of its own, and a subscription without one stops at startup. In the lease form the crate also leases each id the claim returns, in the same transaction, and passes over an id whose row another lease holds.

§Examples

use ruststream_sqlx::{Claim, Inbox};
use sqlx::{PgConnection, Postgres};

#[derive(Inbox, sqlx::FromRow)]
#[inbox(table = "jobs", custom(claim))]
pub struct Job {
    #[field(id)]
    id: i64,
    #[field(payload)]
    payload: Vec<u8>,
}

impl Claim<Postgres> for Job {
    async fn claim(
        conn: &mut PgConnection,
        _queue: &str,
        limit: i64,
    ) -> Result<Vec<i64>, sqlx::Error> {
        // The oldest rows first, skipping the ones another worker holds.
        sqlx::query_scalar("SELECT id FROM jobs ORDER BY id LIMIT $1 FOR UPDATE SKIP LOCKED")
            .bind(limit)
            .fetch_all(conn)
            .await
    }
}

Required Methods§

Source

fn claim( conn: &mut DB::Connection, queue: &str, limit: i64, ) -> impl Future<Output = Result<Vec<Self::Id>, Error>> + Send

Claims up to limit rows and returns their ids.

§Errors

The database’s error; the subscription waits one second and claims again.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§