Skip to main content

Publish

Trait Publish 

Source
pub trait Publish<DB: Database>: OutboxRow {
    // Required method
    fn publish(
        conn: &mut DB::Connection,
        msg: &OutgoingMessage<'_>,
    ) -> impl Future<Output = Result<Self::Id, Error>> + Send;
}
Expand description

Creates the record of a message a handler publishes under a registered name, and returns its id, which the message then carries in OUTBOX_ID_HEADER.

It has no default: the service’s statement lays the name, the payload and the headers out in its columns. A record type without it does not register.

§Examples

use ruststream_sqlx::{Outbox, outbox};

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

impl outbox::Publish<Postgres> for OrderOutbox {
    async fn publish(conn: &mut PgConnection, msg: &OutgoingMessage<'_>) -> sqlx::Result<i64> {
        sqlx::query_scalar("INSERT INTO outbox (name, payload) VALUES ($1, $2) RETURNING id")
            .bind(msg.name())
            .bind(msg.payload())
            .fetch_one(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 publish( conn: &mut DB::Connection, msg: &OutgoingMessage<'_>, ) -> impl Future<Output = Result<Self::Id, Error>> + Send

Inserts the record of msg and returns its id.

§Errors

The database’s error; the message is not sent, and the publish fails with it.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§