diff --git a/src/health.rs b/src/health.rs new file mode 100644 index 0000000..a604da2 --- /dev/null +++ b/src/health.rs @@ -0,0 +1,48 @@ +use std::time::Duration; + +use tokio::sync::mpsc; +use tracing::{info, warn}; + +use crate::events::OwncastState; +use crate::owncast_api::OwncastApiClient; + +pub async fn run_health_poller( + api_client: &OwncastApiClient, + state_tx: mpsc::Sender, + interval: Duration, + mut shutdown: tokio::sync::watch::Receiver, +) { + let mut current_state = OwncastState::Unavailable; + + loop { + tokio::select! { + _ = tokio::time::sleep(interval) => {}, + _ = shutdown.changed() => { + info!("Health poller shutting down"); + return; + } + } + + let new_state = match api_client.get_status().await { + Ok(status) => { + if status.online { + OwncastState::Online + } else { + OwncastState::OfflineChatOpen + } + } + Err(e) => { + warn!(error = %e, "Failed to poll Owncast status"); + OwncastState::Unavailable + } + }; + + if new_state != current_state { + info!(old = ?current_state, new = ?new_state, "Owncast state changed"); + current_state = new_state.clone(); + if state_tx.send(new_state).await.is_err() { + return; + } + } + } +} diff --git a/src/main.rs b/src/main.rs index 3a164cb..eae70ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ mod config; mod events; +mod health; mod html; mod owncast_api;