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
//! How the columns of a queue row reach a delivery: its key, its attempt.
/// The type of a field playing `partition_key`: the bytes the delivery's key lends.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "postgres")]
/// # mod demo {
/// use ruststream_sqlx::prelude::*;
/// use serde::Deserialize;
/// use sqlx::PgPool;
///
/// #[derive(Inbox, sqlx::FromRow)]
/// #[inbox(table = "order_jobs")]
/// pub struct OrderJob {
/// #[field(id, generated)]
/// id: i64,
/// // A `String` lends its bytes as the delivery's key; an empty `Option` gives none.
/// #[field(partition_key)]
/// customer: Option<String>,
/// #[field(payload)]
/// payload: Vec<u8>,
/// }
///
/// #[derive(Deserialize)]
/// struct Order {
/// id: u64,
/// }
///
/// #[subscriber(InboxQueue::<OrderJob>::new("orders"))]
/// async fn fulfil(order: &Order) -> HandlerOutcome {
/// tracing::info!(order.id, "fulfilling");
/// HandlerOutcome::ack()
/// }
///
/// pub fn app(pool: PgPool) -> RustStream {
/// RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(SqlxBroker::new(pool), |b| {
/// // Four workers, and the orders of one customer keep their order on one of them.
/// b.include(fulfil.workers_by_key(nonzero!(4)));
/// })
/// }
/// # }
/// # fn main() {}
/// ```
keys!;
/// The type of a field playing `attempt`: the delivery's redelivery count.
///
/// The first delivery of a row reads 1, so a table's `attempt` column starts at 1. A negative
/// value reads as 0.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "postgres")]
/// # mod demo {
/// use ruststream_sqlx::prelude::*;
/// use serde::Deserialize;
/// use sqlx::PgPool;
///
/// #[derive(Inbox, sqlx::FromRow)]
/// #[inbox(table = "charge_jobs")]
/// pub struct ChargeJob {
/// #[field(id, generated)]
/// id: i64,
/// // sqlx has no unsigned integers on Postgres, so the column is a `SMALLINT DEFAULT 1`.
/// #[field(attempt, generated)]
/// attempt: i16,
/// #[field(payload)]
/// payload: Vec<u8>,
/// }
///
/// #[derive(Deserialize)]
/// struct Charge {
/// order: u64,
/// }
///
/// # async fn try_charge(_: &Charge) -> bool { true }
/// #[subscriber(InboxQueue::<ChargeJob>::new("charges"))]
/// async fn charge(request: &Charge, Ctx(attempt): Ctx<keys::Attempt>) -> HandlerOutcome {
/// tracing::info!(request.order, ?attempt, "charging");
/// if try_charge(request).await { HandlerOutcome::ack() } else { HandlerOutcome::retry() }
/// }
///
/// pub fn app(pool: PgPool) -> RustStream {
/// RustStream::new(AppInfo::new("payments", "0.1.0")).with_broker(SqlxBroker::new(pool), |b| {
/// // The attempt counts the deliveries: the fifth failure moves the row into
/// // `failed_charge_jobs`, a table with the same columns.
/// b.include(charge).max_attempts(nonzero!(5u32)).dead_letter("failed_charge_jobs");
/// })
/// }
/// # }
/// # fn main() {}
/// ```
attempts!;