shep-daemon 0.15.2

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
//! Tests that wait on real filesystem events or real elapsed time. The inner
//! loop skips them with `--skip ::slow::`; the full suite runs them.

use super::*;

// The overlap a reload runs on: the replacement arms before the
// drainee's exit disarms the old id, so `disarm` finds a member still
// standing. Task identity tells a surviving group from a rebuilt one, and
// the two `AbortHandle`s are held unfired so tokio cannot reuse the id.
#[tokio::test(start_paused = true)]
async fn a_replacement_arming_before_the_drainee_disarms_keeps_the_groups_own_tasks() {
    let dir = tempfile::tempdir().unwrap();
    let paths = test_paths(&dir);
    let root = tempfile::tempdir().unwrap();
    let (handle, _rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    // Both per-name extras, because the overlap has to hold for both.
    let app = app_with("web", |app| {
        app.cron_restart = Some("0 * * * *".to_string());
        app.watch = true;
        app.cwd = Some(root.path().display().to_string());
    });
    handle.start(vec![app.clone()]).await.unwrap();

    // The drainee: id 0, holding instance slot 0.
    registry.arm(
        &armed_entry(0, 0, 1000, app.clone(), &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );
    // Lets the cron worker reach its first poll, so the reading below
    // is settled rather than racing it.
    tokio::task::yield_now().await;

    let group = &registry.groups["web"];
    let cron = group
        .cron
        .as_ref()
        .expect("fixture check: the cron worker must have armed")
        .abort_handle();
    let watch = group
        .watch
        .as_ref()
        .expect("fixture check: the watch must have armed")
        .abort_handle();
    let reads_before = rig.clock.reads();

    // The overlap, in the order a swap performs it: the replacement
    // takes a new id in the drainee's slot and goes `Online` first.
    registry.arm(
        &armed_entry(1, 0, 2000, app, &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );
    registry.disarm(0, "web");
    // Lets a rebuilt worker reach its own first poll, so an unchanged
    // count below means there is none rather than that it had not run.
    tokio::task::yield_now().await;

    let group = &registry.groups["web"];
    assert_eq!(
        group.members,
        HashSet::from([1]),
        "fixture check: the drainee must really have left a group the \
     replacement had already joined — this reads the same under either \
     ordering, which is why it cannot be the claim that matters"
    );
    assert_eq!(
        group.cron.as_ref().map(JoinHandle::id),
        Some(cron.id()),
        "the group must still hold the cron worker it was armed with, not \
     an identical one put back in its place"
    );
    assert_eq!(
        group.watch.as_ref().map(JoinHandle::id),
        Some(watch.id()),
        "and the watch it was armed with, whose rebuild means re-registering \
     the OS watcher"
    );
    assert_eq!(
        rig.clock.reads(),
        reads_before,
        "a surviving cron worker performs no startup work; a rebuilt one \
     reads the clock again to derive its next occurrence"
    );
}

// A name group with zero online instances, armed and disarmed before it
// ever fires. An app whose first spawn is stopped straight away passes
// through this shape, and a worker leaked there restarts a flock nobody
// is running.
#[tokio::test(start_paused = true)]
async fn a_group_disarmed_before_its_first_occurrence_leaves_no_worker_behind() {
    let dir = tempfile::tempdir().unwrap();
    let paths = test_paths(&dir);
    let root = tempfile::tempdir().unwrap();
    let (handle, mut rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    // Both per-name extras and one per-pid extra, so a single disarm
    // has to reach all three.
    let app = app_with("web", |app| {
        app.cron_restart = Some("0 * * * *".to_string());
        app.watch = true;
        app.cwd = Some(root.path().display().to_string());
        app.max_memory = Some(MemSize::from_bytes(1024));
    });
    handle.start(vec![app.clone()]).await.unwrap();

    registry.arm(
        &armed_entry(0, 0, 1000, app, &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );
    let group = &registry.groups["web"];
    assert_eq!(group.members, HashSet::from([0]));
    assert!(group.cron.is_some(), "the cron worker must have armed");
    assert!(group.watch.is_some(), "the watch must have armed");
    assert_eq!(rig.enforcer.arms().len(), 1);

    registry.disarm(0, "web");

    assert!(
        registry.groups.is_empty(),
        "a group whose only member left before its first occurrence must go with it"
    );
    assert!(
        registry.instances.is_empty(),
        "the same disarm must take the instance's own extras too"
    );
    assert_eq!(rig.enforcer.disarms(), vec![0]);
    assert_no_restart_within(&mut rx, "web", PAST_THE_NEXT_OCCURRENCE).await;
}

// The watch twin of the case above, and separate because the gate is two
// independent conditions. The ending is forced with `abort` rather than
// by killing the `WatchSource` the loop really returns on, which dies
// with an OS thread no test reaches; both leave a finished handle.
#[tokio::test]
async fn a_watch_that_ended_on_its_own_is_rebuilt_on_the_next_arm() {
    let home = tempfile::tempdir().unwrap();
    let paths = test_paths(&home);
    let root = tempfile::tempdir().unwrap();
    let (handle, mut rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    let app = app_with("web", |app| {
        app.watch = true;
        app.cwd = Some(root.path().display().to_string());
        app.watch_delay = Some(UpDuration::from_millis(
            real_time::TEST_DELAY.as_millis() as u64
        ));
    });
    handle.start(vec![app.clone()]).await.unwrap();

    registry.arm(
        &armed_entry(0, 0, 1000, app.clone(), &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );
    let armed = registry.groups["web"]
        .watch
        .as_ref()
        .expect("the first arm registers a watcher");
    armed.abort();
    settle_finished(armed).await;

    registry.arm(
        &armed_entry(0, 0, 2000, app, &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );

    touch(root.path(), "trigger.txt").unwrap();
    let info = expect_restart(&mut rx, "web", real_time::SMOKE_DEADLINE).await;
    assert_eq!(info.restarts, 1);
}

// An unresolved root fires never: on macOS a tempdir under `/var/...` is
// delivered as `/private/var/...` and every `strip_prefix` fails.
#[tokio::test]
async fn a_watched_app_restarts_on_a_save_and_goes_quiet_once_disarmed() {
    let home = tempfile::tempdir().unwrap();
    let paths = test_paths(&home);
    let root = tempfile::tempdir().unwrap();
    let (handle, mut rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    let app = app_with("web", |app| {
        app.watch = true;
        app.cwd = Some(root.path().display().to_string());
        // From `watch::real_time`, the owner of this subsystem's
        // real-time constants.
        app.watch_delay = Some(UpDuration::from_millis(
            real_time::TEST_DELAY.as_millis() as u64
        ));
    });
    handle.start(vec![app.clone()]).await.unwrap();
    registry.arm(
        &armed_entry(0, 0, 1000, app, &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );

    touch(root.path(), "trigger.txt").unwrap();
    let info = expect_restart(&mut rx, "web", real_time::SMOKE_DEADLINE).await;
    assert_eq!(info.restarts, 1);

    registry.disarm(0, "web");
    touch(root.path(), "after-disarm.txt").unwrap();
    assert_no_restart_within(&mut rx, "web", real_time::NO_EVENT_WINDOW).await;
}

// `DEFAULT_WATCH_DELAY` is 500ms, so any longer fallback leaves the save
// below with no restart inside the deadline.
#[tokio::test]
async fn a_watched_app_naming_no_delay_still_restarts_on_a_save() {
    let home = tempfile::tempdir().unwrap();
    let paths = test_paths(&home);
    let root = tempfile::tempdir().unwrap();
    let (handle, mut rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    let app = app_with("web", |app| {
        app.watch = true;
        app.cwd = Some(root.path().display().to_string());
        // No `watch_delay`: this case exists for the default.
    });
    handle.start(vec![app.clone()]).await.unwrap();
    registry.arm(
        &armed_entry(0, 0, 1000, app, &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );

    touch(root.path(), "trigger.txt").unwrap();
    let info = expect_restart(&mut rx, "web", real_time::SMOKE_DEADLINE).await;
    assert_eq!(info.restarts, 1);
}

// The watch's own door into the claim the cron case makes, and separate
// because the two subsystems pick their `SupervisorHandle` method
// independently: through `restart`, an autosave is reported as a deploy.
#[tokio::test]
async fn a_watch_restart_is_not_reported_as_a_user_action() {
    let home = tempfile::tempdir().unwrap();
    let paths = test_paths(&home);
    let root = tempfile::tempdir().unwrap();
    let (handle, mut rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    let app = app_with("web", |app| {
        app.watch = true;
        app.cwd = Some(root.path().display().to_string());
        app.watch_delay = Some(UpDuration::from_millis(
            real_time::TEST_DELAY.as_millis() as u64
        ));
    });
    handle.start(vec![app.clone()]).await.unwrap();
    registry.arm(
        &armed_entry(0, 0, 1000, app, &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );

    touch(root.path(), "trigger.txt").unwrap();

    let (info, manually) = expect_restart_event(&mut rx, "web", real_time::SMOKE_DEADLINE).await;
    assert_eq!(info.restarts, 1);
    assert!(
        !manually,
        "a file changing under a watched tree is not a user action"
    );
}

// A filter built from empty slices discards every ignore rule the user
// wrote.
#[tokio::test]
async fn a_watched_app_ignores_the_paths_its_ignore_watch_names() {
    let home = tempfile::tempdir().unwrap();
    let paths = test_paths(&home);
    let root = tempfile::tempdir().unwrap();
    let (handle, mut rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    let app = app_with("web", |app| {
        app.watch = true;
        app.cwd = Some(root.path().display().to_string());
        app.ignore_watch = vec!["ignored.txt".to_string()];
        app.watch_delay = Some(UpDuration::from_millis(
            real_time::TEST_DELAY.as_millis() as u64
        ));
    });
    handle.start(vec![app.clone()]).await.unwrap();
    registry.arm(
        &armed_entry(0, 0, 1000, app, &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );

    touch(root.path(), "ignored.txt").unwrap();
    assert_no_restart_within(&mut rx, "web", real_time::NO_EVENT_WINDOW).await;

    touch(root.path(), "trigger.txt").unwrap();
    let info = expect_restart(&mut rx, "web", real_time::SMOKE_DEADLINE).await;
    assert_eq!(info.restarts, 1);
}

// The default globs plus `ignore_watch` alone let an app naming an
// explicit `out_file`/`err_file` under its own `cwd` restart on its own
// log writes forever: the default log glob covers only a directory named
// `logs`, and an automatic restart resets the budget.
#[tokio::test]
async fn a_watched_app_ignores_its_own_log_writes() {
    let home = tempfile::tempdir().unwrap();
    let paths = test_paths(&home);
    let root = tempfile::tempdir().unwrap();
    let (handle, mut rx, _fixture) = spawn_test_fixture();
    let rig = rig(DEFAULT_MAX_CRON_SLEEP);
    let mut registry = ExtrasRegistry::default();
    let app = app_with("web", |app| {
        app.watch = true;
        app.cwd = Some(root.path().display().to_string());
        // Absolute, under the watched tree, and named nothing like
        // `logs`: a shep write really does land inside the tree.
        app.out_file = Some(root.path().join("app-out.txt").display().to_string());
        app.err_file = Some(root.path().join("app-err.txt").display().to_string());
        app.watch_delay = Some(UpDuration::from_millis(
            real_time::TEST_DELAY.as_millis() as u64
        ));
    });
    handle.start(vec![app.clone()]).await.unwrap();
    registry.arm(
        &armed_entry(0, 0, 1000, app, &paths),
        idle_prober(),
        &rig.extras,
        &handle,
    );

    touch(root.path(), "app-out.txt").unwrap();
    touch(root.path(), "app-err.txt").unwrap();
    assert_no_restart_within(&mut rx, "web", real_time::NO_EVENT_WINDOW).await;

    touch(root.path(), "trigger.txt").unwrap();
    let info = expect_restart(&mut rx, "web", real_time::SMOKE_DEADLINE).await;
    assert_eq!(info.restarts, 1);
}

// `probe_exec` runs `env_clear().envs(&self.env)` and `SHEP_INSTANCE` is
// written by `assemble` alone, so a prober built from `config.env`, or
// built once and shared, expands it to nothing and both instances report.
// A file, not a port: `test -f` needs no listener and cannot race.
#[cfg(unix)]
#[tokio::test]
async fn each_instances_liveness_probe_runs_with_its_own_assembled_env() {
    let markers = tempfile::tempdir().unwrap();
    // Instance 0's marker exists; instance 1's never will.
    std::fs::write(markers.path().join("live-0"), b"").unwrap();

    let mut h = harness(vec![ProcScript::never_exits(); 4]);
    h.ctx
        .supervisor
        .start(vec![app_with("web", |app| {
            app.instances = 2;
            app.liveness_probe = Some(ProbeConfig {
                failure_threshold: 1,
                interval: PROBE_INTERVAL,
                timeout: UpDuration::from_millis(5_000),
                ..probe_config(
                    ProbeKind::Exec,
                    &format!(
                        r#"test -f "{}/live-$SHEP_INSTANCE""#,
                        markers.path().display()
                    ),
                )
            });
        })])
        .await
        .unwrap();

    let listing = h.ctx.supervisor.list().await;
    // `ProcessInfo` carries no instance number, but the assembler's log
    // path does, from the same `assemble` call, so this pins which
    // instance id 1 is rather than assuming the allocation order.
    assert!(
        listing[1]
            .out_file
            .as_ref()
            .is_some_and(|path| path.ends_with("web-1-out.log")),
        "id 1 must be instance 1: {:?}",
        listing[1].out_file
    );
    let instance_one_pid = listing[1].pid.expect("a live sheep has a pid");

    let failure = expect_liveness(&mut h.liveness, LIVENESS_DEADLINE).await;
    assert_eq!(
        failure,
        LivenessReport {
            id: 1,
            pid: instance_one_pid,
            epoch: 1,
        },
        "only the instance whose own marker is missing may report"
    );
    // Both instances report under the bugs above and which arrives
    // first is a race, so the window catching the other is not optional.
    assert_no_liveness_within(&mut h.liveness, PROBE_INTERVAL.as_duration() * 3).await;
}