38 lines
1.1 KiB
Rust
38 lines
1.1 KiB
Rust
use crate::http_client::HttpClient;
|
|
use crate::metars::Metar;
|
|
use chrono::{DateTime, Utc};
|
|
use std::sync::Arc;
|
|
use std::time::{Duration, Instant};
|
|
use tokio::time::interval;
|
|
|
|
pub fn update_metars(client: Arc<HttpClient>, seconds: u64) {
|
|
tokio::spawn(async move {
|
|
// Create interval ticker
|
|
let mut interval = interval(Duration::from_secs(seconds));
|
|
let mut etag = None;
|
|
|
|
loop {
|
|
interval.tick().await;
|
|
|
|
// Record start times
|
|
let start_monotonic = Instant::now();
|
|
let start_utc: DateTime<Utc> = Utc::now();
|
|
log::debug!("METAR update started at {}", start_utc);
|
|
|
|
// Run the update
|
|
match Metar::update_metars(&client, etag.clone()).await {
|
|
Ok(new_etag) => etag = Some(new_etag),
|
|
Err(err) => log::error!("METAR update failed: {}", err),
|
|
}
|
|
|
|
let elapsed = start_monotonic.elapsed();
|
|
let next_utc = Utc::now() + chrono::Duration::from_std(Duration::from_secs(seconds)).unwrap();
|
|
log::info!(
|
|
"METAR update finished in {:.2?}; next run at {}",
|
|
elapsed,
|
|
next_utc
|
|
);
|
|
}
|
|
});
|
|
}
|