use std::path::{Path, PathBuf};
use shep_core::barks::{self, Bark};
use shep_core::config::AppConfig;
use shep_core::protocol::{BusEvent, ProcessEventKind};
use tokio::sync::broadcast::{self, error::RecvError};
use crate::bus::{Bus, SharedEvent};
use super::narrate;
pub fn spawn_dog_watch(
mut events: broadcast::Receiver<SharedEvent>,
publish: Bus,
barks: PathBuf,
) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
loop {
match events.recv().await {
Ok(event) => {
let BusEvent::Process {
event: kind, info, ..
} = &*event
else {
continue;
};
if info.dog.is_none() {
continue;
}
match kind {
ProcessEventKind::Errored => {
record_dog_errored(&barks, &info.name, info.restarts);
}
ProcessEventKind::Start => {
let pid = info
.pid
.map_or_else(|| "unknown".to_string(), |pid| pid.to_string());
narrate(
&publish,
info,
&format!("shep started this dog; its process is pid {pid}"),
)
.await;
}
ProcessEventKind::Exit => {
narrate(&publish, info, &narrate::exit_words(info)).await;
}
_ => {}
}
}
Err(RecvError::Lagged(count)) => {
tracing::warn!(
count,
"the shepherd's dog watch dropped bus events; a dog's exhausted restart budget may have gone unrecorded"
);
}
Err(RecvError::Closed) => break,
}
}
})
}
fn record_dog_errored(barks_path: &Path, name: &str, restarts: u32) {
let budget = AppConfig::default().max_restarts;
tracing::error!(dog = %name, restarts, budget, "a dog exhausted its restart budget");
let bark = Bark {
at_ms: crate::now_ms(),
rule: "daemon".to_string(),
subject: name.to_string(),
message: format!(
"dog {name} exhausted its restart budget: {restarts} restarts against a budget of {budget}"
),
sinks: Vec::new(),
};
if let Err(err) = barks::append(barks_path, &bark, barks::DEFAULT_MAX_BYTES) {
tracing::warn!(%err, dog = %name, "failed to record a dog's exhausted restart budget");
}
}
#[cfg(test)]
mod tests {
use super::*;
use shep_core::protocol::{DogSource, ProcessInfo};
use shep_core::status::ProcStatus;
fn process_event(name: &str, kind: ProcessEventKind, dog: Option<DogSource>) -> SharedEvent {
SharedEvent::new(BusEvent::Process {
event: kind,
info: ProcessInfo::builder(1, name, ProcStatus::Errored)
.restarts(16)
.dog(dog)
.build(),
manually: false,
at_ms: 1_700_000_000_000,
})
}
fn errored_event(name: &str, dog: Option<DogSource>) -> SharedEvent {
process_event(name, ProcessEventKind::Errored, dog)
}
async fn await_barks(path: &std::path::Path, n: usize) -> Vec<Bark> {
tokio::time::timeout(std::time::Duration::from_secs(2), async {
loop {
let found = barks::read(path).unwrap_or_default();
if found.len() >= n {
return found;
}
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
})
.await
.expect("barks.jsonl never reached the expected record count")
}
#[tokio::test]
async fn the_shepherd_records_a_dog_that_gave_up_and_leaves_the_sheep_to_bark() {
let dir = tempfile::tempdir().unwrap();
let barks_path = dir.path().join("barks.jsonl");
let (events, rx) = crate::bus::test_bus(16);
let watch = spawn_dog_watch(rx, events.clone(), barks_path.clone());
events.send(errored_event("web", None)).unwrap();
events
.send(errored_event(
"bark",
Some(DogSource::BuiltIn {
grants: None,
events: Vec::new(),
}),
))
.unwrap();
let recorded = await_barks(&barks_path, 1).await;
assert_eq!(recorded.len(), 1, "one record, and it is the dog's");
assert_eq!(recorded[0].subject, "bark");
assert_eq!(recorded[0].rule, "daemon");
assert!(
recorded[0].sinks.is_empty(),
"the shepherd has no sinks and says so by carrying none"
);
watch.abort();
}
#[tokio::test]
async fn a_dog_that_merely_exited_is_not_recorded() {
let dir = tempfile::tempdir().unwrap();
let barks_path = dir.path().join("barks.jsonl");
let (events, rx) = crate::bus::test_bus(16);
let watch = spawn_dog_watch(rx, events.clone(), barks_path.clone());
events
.send(process_event(
"bark",
ProcessEventKind::Exit,
Some(DogSource::BuiltIn {
grants: None,
events: Vec::new(),
}),
))
.unwrap();
events
.send(errored_event(
"bark",
Some(DogSource::BuiltIn {
grants: None,
events: Vec::new(),
}),
))
.unwrap();
let recorded = await_barks(&barks_path, 1).await;
assert_eq!(
recorded.len(),
1,
"the Exit left no record; only the Errored that followed it did"
);
watch.abort();
}
}