printobserver_server/observation.rs
1//! The driver that gives every active print its periodic observation.
2//!
3//! Core decides which prints are owed an observation and writes each one down
4//! ([`Supervisor::observe_active_prints`]); this is what asks it to, once per
5//! [`CoreConfig::observation_interval`](printobserver_core::CoreConfig), and
6//! runs each turn an observation claimed. Each round waits one interval after
7//! the last one finished, as the expiry driver sleeps between its sweeps: the
8//! first is one interval after the server starts rather than at the start, so a
9//! restart is not itself a reason to look, and a slow round is followed by an
10//! interval rather than by a burst of rounds making up for it. A print checked
11//! as it carried on from a planned change is next due one interval after that
12//! check, so the round after it waits only until then
13//! ([`Supervisor::until_next_observation`]).
14//!
15//! # Why it lives in the server rather than beside the expiry driver
16//!
17//! Core's expiry driver is a thread of its own that polls with no reactor, and
18//! the printer port allows that. An observation takes the camera's frame
19//! through the vision port, whose HTTP client needs Tokio's reactor, so the
20//! observation is driven from this server's runtime — written down and run on
21//! blocking threads of it, exactly as the ingress worker writes down an alert
22//! and runs the turn it claimed.
23//!
24//! # It holds the supervisor weakly
25//!
26//! Like the expiry driver, it holds a weak reference, so it stops of its own
27//! accord once the last handle to the supervisor is dropped; a server that is
28//! stopped also aborts it.
29
30use std::sync::{Arc, Weak};
31
32use printobserver_core::Supervisor;
33use tokio::task::JoinHandle;
34
35/// Start observing every active print once per the supervisor's interval.
36pub fn start(supervisor: &Arc<Supervisor>) -> JoinHandle<()> {
37 let weak: Weak<Supervisor> = Arc::downgrade(supervisor);
38 let mut wait = supervisor.config().observation_interval;
39 tokio::spawn(async move {
40 loop {
41 tokio::time::sleep(wait).await;
42 let Some(supervisor) = weak.upgrade() else {
43 return;
44 };
45 let observing = Arc::clone(&supervisor);
46 let observed = tokio::task::spawn_blocking(move || {
47 printobserver_core::block_on(observing.observe_active_prints())
48 })
49 .await;
50 wait = supervisor.until_next_observation();
51 debug_assert!(observed.is_ok(), "one periodic observation panicked");
52 // A round the store refused claimed nothing, and a print whose
53 // observation it refused claimed no turn: the next round tries
54 // again, and there is nobody to answer either to here.
55 let Ok(Ok(observed)) = observed else {
56 continue;
57 };
58 for turn in observed.turns {
59 let runner = Arc::clone(&supervisor);
60 tokio::spawn(async move {
61 let supervised = tokio::task::spawn_blocking(move || {
62 let _ = printobserver_core::block_on(runner.run_supervision(turn));
63 })
64 .await;
65 debug_assert!(
66 supervised.is_ok(),
67 "the supervision of one observation panicked"
68 );
69 });
70 }
71 }
72 })
73}