shep-client 0.25.2

Async client for the shep daemon: typed requests, event subscriptions, and starting a shepherd that is not running yet
Documentation
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
//! [`DogRuntime`], the connection and configuration a dog starts with.

use std::time::Duration;

use serde::de::DeserializeOwned;
use shep_core::paths::ShepPaths;
use shep_core::protocol::DogSectionToml;

use super::long_action::{self, ActionDone, ActionId, LongActionError, Waits};
use super::{DogIdentity, SectionError, ShepherdError, link, parse_section};
use crate::ReconnectingClient;

/// A dog's connection to its shepherd, and its own section of `dogs.toml`.
///
/// `Debug` prints the section's length and never its text, which routinely
/// carries a webhook token.
///
/// # Example
///
/// ```no_run
/// use shep_client::dogs::{DogIdentity, DogRuntime};
/// # use shep_client::shep_core::paths::ShepPaths;
///
/// #[derive(Default, serde::Deserialize)]
/// struct Settings {
///     interval: Option<String>,
/// }
///
/// # async fn dog(paths: ShepPaths) -> Result<(), Box<dyn core::error::Error>> {
/// let env = |key: &str| std::env::var(key).ok();
/// let identity = DogIdentity::from_env(&env, "log-rotate");
/// let runtime = DogRuntime::start(identity, paths).await?;
/// let settings: Settings = runtime.config()?;
/// # let _ = settings.interval;
/// # Ok(())
/// # }
/// # let _ = dog;
/// ```
#[derive(Debug)]
pub struct DogRuntime {
    client: ReconnectingClient,
    section: DogSectionToml,
    paths: ShepPaths,
    identity: DogIdentity,
    waits: Waits,
}

impl DogRuntime {
    /// Connects as `identity` and asks for its section.
    ///
    /// Over the connection the shepherd spawned this process holding, when
    /// there is one, and through `paths`' socket when there is not or it
    /// is lost. The shepherd serves the first from spawn, so a dog started
    /// ahead of the muster restore connects during it.
    ///
    /// # Errors
    ///
    /// - [`ShepherdError::Connect`]: no shepherd answered, or it refused
    ///   the handshake.
    /// - [`ShepherdError::Request`]: the section request failed, or was
    ///   answered with something other than the section.
    pub async fn start(identity: DogIdentity, paths: ShepPaths) -> Result<Self, ShepherdError> {
        let client = link::connect(&identity, &paths.socket).await?;
        let section = client.dog_config(identity.section()).await?;
        Ok(Self {
            client,
            section,
            paths,
            identity,
            waits: Waits::default(),
        })
    }

    /// This dog's section parsed into `T`, or `T::default()` when the
    /// shepherd had none for it.
    ///
    /// # Errors
    ///
    /// [`SectionError`] when the section does not fit `T`, naming the line.
    pub fn config<T>(&self) -> Result<T, SectionError>
    where
        T: DeserializeOwned + Default,
    {
        parse_section(self.identity.section(), self.section.as_str())
    }

    /// Publishes `payload` as this dog's event `kind`, which subscribers
    /// holding `events:dog.<name>` hear as `dog.<name>.<kind>`.
    ///
    /// `kind` must be one the dog declared with
    /// [`Probe::event`](super::Probe::event). The name is the connection's,
    /// so a dog can publish only under its own, and the payload is opaque
    /// to the shepherd.
    ///
    /// # Errors
    ///
    /// [`ShepherdError::Request`]: the shepherd refused (naming `kind` when
    /// it was not declared), or the request failed.
    pub async fn publish(
        &self,
        kind: &str,
        payload: serde_json::Value,
    ) -> Result<(), ShepherdError> {
        Ok(self.client.publish_event(kind, payload).await?)
    }

