pub trait InboxHeaders:
Sized
+ Send
+ Sync
+ 'static { }Expand description
A struct that describes a queue table whose message is assembled from it: the mechanics a
subscription runs the queue by, and the service’s own headers. #[derive(InboxHeaders)]
implements it.
The headers struct takes the table’s attributes (#[inbox(table, schema, advisory_lock, clock, isolation, mode)]) and the fields that play a role. Every field without a role is a
header: the delivery’s header map holds it under its column’s name, built the first time
something reads the delivery’s headers, and a field that holds None is left out. Its type
implements HeaderField. The derive also writes the generated
insert of the table’s columns.
The message struct derives Inbox and holds the headers struct in a
#[field(headers)] #[sqlx(flatten)] field beside its own data. A handler takes the message
struct itself, &Message, or &[Message] in a batch, as in row mode. Its rows are read
by the default fetch, which names the headers struct’s columns and the message’s own, or by
the message’s own Fetch with #[inbox(custom(fetch))], which may read the
data from other tables.
§Examples
use ruststream_sqlx::prelude::*;
use serde::Deserialize;
use sqlx::PgPool;
/// The queue table: the mechanics, and the service's own headers `tenant` and `trace`.
#[derive(Debug, Clone, InboxHeaders, sqlx::FromRow)]
#[inbox(table = "order_jobs")]
pub struct OrderHeaders {
#[field(id, generated)]
job_id: i64,
#[field(group)]
name: String,
#[field(attempt, generated)]
attempt: i16,
tenant: String,
trace: Option<String>,
}
/// The message a handler takes: the headers and the order's note, read from the same row.
#[derive(Debug, Clone, Inbox, sqlx::FromRow)]
pub struct OrderJob {
#[field(headers)]
#[sqlx(flatten)]
headers: OrderHeaders,
note: Option<String>,
}
#[derive(Deserialize)]
struct Tenant {
tenant: String,
}
// The header map is built from `OrderHeaders` on the first read: here, by `Headers<Tenant>`.
#[subscriber(InboxQueue::<OrderJob>::new("orders"))]
async fn ship(job: &OrderJob, Headers(tenant): Headers<Tenant>) -> HandlerOutcome {
tracing::info!(tenant = %tenant.tenant, note = ?job.note, "shipping");
HandlerOutcome::ack()
}
pub fn app(pool: PgPool) -> RustStream {
RustStream::new(AppInfo::new("shop", "0.1.0")).with_broker(SqlxBroker::new(pool), |b| {
b.include(ship);
})
}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".