feat: add Owncast health poller with state change detection

Made-with: Cursor
This commit is contained in:
cottongin
2026-03-10 21:55:13 -04:00
parent 3876e12ec9
commit 09fbf76873
2 changed files with 49 additions and 0 deletions

48
src/health.rs Normal file
View File

@@ -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<OwncastState>,
interval: Duration,
mut shutdown: tokio::sync::watch::Receiver<bool>,
) {
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;
}
}
}
}

View File

@@ -1,5 +1,6 @@
mod config; mod config;
mod events; mod events;
mod health;
mod html; mod html;
mod owncast_api; mod owncast_api;