    /// Reports the result of the long action `id` as this dog's
    /// [`DONE_KIND`](super::DONE_KIND) event: `Ok` carries what the work
    /// produced, `Err` what went wrong.
    ///
    /// Holders of `events:dog.<name>` all hear it, and the caller picks its
    /// own out by id. The dog declares [`DONE_KIND`](super::DONE_KIND) with
    /// [`Probe::event`](super::Probe::event) first, or the shepherd refuses.
    /// So does a dog whose name contains a dot: it cannot publish at all, so
    /// no caller will hear its results.
    ///
    /// # Errors
    ///
    /// As [`Self::publish`].
    pub async fn finish_action(
        &self,
        id: &ActionId,
        outcome: Result<serde_json::Value, String>,
    ) -> Result<(), ShepherdError> {
        self.publish(super::DONE_KIND, long_action::done_payload(id, outcome))
            .await
    }

    /// Triggers `action` on the dog `callee` and waits up to `timeout` for
    /// the result of the long action it answers with.
    ///
    /// Subscribes to `callee`'s results before it triggers, so a result
    /// published at once is not missed. Waits share one subscription, sent
    /// only when a callee joins it, which replaces any other this connection
    /// holds. The caller needs `events:dog.<callee>` and
    /// `dogs.trigger:<callee>`. The timeout starts once the callee answers.
    ///
    /// # Errors
    ///
    /// [`LongActionError::NotGranted`] names the missing grant. The other
    /// variants say why no result arrived.
    pub async fn trigger_and_wait(
        &self,
        callee: &str,
        action: &str,
        params: Option<&str>,
        timeout: Duration,
    ) -> Result<ActionDone, LongActionError> {
        long_action::trigger_and_wait(&self.waits, &self.client, callee, action, params, timeout)
            .await
    }

    /// The connection, which re-establishes itself across a handover.
    #[must_use]
    pub fn client(&self) -> &ReconnectingClient {
        &self.client
    }

    /// The section's text as the shepherd served it, empty when there is
    /// none.
    #[must_use]
    pub fn section(&self) -> &DogSectionToml {
        &self.section
    }

    /// The `$SHEP_HOME` layout this dog was started with.
    #[must_use]
    pub fn paths(&self) -> &ShepPaths {
        &self.paths
    }

    /// Which dog this is.
    #[must_use]
    pub fn identity(&self) -> &DogIdentity {
        &self.identity
    }

    /// The connection alone, for a dog that hands it to a task of its own.
    #[must_use]
    pub fn into_client(self) -> ReconnectingClient {
        self.client
    }
}

#[cfg(test)]
mod tests {
    use std::path::Path;
    use std::time::Duration;

    use shep_core::protocol::{Request, Response, SelectorSpec};

    use super::*;
    use crate::testing::{control_address, fake_daemon, sample_ack, serve_one_request};

    /// A layout rooted at `dir`, with its socket where the fake daemon bound.
    fn paths_in(dir: &Path) -> ShepPaths {
        let mut paths = ShepPaths::resolve(&|_| None, dir);
        paths.socket = control_address(dir);
        paths
    }

    /// Starts a runtime against a fake that serves `section` once.
    async fn started(dir: &Path, section: &str) -> (DogRuntime, Request) {
        let paths = paths_in(dir);
        let served = serve_one_request(
            &paths.socket,
            sample_ack(),
            Response::DogSection {
                toml: section.to_owned().into(),
            },
        )
        .await;
        let runtime = tokio::time::timeout(
            Duration::from_secs(5),
            DogRuntime::start(DogIdentity::named("bark"), paths),
        )
        .await
        .expect("DogRuntime::start hung instead of connecting")
        .unwrap();
        let asked = tokio::time::timeout(Duration::from_secs(5), served)
            .await
            .expect("the request reached the fake daemon")
            .unwrap();
        (runtime, asked.body)
    }

    #[tokio::test]
    async fn a_dog_asks_for_its_own_section_by_name() {
        let dir = tempfile::tempdir().unwrap();
        let (runtime, asked) = started(dir.path(), "poll = \"1m\"\n").await;

        assert_eq!(
            asked,
            Request::DogConfigResolved {
                name: "bark".to_owned()
            }
        );
        assert_eq!(runtime.section().as_str(), "poll = \"1m\"\n");
        assert_eq!(runtime.identity().section(), "bark");
    }

