Skip to main content

Recover

Trait Recover 

Source
pub trait Recover<DB: Database>: OutboxRow {
    // Required method
    fn recover(
        conn: &mut DB::Connection,
        name: &str,
    ) -> impl Future<Output = Result<Vec<Self>, Error>> + Send;
}
Expand description

Selects the unprocessed records of one name, for the republish at startup.

The default selects the records of name whose processed_at is NULL, or every record of name when the struct has no processed_at field.

§Examples

use ruststream_sqlx::{Outbox, outbox};

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

// A record published in the last minute may still be in flight; the next startup takes it.
impl outbox::Recover<Postgres> for OrderOutbox {
    async fn recover(conn: &mut PgConnection, name: &str) -> sqlx::Result<Vec<Self>> {
        sqlx::query_as(
            "SELECT id, name, payload, processed_at FROM outbox \
             WHERE name = $1 AND processed_at IS NULL AND created_at < now() - interval '1 minute'",
        )
        .bind(name)
        .fetch_all(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 recover( conn: &mut DB::Connection, name: &str, ) -> impl Future<Output = Result<Vec<Self>, Error>> + Send

The unprocessed records published under name.

§Errors

The database’s error; startup fails with it.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§