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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! The settings of an outbox table described by hand, as types.
//!
//! Each typed setter of [`OutboxSpec`] adds one marker of this module to the
//! table's settings, and the table's `type Table` lists them in the order the chain sets them. A
//! setting the chain leaves out keeps its default: no headers column, a processed record deleted,
//! and the crate's events. A setting set twice does not compile ([`Merge`]).
mod builder;
mod declaration;
pub use builder::{Described, OutboxSpec, OutboxTable};
pub use declaration::Declaration;
use declaration::setting;
pub use crate::settings::{Merge, Push, Set, Unset};
setting!(
/// The published headers, kept in one column: set by
/// [`OutboxSpec::headers`](super::OutboxSpec::headers). The record implements
/// [`HeaderRow`](crate::HeaderRow).
///
/// # Examples
///
/// ```
/// # #[cfg(all(feature = "outbox", feature = "json", feature = "postgres"))]
/// # mod demo {
/// # use ruststream::OutgoingMessage;
/// # use ruststream::memory::prelude::*;
/// # use serde::{Deserialize, Serialize};
/// # use ruststream_sqlx::outbox::{Outbox, TrackedName};
/// use std::collections::BTreeMap;
///
/// use ruststream_sqlx::HeaderRow;
/// use ruststream_sqlx::dialect::Column;
/// use ruststream_sqlx::outbox::spec::Headers;
/// use ruststream_sqlx::outbox::{self, OutboxSpec, OutboxTable};
/// use sqlx::types::Json;
/// use sqlx::{PgConnection, PgPool, Postgres};
///
/// #[derive(sqlx::FromRow)]
/// pub struct OrderEvent {
/// id: i64,
/// name: String,
/// payload: Vec<u8>,
/// headers: Option<Json<BTreeMap<String, String>>>,
/// }
///
/// impl OutboxTable for OrderEvent {
/// type Id = i64;
/// type Table = OutboxSpec<(Headers,)>;
/// const TABLE: Self::Table = OutboxSpec::new(
/// "outbox",
/// Column::new("id"),
/// Column::new("name"),
/// Column::new("payload"),
/// )
/// .headers(Column::new("headers"));
/// # fn id(&self) -> &i64 { &self.id }
/// # fn name(&self) -> &str { &self.name }
/// # fn payload(&self) -> &[u8] { &self.payload }
/// }
///
/// // A republished record carries the headers it was published with.
/// impl HeaderRow for OrderEvent {
/// type Column = Option<Json<BTreeMap<String, String>>>;
///
/// fn headers_mut(&mut self) -> &mut Self::Column {
/// &mut self.headers
/// }
/// }
/// # impl outbox::Publish<Postgres> for OrderEvent {
/// # 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() }
/// # pub struct Orders;
/// # impl TrackedName for Orders { const NAME: &'static str = "orders"; }
/// # pub fn app(pool: PgPool) -> impl App {
/// # let tracking = Outbox::new(pool).track::<OrderEvent, Orders>();
/// # 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());
/// # })
/// # }
/// # }
/// # fn main() {}
/// ```
Headers, Headers
);
setting!(
/// The time a record was processed, which the database's clock writes: set by
/// [`OutboxSpec::processed_at`](super::OutboxSpec::processed_at). Without it a processed record
/// is deleted.
///
/// # Examples
///
/// ```
/// # #[cfg(all(feature = "outbox", feature = "postgres"))]
/// # mod demo {
/// # use ruststream::OutgoingMessage;
/// # use ruststream::memory::prelude::*;
/// # use serde::{Deserialize, Serialize};
/// # use ruststream_sqlx::outbox::{Outbox, TrackedName};
/// use ruststream_sqlx::dialect::Column;
/// use ruststream_sqlx::outbox::spec::ProcessedAt;
/// use ruststream_sqlx::outbox::{self, OutboxSpec, OutboxTable};
/// use sqlx::{PgConnection, PgPool, Postgres};
///
/// #[derive(sqlx::FromRow)]
/// pub struct OrderEvent {
/// id: i64,
/// name: String,
/// payload: Vec<u8>,
/// }
///
/// // A processed record stays in the table, stamped with the database's time.
/// impl OutboxTable for OrderEvent {
/// type Id = i64;
/// type Table = OutboxSpec<(ProcessedAt,)>;
/// const TABLE: Self::Table = OutboxSpec::new(
/// "outbox",
/// Column::new("id"),
/// Column::new("name"),
/// Column::new("payload"),
/// )
/// .processed_at(Column::new("processed_at"));
/// # fn id(&self) -> &i64 { &self.id }
/// # fn name(&self) -> &str { &self.name }
/// # fn payload(&self) -> &[u8] { &self.payload }
/// }
/// # impl outbox::Publish<Postgres> for OrderEvent {
/// # 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() }
/// # pub struct Orders;
/// # impl TrackedName for Orders { const NAME: &'static str = "orders"; }
/// # pub fn app(pool: PgPool) -> impl App {
/// # let tracking = Outbox::new(pool).track::<OrderEvent, Orders>();
/// # 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());
/// # })
/// # }
/// # }
/// # fn main() {}
/// ```
ProcessedAt, ProcessedAt
);
/// The events a record runs itself instead of the crate's default, each set by
/// [`OutboxSpec::own`](super::OutboxSpec::own); the record implements the event's trait of
/// [`outbox`](super).
pub mod own {
use super::declaration::{Declaration, setting};
use super::{Set, Unset};
setting!(
/// The take of a record when its message reaches a subscription:
/// [`outbox::Fetch`](crate::outbox::Fetch).
///
/// # Examples
///
/// ```
/// # #[cfg(all(feature = "outbox", feature = "postgres"))]
/// # mod demo {
/// # use ruststream::OutgoingMessage;
/// # use ruststream::memory::prelude::*;
/// # use serde::{Deserialize, Serialize};
/// # use ruststream_sqlx::outbox::{Outbox, TrackedName};
/// use ruststream_sqlx::dialect::Column;
/// use ruststream_sqlx::outbox::spec::own;
/// use ruststream_sqlx::outbox::{self, OutboxSpec, OutboxTable};
/// use sqlx::{PgConnection, PgPool, Postgres};
///
/// #[derive(sqlx::FromRow)]
/// pub struct OrderEvent {
/// id: i64,
/// name: String,
/// payload: Vec<u8>,
/// }
///
/// impl OutboxTable for OrderEvent {
/// type Id = i64;
/// type Table = OutboxSpec<(own::Fetch,)>;
/// const TABLE: Self::Table = OutboxSpec::new(
/// "outbox",
/// Column::new("id"),
/// Column::new("name"),
/// Column::new("payload"),
/// )
/// .own::<own::Fetch>();
/// # fn id(&self) -> &i64 { &self.id }
/// # fn name(&self) -> &str { &self.name }
/// # fn payload(&self) -> &[u8] { &self.payload }
/// }
/// # impl outbox::Publish<Postgres> for OrderEvent {
/// # 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
/// # }
/// # }
///
/// // A record the service cancelled is not delivered: its delivery is acknowledged without the handler.
/// impl outbox::Fetch<Postgres> for OrderEvent {
/// async fn fetch(conn: &mut PgConnection, id: &i64) -> sqlx::Result<Option<Self>> {
/// sqlx::query_as("SELECT id, name, payload FROM outbox WHERE id = $1 AND NOT cancelled")
/// .bind(id)
/// .fetch_optional(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() }
/// # pub struct Orders;
/// # impl TrackedName for Orders { const NAME: &'static str = "orders"; }
/// # pub fn app(pool: PgPool) -> impl App {
/// # let tracking = Outbox::new(pool).track::<OrderEvent, Orders>();
/// # 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());
/// # })
/// # }
/// # }
/// # fn main() {}
/// ```
Fetch, OwnFetch
);
setting!(
/// The mark of an acknowledged record: [`outbox::Ack`](crate::outbox::Ack).
///
/// # Examples
///
/// ```
/// # #[cfg(all(feature = "outbox", feature = "postgres"))]
/// # mod demo {
/// # use ruststream::OutgoingMessage;
/// # use ruststream::memory::prelude::*;
/// # use serde::{Deserialize, Serialize};
/// # use ruststream_sqlx::outbox::{Outbox, TrackedName};
/// use ruststream_sqlx::dialect::Column;
/// use ruststream_sqlx::outbox::spec::own;
/// use ruststream_sqlx::outbox::{self, OutboxSpec, OutboxTable};
/// use sqlx::{PgConnection, PgPool, Postgres};
///
/// #[derive(sqlx::FromRow)]
/// pub struct OrderEvent {
/// id: i64,
/// name: String,
/// payload: Vec<u8>,
/// }
///
/// impl OutboxTable for OrderEvent {
/// type Id = i64;
/// type Table = OutboxSpec<(own::Ack,)>;
/// const TABLE: Self::Table = OutboxSpec::new(
/// "outbox",
/// Column::new("id"),
/// Column::new("name"),
/// Column::new("payload"),
/// )
/// .own::<own::Ack>();
/// # fn id(&self) -> &i64 { &self.id }
/// # fn name(&self) -> &str { &self.name }
/// # fn payload(&self) -> &[u8] { &self.payload }
/// }
/// # impl outbox::Publish<Postgres> for OrderEvent {
/// # 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
/// # }
/// # }
///
/// // An acknowledged record moves to the archive.
/// impl outbox::Ack<Postgres> for OrderEvent {
/// async fn ack(conn: &mut PgConnection, id: &i64) -> sqlx::Result<()> {
/// sqlx::query(
/// "WITH done AS (DELETE FROM outbox WHERE id = $1 RETURNING *) \
/// INSERT INTO outbox_archive SELECT * FROM done",
/// )
/// .bind(id)
/// .execute(conn)
/// .await?;
/// Ok(())
/// }
/// }
/// # #[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() }
/// # pub struct Orders;
/// # impl TrackedName for Orders { const NAME: &'static str = "orders"; }
/// # pub fn app(pool: PgPool) -> impl App {
/// # let tracking = Outbox::new(pool).track::<OrderEvent, Orders>();
/// # 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());
/// # })
/// # }
/// # }
/// # fn main() {}
/// ```
Ack, OwnAck
);
setting!(
/// What a retried record runs: [`outbox::Retry`](crate::outbox::Retry).
///
/// # Examples
///
/// ```
/// # #[cfg(all(feature = "outbox", feature = "postgres"))]
/// # mod demo {
/// # use ruststream::OutgoingMessage;
/// # use ruststream::memory::prelude::*;
/// # use serde::{Deserialize, Serialize};
/// # use ruststream_sqlx::outbox::{Outbox, TrackedName};
/// use ruststream_sqlx::dialect::Column;
/// use ruststream_sqlx::outbox::spec::own;
/// use ruststream_sqlx::outbox::{self, OutboxSpec, OutboxTable};
/// use sqlx::{PgConnection, PgPool, Postgres};
///
/// #[derive(sqlx::FromRow)]
/// pub struct OrderEvent {
/// id: i64,
/// name: String,
/// payload: Vec<u8>,
/// }
///
/// impl OutboxTable for OrderEvent {
/// type Id = i64;
/// type Table = OutboxSpec<(own::Retry,)>;
/// const TABLE: Self::Table = OutboxSpec::new(
/// "outbox",
/// Column::new("id"),
/// Column::new("name"),
/// Column::new("payload"),
/// )
/// .own::<own::Retry>();
/// # fn id(&self) -> &i64 { &self.id }
/// # fn name(&self) -> &str { &self.name }
/// # fn payload(&self) -> &[u8] { &self.payload }
/// }
/// # impl outbox::Publish<Postgres> for OrderEvent {
/// # 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
/// # }
/// # }
///
/// // The service counts how often a consumer asked for a record again.
/// impl outbox::Retry<Postgres> for OrderEvent {
/// async fn retry(conn: &mut PgConnection, id: &i64) -> sqlx::Result<()> {
/// sqlx::query("UPDATE outbox SET retries = retries + 1 WHERE id = $1")
/// .bind(id)
/// .execute(conn)
/// .await?;
/// Ok(())
/// }
/// }
/// # #[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() }
/// # pub struct Orders;
/// # impl TrackedName for Orders { const NAME: &'static str = "orders"; }
/// # pub fn app(pool: PgPool) -> impl App {
/// # let tracking = Outbox::new(pool).track::<OrderEvent, Orders>();
/// # 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());
/// # })
/// # }
/// # }
/// # fn main() {}
/// ```
Retry, OwnRetry
);
setting!(
/// The mark of a dropped record: [`outbox::Discard`](crate::outbox::Discard).
///
/// # Examples
///
/// ```
/// # #[cfg(all(feature = "outbox", feature = "postgres"))]
/// # mod demo {
/// # use ruststream::OutgoingMessage;
/// # use ruststream::memory::prelude::*;
/// # use serde::{Deserialize, Serialize};
/// # use ruststream_sqlx::outbox::{Outbox, TrackedName};
/// use ruststream_sqlx::dialect::Column;
/// use ruststream_sqlx::outbox::spec::own;
/// use ruststream_sqlx::outbox::{self, OutboxSpec, OutboxTable};
/// use sqlx::{PgConnection, PgPool, Postgres};
///
/// #[derive(sqlx::FromRow)]
/// pub struct OrderEvent {
/// id: i64,
/// name: String,
/// payload: Vec<u8>,
/// }
///
/// impl OutboxTable for OrderEvent {
/// type Id = i64;
/// type Table = OutboxSpec<(own::Discard,)>;
/// const TABLE: Self::Table = OutboxSpec::new(
/// "outbox",
/// Column::new("id"),
/// Column::new("name"),
/// Column::new("payload"),
/// )
/// .own::<own::Discard>();
/// # fn id(&self) -> &i64 { &self.id }
/// # fn name(&self) -> &str { &self.name }
/// # fn payload(&self) -> &[u8] { &self.payload }
/// }
/// # impl outbox::Publish<Postgres> for OrderEvent {
/// # 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
/// # }
/// # }
///
/// // A dropped record moves to a table the operators review.
/// impl outbox::Discard<Postgres> for OrderEvent {
/// async fn discard(conn: &mut PgConnection, id: &i64) -> sqlx::Result<()> {
/// sqlx::query(
/// "WITH dropped AS (DELETE FROM outbox WHERE id = $1 RETURNING *) \
/// INSERT INTO outbox_dropped SELECT * FROM dropped",
/// )
/// .bind(id)
/// .execute(conn)
/// .await?;
/// Ok(())
/// }
/// }
/// # #[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() }
/// # pub struct Orders;
/// # impl TrackedName for Orders { const NAME: &'static str = "orders"; }
/// # pub fn app(pool: PgPool) -> impl App {
/// # let tracking = Outbox::new(pool).track::<OrderEvent, Orders>();
/// # 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());
/// # })
/// # }
/// # }
/// # fn main() {}
/// ```
Discard, OwnDiscard
);
setting!(
/// The selection of the unprocessed records at startup:
/// [`outbox::Recover`](crate::outbox::Recover).
///
/// # Examples
///
/// ```
/// # #[cfg(all(feature = "outbox", feature = "postgres"))]
/// # mod demo {
/// # use ruststream::OutgoingMessage;
/// # use ruststream::memory::prelude::*;
/// # use serde::{Deserialize, Serialize};
/// # use ruststream_sqlx::outbox::{Outbox, TrackedName};
/// use ruststream_sqlx::dialect::Column;
/// use ruststream_sqlx::outbox::spec::own;
/// use ruststream_sqlx::outbox::{self, OutboxSpec, OutboxTable};
/// use sqlx::{PgConnection, PgPool, Postgres};
///
/// #[derive(sqlx::FromRow)]
/// pub struct OrderEvent {
/// id: i64,
/// name: String,
/// payload: Vec<u8>,
/// }
///
/// impl OutboxTable for OrderEvent {
/// type Id = i64;
/// type Table = OutboxSpec<(own::Recover,)>;
/// const TABLE: Self::Table = OutboxSpec::new(
/// "outbox",
/// Column::new("id"),
/// Column::new("name"),
/// Column::new("payload"),
/// )
/// .own::<own::Recover>();
/// # fn id(&self) -> &i64 { &self.id }
/// # fn name(&self) -> &str { &self.name }
/// # fn payload(&self) -> &[u8] { &self.payload }
/// }
/// # impl outbox::Publish<Postgres> for OrderEvent {
/// # 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
/// # }
/// # }
///
/// // The startup republish sends the records oldest first.
/// impl outbox::Recover<Postgres> for OrderEvent {
/// async fn recover(conn: &mut PgConnection, name: &str) -> sqlx::Result<Vec<Self>> {
/// sqlx::query_as("SELECT id, name, payload FROM outbox WHERE name = $1 ORDER BY id")
/// .bind(name)
/// .fetch_all(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() }
/// # pub struct Orders;
/// # impl TrackedName for Orders { const NAME: &'static str = "orders"; }
/// # pub fn app(pool: PgPool) -> impl App {
/// # let tracking = Outbox::new(pool).track::<OrderEvent, Orders>();
/// # 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());
/// # })
/// # }
/// # }
/// # fn main() {}
/// ```
Recover, OwnRecover
);
}
/// An event a record runs itself: a marker of [`own`], which
/// [`OutboxSpec::own`](super::OutboxSpec::own) takes.
#[diagnostic::on_unimplemented(
message = "`{Self}` is not an event of an outbox record",
label = "not an outbox event",
note = "name one of `outbox::spec::own::{{Fetch, Ack, Retry, Discard, Recover}}`"
)]
pub trait OwnEvent: Declaration {}
impl OwnEvent for own::Fetch {}
impl OwnEvent for own::Ack {}
impl OwnEvent for own::Retry {}
impl OwnEvent for own::Discard {}
impl OwnEvent for own::Recover {}