Skip to main content

Extend

Trait Extend 

Source
pub trait Extend<DB: Database>: LeaseRow {
    // Required method
    fn extend(
        conn: &mut DB::Connection,
        id: &Self::Id,
        held: &Self::Lease,
        until: &Self::Lease,
    ) -> impl Future<Output = Result<bool, Error>> + Send;
}
Expand description

Extends the lease on a row: writes until into locked_until while the row still holds held, and says whether it did.

The dialect builds it for every table in the lease form; a table names extend as its own (custom(extend), or own::Extend by hand) to take it over, for a database without a built-in dialect. A subscription runs it each half lease for every delivery in work, on one connection. The crate also runs it with until equal to held to confirm a delivery’s lease, inside the transaction where an event of the service’s own then runs.

§Examples

use chrono::{DateTime, Utc};
use ruststream_sqlx::{Extend, Inbox};
use sqlx::{PgConnection, Postgres};

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

impl Extend<Postgres> for Job {
    async fn extend(
        conn: &mut PgConnection,
        id: &i64,
        held: &DateTime<Utc>,
        until: &DateTime<Utc>,
    ) -> Result<bool, sqlx::Error> {
        // The row keeps its lease only while it holds the expiry the delivery knows.
        let extended =
            sqlx::query("UPDATE jobs SET locked_until = $1 WHERE id = $2 AND locked_until = $3")
                .bind(until)
                .bind(id)
                .bind(held)
                .execute(conn)
                .await?;
        Ok(extended.rows_affected() == 1)
    }
}

Required Methods§

Source

fn extend( conn: &mut DB::Connection, id: &Self::Id, held: &Self::Lease, until: &Self::Lease, ) -> impl Future<Output = Result<bool, Error>> + Send

Writes until into the lease of the row id while it still holds held; true when it did, false when the row no longer holds held.

§Errors

The database’s error.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§