2023-01-09 20:23:55 +01:00
|
|
|
//! Module for handling the status updating/retrieval via the cloud service API.
|
2023-01-08 15:21:45 +01:00
|
|
|
|
|
|
|
use std::time::{Duration, SystemTime};
|
|
|
|
|
2023-01-09 20:23:55 +01:00
|
|
|
use reqwest::StatusCode;
|
2023-01-08 15:21:45 +01:00
|
|
|
use rocket::tokio::time::sleep;
|
|
|
|
|
2023-01-09 20:23:55 +01:00
|
|
|
use crate::{
|
|
|
|
services::{Service, Services},
|
|
|
|
STATUS,
|
|
|
|
};
|
2023-01-08 15:21:45 +01:00
|
|
|
|
|
|
|
/// Main update loop that logs in and periodically acquires updates from the API.
|
|
|
|
///
|
2023-01-09 20:23:55 +01:00
|
|
|
/// It updates the mutex-guarded current update [`Status`](crate::Status) struct which can be
|
|
|
|
/// retrieved via Rocket.
|
|
|
|
pub(super) async fn update_loop(service: Services) -> color_eyre::Result<()> {
|
|
|
|
// Log in on the cloud service.
|
2023-01-08 15:21:45 +01:00
|
|
|
println!("⚡ Logging in...");
|
2023-01-09 20:23:55 +01:00
|
|
|
service.login().await?;
|
2023-01-08 15:21:45 +01:00
|
|
|
println!("⚡ Logged in successfully!");
|
|
|
|
|
|
|
|
let mut last_updated = 0;
|
2023-01-09 20:23:55 +01:00
|
|
|
let poll_interval = service.poll_interval();
|
2023-01-08 15:21:45 +01:00
|
|
|
loop {
|
|
|
|
// Wake up every 10 seconds and check if an update is due.
|
|
|
|
sleep(Duration::from_secs(10)).await;
|
|
|
|
|
|
|
|
let timestamp = SystemTime::now()
|
|
|
|
.duration_since(SystemTime::UNIX_EPOCH)
|
|
|
|
.unwrap_or_default()
|
|
|
|
.as_secs();
|
2023-01-09 20:23:55 +01:00
|
|
|
if timestamp - last_updated < poll_interval {
|
2023-01-08 15:21:45 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-01-09 20:23:55 +01:00
|
|
|
let status = match service.update(timestamp).await {
|
2023-01-08 15:21:45 +01:00
|
|
|
Ok(status) => status,
|
|
|
|
Err(e) if e.status() == Some(StatusCode::UNAUTHORIZED) => {
|
|
|
|
println!("✨ Update unauthorized, trying to log in again...");
|
2023-01-09 20:23:55 +01:00
|
|
|
service.login().await?;
|
2023-01-08 15:21:45 +01:00
|
|
|
println!("⚡ Logged in successfully!");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
println!("✨ Failed to update status: {}", e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
last_updated = timestamp;
|
|
|
|
|
|
|
|
println!("⚡ Updated status to: {:#?}", status);
|
|
|
|
let mut status_guard = STATUS.lock().expect("Status mutex was poisoned");
|
|
|
|
status_guard.replace(status);
|
|
|
|
}
|
|
|
|
}
|