74 lines
1.8 KiB
Rust
74 lines
1.8 KiB
Rust
use std::time::Duration;
|
|
|
|
use common::ServerMessage;
|
|
use tokio::time::sleep;
|
|
|
|
use crate::{
|
|
collector::{Collector, MarkdownWeb, WeatherApi},
|
|
State,
|
|
};
|
|
|
|
pub async fn info_task(state: &State) {
|
|
let mut collectors: Vec<Box<dyn Collector + Send>> = vec![];
|
|
|
|
for url in &state.config.collectors.markdown_web_links {
|
|
collectors.push(Box::new(MarkdownWeb {
|
|
url: url.to_string(),
|
|
}));
|
|
}
|
|
|
|
if !state.config.collectors.weatherapi_locations.is_empty() {
|
|
let api_key = state
|
|
.config
|
|
.collectors
|
|
.weatherapi_key
|
|
.as_deref()
|
|
.expect("Missing weatherapi_key");
|
|
|
|
for location in state.config.collectors.weatherapi_locations.iter().cloned() {
|
|
collectors.push(Box::new(WeatherApi {
|
|
api_key: api_key.to_string(),
|
|
location,
|
|
}));
|
|
}
|
|
}
|
|
|
|
let mut collectors = collectors.into_boxed_slice();
|
|
|
|
if collectors.is_empty() {
|
|
return;
|
|
}
|
|
|
|
let server_message = &state.server_message;
|
|
|
|
let collectors_len = collectors.len();
|
|
let next = move |i: usize| (i + 1) % collectors_len;
|
|
let mut i = 0;
|
|
|
|
loop {
|
|
sleep(Duration::from_secs(30)).await;
|
|
|
|
// don't bother collecting if no clients are connected
|
|
// there is always 1 receiver held by main process
|
|
if server_message.receiver_count() <= 1 {
|
|
continue;
|
|
}
|
|
|
|
i = next(i);
|
|
let collector = &mut collectors[i];
|
|
|
|
let msg = match collector.collect().await {
|
|
Ok(html) => ServerMessage::InfoPage { html },
|
|
Err(e) => {
|
|
warn!("collector error: {e}");
|
|
continue;
|
|
}
|
|
};
|
|
|
|
if let Err(e) = server_message.send(msg) {
|
|
error!("broadcast channel error: {e}");
|
|
return;
|
|
}
|
|
}
|
|
}
|