feat: add Owncast health poller with state change detection
Made-with: Cursor
This commit is contained in:
48
src/health.rs
Normal file
48
src/health.rs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
mod config;
|
mod config;
|
||||||
mod events;
|
mod events;
|
||||||
|
mod health;
|
||||||
mod html;
|
mod html;
|
||||||
mod owncast_api;
|
mod owncast_api;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user