    /// An unnamed dog still reads its default section, and says nothing
    /// about who it is.
    /// A runtime against a shepherd that answers any request, and the
    /// shepherd's record of what it was sent.
    #[tokio::test]
    async fn publish_sends_the_dogs_event_and_payload_to_the_shepherd() {
        use crate::testing::{Handshake, fake_daemon_across_handovers};

        let dir = tempfile::tempdir().unwrap();
        let paths = paths_in(dir.path());
        let shepherd =
            fake_daemon_across_handovers(&paths.socket, vec![Handshake::Accept(sample_ack())]);
        let runtime = tokio::time::timeout(
            Duration::from_secs(5),
            DogRuntime::start(DogIdentity::named("bark"), paths),
        )
        .await
        .expect("DogRuntime::start hung instead of connecting")
        .unwrap();
        let payload = serde_json::json!({ "sent": 3 });

        runtime.publish("exported", payload.clone()).await.unwrap();

        let sent: Vec<Request> = shepherd
            .envelopes()
            .into_iter()
            .map(|(_, envelope)| envelope.body)
            .collect();
        assert!(
            sent.contains(&Request::PublishEvent {
                event: "exported".to_owned(),
                payload
            }),
            "{sent:?}"
        );
    }

    /// A callee's `action_timeout` reaches 58s, so a trigger sent with the
    /// client's 5s default would abandon an id the callee is still minting.
    #[tokio::test]
    async fn the_trigger_waits_as_long_as_any_other_trigger_does() {
        use crate::testing::{Handshake, fake_daemon_across_handovers};

        let dir = tempfile::tempdir().unwrap();
        let paths = paths_in(dir.path());
        let shepherd =
            fake_daemon_across_handovers(&paths.socket, vec![Handshake::Accept(sample_ack())]);
        let runtime = tokio::time::timeout(
            Duration::from_secs(5),
            DogRuntime::start(DogIdentity::named("bark"), paths),
        )
        .await
        .expect("DogRuntime::start hung instead of connecting")
        .unwrap();

        // The fake answers `Pong`, which is no list of replies: the result
        // is beside the point, the envelope is the subject.
        let _ = runtime
            .trigger_and_wait("builder", "build", Some("p"), Duration::from_millis(50))
            .await;

        let trigger = shepherd
            .envelopes()
            .into_iter()
            .map(|(_, envelope)| envelope)
            .find(|envelope| matches!(envelope.body, Request::Trigger { .. }))
            .expect("the shepherd was sent a Trigger");
        assert_eq!(
            trigger.body,
            Request::Trigger {
                selector: SelectorSpec::Name("builder".to_owned()),
                action: "build".to_owned(),
                params: Some("p".to_owned()),
            }
        );
        assert_eq!(
            trigger.deadline_ms,
            Some(u64::try_from(crate::TRIGGER_DEADLINE.as_millis()).unwrap())
        );
    }

    /// How many `Subscribe`s each generation of `shepherd` was sent.
    fn subscribes(shepherd: &crate::testing::Handovers) -> Vec<u32> {
        shepherd
            .envelopes()
            .into_iter()
            .filter(|(_, envelope)| matches!(envelope.body, Request::Subscribe { .. }))
            .map(|(generation, _)| generation)
            .collect()
    }

    /// A second `Subscribe` makes the shepherd replace the first one's
    /// forwarder and drop the event it holds in flight, so a wait whose topic
    /// the connection already hears does not send one.
    #[tokio::test]
    async fn a_wait_on_a_topic_the_connection_already_hears_sends_no_subscribe() {
        use crate::testing::{Handshake, fake_daemon_across_handovers};

        let dir = tempfile::tempdir().unwrap();
        let paths = paths_in(dir.path());
        let shepherd =
            fake_daemon_across_handovers(&paths.socket, vec![Handshake::Accept(sample_ack())]);
        let runtime = DogRuntime::start(DogIdentity::named("bark"), paths)
            .await
            .unwrap();
        let wait = || runtime.trigger_and_wait("builder", "build", None, Duration::from_millis(50));

        let _ = tokio::join!(wait(), wait());
        let _ = wait().await;

        assert_eq!(subscribes(&shepherd), [1]);
    }

