#[derive(Debug, Clone)]
pub struct SupervisionState<'a, F>
where
F: Fn(u32) -> bool,
{
pub original_ppid: u32,
pub current_ppid: u32,
pub host_ppid: Option<u32>,
pub is_alive: F,
pub platform: Option<&'a str>,
}
pub fn supervision_lost_reason<F>(state: SupervisionState<'_, F>) -> Option<String>
where
F: Fn(u32) -> bool,
{
let platform = state.platform.unwrap_or(std::env::consts::OS);
if state.current_ppid != state.original_ppid {
return Some(format!(
"ppid {} -> {}",
state.original_ppid, state.current_ppid
));
}
if platform == "windows" && state.original_ppid > 1 && !(state.is_alive)(state.original_ppid) {
return Some(format!("parent pid {} exited", state.original_ppid));
}
if let Some(host_pid) = state.host_ppid
&& !(state.is_alive)(host_pid)
{
return Some(format!("host pid {host_pid} exited"));
}
None
}