shep-daemon 0.22.0

The supervision engine behind shep: process spawning, restart policy, the log plane, watch and cron restarts, and the RPC server
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
//! A wildcard passes a disabled app by and a name still reaches it, and
//! `enable` and `disable` take any selector.

use super::*;

fn by_name(name: &str) -> SelectorSpec {
    SelectorSpec::Name(name.to_string())
}

fn in_fold(name: &str, fold: &str) -> AppConfig {
    let mut app = AppConfig::minimal(name, "./srv");
    app.fold = Some(fold.to_string());
    app
}

async fn start(h: &Harness, id: u64, apps: Vec<AppConfig>) {
    let reply = reply_of(dispatch(envelope(id, Request::Start { apps }), &h.ctx).await);
    assert!(reply.result.is_ok(), "fixture start failed: {reply:?}");
}

async fn send(h: &Harness, id: u64, body: Request) -> Reply {
    reply_of(dispatch(envelope(id, body), &h.ctx).await)
}

async fn disable(h: &Harness, id: u64, selector: SelectorSpec) -> Vec<ProcessInfo> {
    match send(h, id, Request::Disable { selector }).await.result {
        Ok(Response::Disabled(infos)) => infos,
        other => panic!("expected Disabled, got {other:?}"),
    }
}

async fn enable(h: &Harness, id: u64, selector: SelectorSpec) -> Reply {
    send(h, id, Request::Enable { selector }).await
}

fn names(infos: &[ProcessInfo]) -> Vec<&str> {
    let mut names: Vec<&str> = infos.iter().map(|info| info.name.as_str()).collect();
    names.sort_unstable();
    names.dedup();
    names
}

/// A flock of `api` (disabled), `web` and `worker`, all started.
async fn flock_with_api_disabled(h: &Harness) {
    start(
        h,
        1,
        vec![
            AppConfig::minimal("api", "./srv"),
            AppConfig::minimal("web", "./srv"),
            AppConfig::minimal("worker", "./srv"),
        ],
    )
    .await;
    disable(h, 2, by_name("api")).await;
}

/// Fails if the supervisor's matching stops passing a disabled app by: `stop
/// all` would answer a row for `api` too.
#[tokio::test(start_paused = true)]
async fn stop_all_skips_a_disabled_app() {
    let h = harness(vec![ProcScript::never_exits(); 3]);
    flock_with_api_disabled(&h).await;

    let Ok(Response::Stopped(infos)) = send(
        &h,
        3,
        Request::Stop {
            selector: SelectorSpec::All,
        },
    )
    .await
    .result
    else {
        panic!("expected Stopped")
    };

    assert_eq!(names(&infos), ["web", "worker"]);
}

#[tokio::test(start_paused = true)]
async fn delete_all_skips_a_disabled_app_and_leaves_it_in_the_flock() {
    let h = harness(vec![ProcScript::never_exits(); 3]);
    flock_with_api_disabled(&h).await;

    let reply = send(
        &h,
        3,
        Request::Delete {
            selector: SelectorSpec::All,
        },
    )
    .await;
    let Ok(Response::Deleted(ids)) = reply.result else {
        panic!("expected Deleted, got {reply:?}")
    };

    assert_eq!(ids.len(), 2, "{ids:?}");
    let flock = list_flock(&h.ctx, 4).await;
    assert_eq!(names(&flock), ["api"]);
    assert_eq!(flock[0].disabled, Some(true));
}

#[tokio::test(start_paused = true)]
async fn a_regex_and_a_fold_pass_a_disabled_app_by() {
    let h = harness(vec![ProcScript::never_exits(); 3]);
    start(
        &h,
        1,
        vec![
            in_fold("api", "web"),
            in_fold("site", "web"),
            AppConfig::minimal("worker", "./srv"),
        ],
    )
    .await;
    disable(&h, 2, by_name("api")).await;

    let Ok(Response::Stopped(by_fold)) = send(
        &h,
        3,
        Request::Stop {
            selector: SelectorSpec::Fold("web".to_string()),
        },
    )
    .await
    .result
    else {
        panic!("expected Stopped")
    };
    assert_eq!(names(&by_fold), ["site"]);

    let regex = SelectorSpec::Regex("^a".to_string());
    let missed = send(&h, 4, Request::Stop { selector: regex }).await;
    assert_eq!(
        missed.result.unwrap_err().code,
        RpcErrorCode::NotFound,
        "`^a` only matches the disabled `api`"
    );
}

