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
//! The file transport's typed contexts and their compile-time keys.
//!
//! A stream file is a replayable log, so a handler running on it can reposition its own
//! subscription. The delivery's position and the subscription's reposition handle travel as
//! fields of a context the runtime builds per delivery, and handlers read them by key -
//! [`Ctx`](ruststream::runtime::Ctx) on the attribute path, `ctx.context(..)` on the manual one.
//!
//! Standard input has no retained log, so nothing here applies to it: its deliveries carry no
//! reposition handle, which is what makes a stdio mount of a seeking handler a compile error.
//!
//! The [Position](https://powersemmi.github.io/ruststream-sea-file/latest/components/positions/position/),
//! [Seek](https://powersemmi.github.io/ruststream-sea-file/latest/components/positions/seek/) and
//! [FileBatchContext](https://powersemmi.github.io/ruststream-sea-file/latest/components/positions/batch-context/)
//! pages show them in a service.
use ;
use crateFilePosition;
use crate;
/// The file transport's per-delivery context: this delivery's position in the stream file, and
/// the subscription's own seeker.
///
/// The runtime builds one per delivery, and handlers read its fields by key - [`Position`] and
/// [`SeekHandle`]. A handler that reads both, or that reads them on the manual path, names this
/// type as its context.
///
/// # Examples
///
/// ```
/// use ruststream_sea_file::file::prelude::*;
/// use serde::Deserialize;
///
/// #[derive(Debug, Deserialize)]
/// struct Job {
/// id: u64,
/// }
///
/// #[subscriber(FileStream::new("jobs"))]
/// async fn work(job: &Job, ctx: &mut Context<'_, FileContext>) -> HandlerOutcome {
/// if job.id == 0 && ctx.context(SeekHandle).seek(FilePosition::end()).await.is_err() {
/// return HandlerOutcome::retry();
/// }
/// tracing::info!(job.id, position = ?ctx.context(Position), "worked");
/// HandlerOutcome::ack()
/// }
///
/// #[ruststream::app]
/// fn app() -> impl App {
/// RustStream::new(AppInfo::new("jobs", "0.1.0"))
/// .with_broker(FileBroker::new("/tmp/jobs.ss"), |b| {
/// b.include(work);
/// })
/// }
/// ```
/// The file transport's subscription-scoped batch context: the subscription's own seeker, shared
/// by every delivery of the batch.
///
/// The runtime builds one per dispatched batch from the batch's first delivery, and a batch body
/// reads it by key - [`SeekHandle`]. Per-delivery data (a [`Position`]) has no place here: a
/// batch spans many deliveries, so the position a body reacts to rides the elements themselves,
/// and keeping this a separate type from [`FileContext`] is what rejects a batch body asking for
/// per-delivery fields at compile time.
///
/// # Examples
///
/// The batch settles first, so a seek from its body applies to the batches after it:
///
/// ```
/// use ruststream_sea_file::file::prelude::*;
/// use serde::Deserialize;
///
/// #[derive(Debug, Deserialize)]
/// struct Job {
/// id: u64,
/// resume_at: Option<u64>,
/// }
///
/// #[subscriber(FileStream::new("jobs"), start_at(FilePosition::beginning()))]
/// async fn drain(jobs: &[Job], ctx: &mut Context<'_, FileBatchContext>) -> HandlerOutcome {
/// tracing::info!(jobs = jobs.len(), "drained");
/// let Some(resume_at) = jobs.iter().find_map(|job| job.resume_at) else {
/// return HandlerOutcome::ack();
/// };
/// if let Err(error) = ctx
/// .context(SeekHandle)
/// .seek(FilePosition::sequence(resume_at))
/// .await
/// {
/// tracing::warn!(resume_at, %error, "seek refused");
/// }
/// HandlerOutcome::ack()
/// }
///
/// #[ruststream::app]
/// fn app() -> impl App {
/// RustStream::new(AppInfo::new("worker", "0.1.0"))
/// .with_broker(FileBroker::new("/tmp/jobs.ss"), |b| {
/// b.include(drain.batch(nonzero!(3)));
/// })
/// }
/// ```
///
/// A batch body that asks for a per-delivery position does not compile: [`Position`] is a key of
/// [`FileContext`] alone.
///
/// ```compile_fail,E0277
/// use ruststream_sea_file::file::prelude::*;
/// use serde::Deserialize;
///
/// #[derive(Debug, Deserialize)]
/// struct Job {
/// id: u64,
/// }
///
/// #[subscriber(FileStream::new("jobs"))]
/// async fn drain(jobs: &[Job], ctx: &mut Context<'_, FileBatchContext>) -> HandlerOutcome {
/// let first: FilePosition = ctx.context(Position);
/// tracing::info!(jobs = jobs.len(), ?first, "drained");
/// HandlerOutcome::ack()
/// }
/// ```
/// The key reading this delivery's [`FilePosition`] out of [`FileContext`].
///
/// The value is the pinned form: seeking back to it redelivers exactly this message.
///
/// # Examples
///
/// ```
/// use ruststream_sea_file::file::prelude::*;
/// # #[derive(serde::Deserialize)]
/// # struct Job { id: u64 }
///
/// #[subscriber(FileStream::new("jobs"))]
/// async fn audit(job: &Job, Ctx(at): Ctx<Position>) -> HandlerOutcome {
/// tracing::info!(job.id, position = ?at, "audited");
/// HandlerOutcome::ack()
/// }
/// ```
;
/// The key reading the subscription's [`FileSeeker`] out of [`FileContext`] or
/// [`FileBatchContext`]: the reposition handle, resolved once when the subscription opens and
/// carried by every delivery.
///
/// # Examples
///
/// ```
/// use ruststream_sea_file::file::prelude::*;
/// # #[derive(serde::Deserialize)]
/// # struct Job { id: u64, poisoned: bool }
///
/// /// Skips to the live tail when the producer marks a poisoned region.
/// #[subscriber(FileStream::new("jobs"))]
/// async fn work(job: &Job, Ctx(seeker): Ctx<SeekHandle>) -> HandlerOutcome {
/// if job.poisoned && seeker.seek(FilePosition::end()).await.is_err() {
/// return HandlerOutcome::retry();
/// }
/// HandlerOutcome::ack()
/// }
/// ```
///
/// Standard input keeps no log to move within, so a handler that reads the key does not compile
/// against [`StdioBroker`](crate::StdioBroker):
///
/// ```compile_fail,E0277
/// use ruststream_sea_file::prelude::*;
/// use serde::Deserialize;
///
/// #[derive(Debug, Deserialize)]
/// struct Job {
/// id: u64,
/// }
///
/// #[subscriber("jobs")]
/// async fn work(job: &Job, Ctx(seeker): Ctx<SeekHandle>) -> HandlerOutcome {
/// if job.id == 0 && seeker.seek(FilePosition::end()).await.is_err() {
/// return HandlerOutcome::retry();
/// }
/// HandlerOutcome::ack()
/// }
///
/// #[ruststream::app]
/// fn app() -> impl App {
/// RustStream::new(AppInfo::new("pipeline", "0.1.0")).with_broker(StdioBroker::new(), |b| {
/// b.include(work);
/// })
/// }
/// ```
;