Skip to main content

outbox

Macro outbox 

Source
outbox!() { /* proc-macro */ }
Expand description

Registers outbox records under the names they track, and returns the registry.

outbox! { pool: pool, "orders" => OrderEvent, "refunds" => RefundEvent } is Outbox::new(pool).register::<OrderEvent>("orders").register::<RefundEvent>("refunds"). Without pool: the registry starts as Outbox::deferred(), and the pool is set once it is built. A name written twice does not compile, and the error points at its second literal.

ยงExamples

use std::io;

/// Inside `#[ruststream::app]`: no `pool:`, and the pool arrives in `on_startup`.
pub fn app() -> impl App {
    let tracking = outbox! { "orders" => OrderOutbox };
    let registry = tracking.clone();
    RustStream::new(AppInfo::new("orders", "0.1.0"))
        .on_startup(async move |()| {
            let pool = PgPool::connect("postgres://localhost/orders")
                .await
                .map_err(io::Error::other)?;
            registry.set_pool(pool.clone()).map_err(io::Error::other)?;
            Ok::<_, io::Error>(pool)
        })
        .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());
        })
}

/// An app built inside a running runtime takes the pool it has.
pub fn app_with(pool: PgPool) -> impl App {
    let tracking = outbox! { pool: pool, "orders" => OrderOutbox };
    RustStream::new(AppInfo::new("orders", "0.1.0"))
        .publish_layer(tracking.publish_layer())
        .with_broker(MemoryBroker::new(), |b| {
            b.include(place).out_reply(Publish);
        })
}

Implemented in ruststream-sqlx-macros and used through ruststream-sqlx.