/// Fails if the sweep's condition also gates an exact selector: naming
/// `api` must still reach it for every verb that is not a spawn.
#[tokio::test(start_paused = true)]
async fn naming_a_disabled_app_still_reaches_stop_signal_describe_and_delete() {
    let h = harness(vec![ProcScript::never_exits(); 3]);
    flock_with_api_disabled(&h).await;

    let Ok(Response::Stopped(stopped)) = send(
        &h,
        3,
        Request::Stop {
            selector: by_name("api"),
        },
    )
    .await
    .result
    else {
        panic!("expected Stopped")
    };
    assert_eq!(names(&stopped), ["api"]);

    let Ok(Response::Signalled(rows)) = send(
        &h,
        4,
        Request::Signal {
            selector: by_name("api"),
            signal: "TERM".to_string(),
            lamb: None,
        },
    )
    .await
    .result
    else {
        panic!("expected Signalled")
    };
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0].name, "api");

    let described = describe(&h.ctx, 5, SelectorSpec::All).await;
    assert_eq!(names(&described), ["api", "web", "worker"]);

    let Ok(Response::Deleted(deleted)) = send(
        &h,
        6,
        Request::Delete {
            selector: by_name("api"),
        },
    )
    .await
    .result
    else {
        panic!("expected Deleted")
    };
    assert_eq!(deleted.len(), 1);
    assert_eq!(names(&list_flock(&h.ctx, 7).await), ["web", "worker"]);
}

#[tokio::test(start_paused = true)]
async fn disable_takes_a_fold_and_disables_every_app_in_it() {
    let h = harness(vec![ProcScript::never_exits(); 3]);
    start(
        &h,
        1,
        vec![
            in_fold("api", "web"),
            in_fold("site", "web"),
            AppConfig::minimal("worker", "./srv"),
        ],
    )
    .await;

    let disabled = disable(&h, 2, SelectorSpec::Fold("web".to_string())).await;

    assert_eq!(names(&disabled), ["api", "site"]);
    let flock = list_flock(&h.ctx, 3).await;
    for info in &flock {
        let parked = info.name != "worker";
        assert_eq!(info.disabled, parked.then_some(true), "{info:?}");
        assert_eq!(info.status == ProcStatus::Stopped, parked, "{info:?}");
    }
}

#[tokio::test(start_paused = true)]
async fn disable_all_takes_every_enabled_sheep_and_no_dog() {
    let h = harness(vec![ProcScript::never_exits(); 3]);
    start(&h, 1, vec![AppConfig::minimal("api", "./srv")]).await;
    let dog = enable_dog(&h.ctx, 2, "metrics").await;

    let disabled = disable(&h, 3, SelectorSpec::All).await;

    assert_eq!(names(&disabled), ["api"]);
    let flock = list_flock(&h.ctx, 4).await;
    let dog_row = flock.iter().find(|info| info.id == dog.id).unwrap();
    assert_eq!(dog_row.status, ProcStatus::Online, "{dog_row:?}");
    assert_eq!(dog_row.disabled, None);
}

/// Fails if `reset`'s sweep skips a parked app, as the lifecycle verbs do,
/// or reaches a dog it was not asked for by name.
#[tokio::test(start_paused = true)]
async fn reset_all_reaches_a_parked_app_and_passes_a_dog_by() {
    let h = harness(vec![ProcScript::never_exits(); 4]);
    flock_with_api_disabled(&h).await;
    let dog = enable_dog(&h.ctx, 4, "metrics").await;

    let Ok(Response::Reset(swept)) = send(
        &h,
        5,
        Request::Reset {
            selector: SelectorSpec::All,
        },
    )
    .await
    .result
    else {
        panic!("expected Reset")
    };
    assert_eq!(names(&swept), ["api", "web", "worker"]);

    let Ok(Response::Reset(named)) = send(
        &h,
        6,
        Request::Reset {
            selector: by_name("metrics"),
        },
    )
    .await
    .result
    else {
        panic!("expected Reset")
    };
    assert_eq!(
        named.iter().map(|info| info.id).collect::<Vec<_>>(),
        [dog.id]
    );
}

