Skip to main content

OUTBOX_ID_HEADER

Constant OUTBOX_ID_HEADER 

Source
pub const OUTBOX_ID_HEADER: &str = "x-ruststream-outbox-id";
Expand description

The header a tracked message carries its record’s id in: written with the id’s Display, read back with its FromStr.

§Examples

use ruststream::runtime::{BlanketLayer, Context, Handler};
use ruststream_sqlx::outbox::OUTBOX_ID_HEADER;

/// A layer of the service's own that logs the record id of every tracked delivery.
#[derive(Clone, Copy)]
pub struct RecordIds;

pub struct Logged<H>(H);

impl BlanketLayer for RecordIds {
    fn apply<M, C, S, H>(&self, handler: H) -> impl Handler<M, C, S> + 'static
    where
        M: Send + Sync + 'static,
        C: Send + 'static,
        S: Send + Sync + 'static,
        H: Handler<M, C, S> + 'static,
    {
        Logged(handler)
    }
}

impl<M: Sync, C: Send, S: Send + Sync, H: Handler<M, C, S>> Handler<M, C, S> for Logged<H> {
    async fn handle(&self, msg: &M, ctx: &mut Context<'_, C, S>) -> HandlerOutcome {
        if let Some(id) = ctx.headers().get(OUTBOX_ID_HEADER) {
            let id = String::from_utf8_lossy(id);
            tracing::info!(subscription = ctx.name(), %id, "a tracked delivery");
        }
        self.0.handle(msg, ctx).await
    }
}

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