Skip to main content

Fetch

Trait Fetch 

Source
pub trait Fetch<DB: Database>: OutboxRow {
    // Required method
    fn fetch(
        conn: &mut DB::Connection,
        id: &Self::Id,
    ) -> impl Future<Output = Result<Option<Self>, Error>> + Send;
}
Expand description

Takes the record id into work when its message reaches a subscription.

The default selects the record while it is unprocessed. None means the record is taken or processed already: the handler does not run, and the delivery is acknowledged.

§Examples

use ruststream_sqlx::{Outbox, outbox};

#[derive(Outbox, sqlx::FromRow)]
#[outbox(table = "outbox", custom(fetch))]
pub struct OrderOutbox {
    #[field(id)]
    id: i64,
    #[field(name)]
    name: String,
    #[field(payload)]
    payload: Vec<u8>,
    #[field(processed_at)]
    processed_at: Option<DateTime<Utc>>,
}

// The service's fetch also stamps when the record was taken.
impl outbox::Fetch<Postgres> for OrderOutbox {
    async fn fetch(conn: &mut PgConnection, id: &i64) -> sqlx::Result<Option<Self>> {
        sqlx::query_as(
            "UPDATE outbox SET taken_at = now() WHERE id = $1 AND processed_at IS NULL \
             RETURNING id, name, payload, processed_at",
        )
        .bind(id)
        .fetch_optional(conn)
        .await
    }
}

pub fn app(pool: PgPool) -> impl App {
    let tracking = outbox! { pool: pool, "orders" => OrderOutbox };
    RustStream::new(AppInfo::new("orders", "0.1.0"))
        .layer(tracking.layer())
        .publish_layer(tracking.publish_layer())
        .with_broker(MemoryBroker::new(), |b| {
            b.include(place).out_reply(Publish);
            b.include(fulfil);
            b.after_startup(Publish, tracking.republish());
        })
}

Required Methods§

Source

fn fetch( conn: &mut DB::Connection, id: &Self::Id, ) -> impl Future<Output = Result<Option<Self>, Error>> + Send

Takes the record id into work, or None when it is taken or processed already.

§Errors

The database’s error; the handler does not run, and the delivery is retried.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§