/// Fails if `enable`'s sweep uses the ordinary condition: it would find no
/// disabled app at all.
#[tokio::test(start_paused = true)]
async fn enable_all_reaches_every_disabled_sheep_and_no_dog() {
    let h = harness(vec![ProcScript::never_exits(); 6]);
    start(
        &h,
        1,
        vec![
            AppConfig::minimal("api", "./srv"),
            AppConfig::minimal("web", "./srv"),
            AppConfig::minimal("worker", "./srv"),
        ],
    )
    .await;
    let dog = enable_dog(&h.ctx, 2, "metrics").await;
    disable(&h, 3, by_name("api")).await;
    disable(&h, 4, by_name("web")).await;

    let Ok(Response::Enabled(enabled)) = enable(&h, 5, SelectorSpec::All).await.result else {
        panic!("expected Enabled")
    };

    assert_eq!(names(&enabled), ["api", "web"]);
    let flock = list_flock(&h.ctx, 6).await;
    assert!(
        flock.iter().all(|info| info.disabled.is_none()),
        "{flock:?}"
    );
    assert!(
        flock.iter().all(|info| info.status == ProcStatus::Online),
        "{flock:?}"
    );
    assert_eq!(
        flock.iter().filter(|info| info.id == dog.id).count(),
        1,
        "the dog is neither restarted nor re-registered"
    );
}

#[tokio::test(start_paused = true)]
async fn enable_by_fold_and_by_regex_reaches_only_disabled_members() {
    let h = harness(vec![ProcScript::never_exits(); 5]);
    start(
        &h,
        1,
        vec![
            in_fold("api", "web"),
            in_fold("site", "web"),
            AppConfig::minimal("worker", "./srv"),
        ],
    )
    .await;
    disable(&h, 2, by_name("api")).await;

    let Ok(Response::Enabled(by_fold)) = enable(&h, 3, SelectorSpec::Fold("web".to_string()))
        .await
        .result
    else {
        panic!("expected Enabled")
    };
    assert_eq!(names(&by_fold), ["api"]);

    disable(&h, 4, by_name("worker")).await;
    let regex = SelectorSpec::Regex("^w".to_string());
    let Ok(Response::Enabled(by_regex)) = enable(&h, 5, regex).await.result else {
        panic!("expected Enabled")
    };
    assert_eq!(names(&by_regex), ["worker"]);
}

#[tokio::test(start_paused = true)]
async fn enable_all_with_nothing_disabled_changes_nothing() {
    let h = harness(vec![ProcScript::never_exits()]);
    start(&h, 1, vec![AppConfig::minimal("api", "./srv")]).await;

    let Ok(Response::Enabled(enabled)) = enable(&h, 2, SelectorSpec::All).await.result else {
        panic!("expected Enabled")
    };

    assert!(enabled.is_empty(), "{enabled:?}");
}

/// Fails if a disable sweep over apps that are all disabled already answers
/// `NotFound`: the fold exists and every app in it is parked as asked.
#[tokio::test(start_paused = true)]
async fn disabling_a_fold_again_changes_nothing_and_succeeds() {
    let h = harness(vec![ProcScript::never_exits(); 2]);
    start(&h, 1, vec![in_fold("api", "web"), in_fold("site", "web")]).await;
    disable(&h, 2, SelectorSpec::Fold("web".to_string())).await;

    let again = disable(&h, 3, SelectorSpec::Fold("web".to_string())).await;
    assert!(again.is_empty(), "{again:?}");
    let all = disable(&h, 4, SelectorSpec::All).await;
    assert!(all.is_empty(), "{all:?}");

    let missed = send(
        &h,
        5,
        Request::Disable {
            selector: SelectorSpec::Fold("nope".to_string()),
        },
    )
    .await;
    assert_eq!(missed.result.unwrap_err().code, RpcErrorCode::NotFound);
}

/// Fails if the log verbs resolve through the ordinary sweep: `flush all` and
/// `reopen all` would skip `api`, or answer `NotFound` with every app parked.
#[tokio::test(start_paused = true)]
async fn flush_and_reopen_all_still_reach_a_disabled_app() {
    let h = harness(vec![ProcScript::never_exits(); 2]);
    start(
        &h,
        1,
        vec![
            AppConfig::minimal("api", "./srv"),
            AppConfig::minimal("web", "./srv"),
        ],
    )
    .await;
    disable(&h, 2, by_name("api")).await;

    let Ok(Response::Flushed(flushed)) = send(
        &h,
        3,
        Request::Flush {
            selector: SelectorSpec::All,
        },
    )
    .await
    .result
    else {
        panic!("expected Flushed")
    };
    assert_eq!(names(&flushed), ["api", "web"]);

    disable(&h, 4, by_name("web")).await;
    let Ok(Response::Reopened(reopened)) = send(
        &h,
        5,
        Request::Reopen {
            selector: SelectorSpec::All,
        },
    )
    .await
    .result
    else {
        panic!("expected Reopened")
    };
    assert_eq!(names(&reopened), ["api", "web"]);
}