1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//! The transactional outbox: what a service publishes is recorded in its own table until a
//! consumer has processed it, and what was not processed is published again at startup.
//!
//! A struct deriving [`Outbox`](derive@crate::Outbox), or implementing [`OutboxTable`] by hand,
//! describes the table; `outbox!`, [`Outbox::register`] or [`Outbox::track`] registers it under the
//! names it tracks, and the registry hands out the two middlewares and the republish. The events
//! below are what the record's statements do; a service implements the ones it names as its own
//! (`#[outbox(custom(..))]`, or [`OutboxSpec::own`]), and always [`Publish`], which has no
//! default.
//!
//! Each topic has its page on the site:
//!
//! - [The outbox](https://powersemmi.github.io/ruststream-sqlx/latest/components/outbox/): what it tracks and the at-least-once guarantee.
//! - [Record](https://powersemmi.github.io/ruststream-sqlx/latest/components/outbox/record/): the outbox table, derived or described by hand.
//! - [Registry](https://powersemmi.github.io/ruststream-sqlx/latest/components/outbox/registry/): `outbox!`, [`Outbox::track`] and
//! [`Outbox::register`].
//! - [Tracking](https://powersemmi.github.io/ruststream-sqlx/latest/components/outbox/tracking/): [`Outbox::layer`] and [`Outbox::publish_layer`].
//! - [republish](https://powersemmi.github.io/ruststream-sqlx/latest/components/outbox/republish/): [`Outbox::republish`] at startup.
//! - [wrap](https://powersemmi.github.io/ruststream-sqlx/latest/components/outbox/wrap/): [`Outbox::wrap`], a publisher outside the pipeline.
//! - [set_pool](https://powersemmi.github.io/ruststream-sqlx/latest/components/outbox/set-pool/): [`Outbox::set_pool`] inside `#[ruststream::app]`.
//! - [Statements](https://powersemmi.github.io/ruststream-sqlx/latest/components/outbox/statements/): the record's events in the service's own SQL.
//! - [Interplay](https://powersemmi.github.io/ruststream-sqlx/latest/components/outbox/interplay/): the outbox beside the inbox.
//! - [Switch](https://powersemmi.github.io/ruststream-sqlx/latest/components/outbox/switch/): `RUSTSTREAM_SQLX_OUTBOX=on` in test builds.
//! - [OutboxError](https://powersemmi.github.io/ruststream-sqlx/latest/components/outbox/outbox-error/): [`OutboxError`].
//!
//! # Examples
//!
//! A service whose replies to `orders` are tracked over the in-memory broker. Inside
//! `#[ruststream::app]` the registry starts without a pool, and `on_startup` hands it one:
//!
//! ```
//! # #[cfg(all(feature = "outbox", feature = "postgres"))]
//! # mod demo {
//! use std::convert::Infallible;
//! use std::io;
//!
//! use chrono::{DateTime, Utc};
//! use ruststream::OutgoingMessage;
//! use ruststream::memory::prelude::*;
//! use ruststream_sqlx::{Outbox, outbox};
//! use serde::{Deserialize, Serialize};
//! use sqlx::postgres::{PgConnection, PgPool, Postgres};
//!
//! // outbox: id BIGSERIAL PRIMARY KEY, name TEXT NOT NULL, payload BYTEA NOT NULL,
//! // processed_at TIMESTAMPTZ
//! #[derive(Outbox, sqlx::FromRow)]
//! #[outbox(table = "outbox")]
//! 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
//! }
//! }
//!
//! #[derive(Deserialize)]
//! struct PlaceOrder {
//! id: u64,
//! }
//!
//! #[derive(Serialize, Deserialize, Outgoing)]
//! #[outgoing(name = "orders")]
//! struct OrderPlaced {
//! id: u64,
//! }
//!
//! #[subscriber("checkout", reply)]
//! async fn place(cmd: &PlaceOrder) -> OrderPlaced {
//! OrderPlaced { id: cmd.id }
//! }
//!
//! #[subscriber("orders")]
//! async fn fulfil(order: &OrderPlaced) -> HandlerOutcome {
//! tracing::info!(order = order.id, "fulfilling the order");
//! HandlerOutcome::ack()
//! }
//!
//! /// What `#[ruststream::app]` runs.
//! 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())
//! .after_shutdown(async move |pool| {
//! pool.close().await;
//! Ok::<_, Infallible>(())
//! })
//! .with_broker(MemoryBroker::new(), |b| {
//! b.include(place).out_reply(Publish);
//! b.include(fulfil);
//! b.after_startup(Publish, tracking.republish());
//! })
//! }
//! # }
//! # fn main() {}
//! ```
pub use OutboxDatabase;
pub use ;
pub use ;
pub use ;
pub use ;
pub use TrackingLayer;
pub use TrackingPublishLayer;
pub use ;
pub use ;
pub use Republishing;
pub use OutboxRow;
pub use ;
pub use Store;
pub use TrackedPublisher;
/// The header a tracked message carries its record's id in: written with the id's `Display`,
/// read back with its `FromStr`.
///
/// # Examples
///
/// ```
/// # #[cfg(all(feature = "outbox", feature = "postgres"))]
/// # mod demo {
/// # use ruststream::OutgoingMessage;
/// # use ruststream::memory::prelude::*;
/// # use ruststream_sqlx::{Outbox, outbox};
/// # use serde::{Deserialize, Serialize};
/// # use sqlx::postgres::{PgConnection, PgPool, Postgres};
/// # #[derive(Outbox, sqlx::FromRow)]
/// # #[outbox(table = "outbox")]
/// # pub struct OrderOutbox { #[field(id)] id: i64, #[field(name)] name: String, #[field(payload)] payload: Vec<u8> }
/// # 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
/// # }
/// # }
/// # #[derive(Deserialize)] pub struct PlaceOrder { id: u64 }
/// # #[derive(Serialize, Deserialize, Outgoing)] #[outgoing(name = "orders")] pub struct OrderPlaced { id: u64 }
/// # #[subscriber("checkout", reply)] async fn place(cmd: &PlaceOrder) -> OrderPlaced { OrderPlaced { id: cmd.id } }
/// # #[subscriber("orders")] async fn fulfil(_: &OrderPlaced) -> HandlerOutcome { HandlerOutcome::ack() }
/// 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);
/// })
/// }
/// # }
/// # fn main() {}
/// ```
pub const OUTBOX_ID_HEADER: &str = "x-ruststream-outbox-id";