    /// The shepherd's filter belongs to a connection, so a successor starts
    /// with none and the next wait has to ask again.
    #[tokio::test]
    async fn a_wait_after_a_handover_subscribes_again() {
        use crate::testing::{Handshake, fake_daemon_across_handovers};

        let dir = tempfile::tempdir().unwrap();
        let paths = paths_in(dir.path());
        let shepherd = fake_daemon_across_handovers(
            &paths.socket,
            vec![
                Handshake::Accept(sample_ack()),
                Handshake::Accept(sample_ack()),
            ],
        );
        let runtime = DogRuntime::start(DogIdentity::named("bark"), paths)
            .await
            .unwrap();
        let wait = || runtime.trigger_and_wait("builder", "build", None, Duration::from_millis(50));
        let _ = wait().await;

        shepherd.cut().await;
        tokio::time::timeout(Duration::from_secs(5), async {
            while shepherd.accepted() < 2 {
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
            runtime
                .client()
                .connected_within(Duration::from_secs(5))
                .await
        })
        .await
        .expect("the client reconnects")
        .unwrap();
        let _ = wait().await;

        assert_eq!(subscribes(&shepherd), [1, 2]);
    }

    #[tokio::test]
    async fn an_unnamed_dog_reads_its_default_section_anonymously() {
        let dir = tempfile::tempdir().unwrap();
        let paths = paths_in(dir.path());
        let hello = fake_daemon(&paths.socket, Ok(sample_ack())).await;
        let identity = DogIdentity::from_env(&|_| None, "log-rotate");

        // The fake closes after the handshake, so the request fails; the
        // hello is the frame under test.
        let started =
            tokio::time::timeout(Duration::from_secs(5), DogRuntime::start(identity, paths))
                .await
                .expect("DogRuntime::start hung instead of connecting");

        assert!(matches!(started, Err(ShepherdError::Request(_))));
        let hello = tokio::time::timeout(Duration::from_secs(5), hello)
            .await
            .expect("the fake daemon read the hello")
            .unwrap();
        assert_eq!(hello.dog_name, None);
    }

    #[tokio::test]
    async fn a_section_that_does_not_fit_is_refused_rather_than_defaulted() {
        #[derive(Debug, Default, serde::Deserialize, PartialEq)]
        #[serde(deny_unknown_fields, default)]
        struct Settings {
            port: u16,
        }
        let dir = tempfile::tempdir().unwrap();
        let (runtime, _) = started(dir.path(), "port = \"nine thousand\"\n").await;

        let err = runtime.config::<Settings>().unwrap_err();
        assert_eq!(err.section(), "bark");
        assert_eq!(err.line(), Some(1));
    }

    #[tokio::test]
    async fn no_shepherd_is_a_connect_error() {
        let dir = tempfile::tempdir().unwrap();
        let started = DogRuntime::start(DogIdentity::named("bark"), paths_in(dir.path())).await;
        let err = started.unwrap_err();
        assert!(matches!(err, ShepherdError::Connect(_)), "{err}");
        assert_eq!(err.exit_code(), shep_core::exit::DAEMON_UNREACHABLE);
    }

    /// `client`'s own `Debug` names this test's tempdir, so only the
    /// section's part is pinned exactly.
    #[tokio::test]
    async fn debug_never_prints_the_section() {
        let secret = "https://hooks.example.com/services/T00/B00/super-secret-token";
        let section = format!("webhook = \"{secret}\"\n");
        let dir = tempfile::tempdir().unwrap();
        let (runtime, _) = started(dir.path(), &section).await;

        let debug = format!("{runtime:?}");
        assert!(!debug.contains(secret), "{debug}");
        assert!(
            debug.contains(&format!(
                "section: DogSectionToml(<{} bytes>)",
                section.len()
            )),
            "{debug}"
        );
    }
}