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};
|
|
|
|
|
|
|
|
use rocket::tokio::time::sleep;
|
|
|
|
|
2023-01-09 20:23:55 +01:00
|
|
|
use crate::{
|
2023-01-15 16:31:37 +01:00
|
|
|
services::{Error, Service, Services},
|
2023-01-09 20:23:55 +01:00
|
|
|
STATUS,
|
|
|
|
};
|
2023-01-08 15:21:45 +01:00
|
|
|
|
2023-04-14 23:02:19 +02:00
|
|
|
/// The default sleep interval to use between checks.
|
|
|
|
const DEFAULT_SLEEP_INTERVAL: u64 = 10;
|
|
|
|
|
|
|
|
/// The sleep interval upper limit when applying exponential backoff.
|
|
|
|
const MAX_SLEEP_INTERVAL: u64 = 320;
|
|
|
|
|
|
|
|
/// The backoff factor.
|
|
|
|
const BACKOFF_FACTOR: f64 = 2.0;
|
|
|
|
|
|
|
|
/// Calculates the new interval by applying the backoff factor and taking the maximum into account.
|
|
|
|
fn back_off(interval: u64) -> u64 {
|
|
|
|
let new_interval = (interval as f64 * BACKOFF_FACTOR) as u64;
|
|
|
|
|
|
|
|
new_interval.min(MAX_SLEEP_INTERVAL)
|
|
|
|
}
|
|
|
|
|
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<()> {
|
2023-01-15 13:38:33 +01:00
|
|
|
let mut service = service;
|
|
|
|
|
2023-01-09 20:23:55 +01:00
|
|
|
// 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-04-14 23:02:19 +02:00
|
|
|
let mut sleep_interval = DEFAULT_SLEEP_INTERVAL;
|
2023-01-08 15:21:45 +01:00
|
|
|
loop {
|
|
|
|
// Wake up every 10 seconds and check if an update is due.
|
2023-04-14 23:02:19 +02:00
|
|
|
sleep(Duration::from_secs(sleep_interval)).await;
|
2023-01-08 15:21:45 +01:00
|
|
|
|
|
|
|
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,
|
2023-01-15 16:31:37 +01:00
|
|
|
Err(Error::NotAuthorized) => {
|
2023-01-16 20:02:44 +01:00
|
|
|
eprintln!("💥 Update unauthorized, trying to log in again...");
|
2023-04-14 23:02:19 +02:00
|
|
|
if let Err(e) = service.login().await {
|
|
|
|
eprintln!("💥 Login failed: {e}; will retry in {sleep_interval} seconds...");
|
|
|
|
sleep_interval = back_off(sleep_interval);
|
|
|
|
continue;
|
|
|
|
};
|
2023-01-08 15:21:45 +01:00
|
|
|
println!("⚡ Logged in successfully!");
|
2023-04-14 23:02:19 +02:00
|
|
|
sleep_interval = DEFAULT_SLEEP_INTERVAL;
|
2023-01-08 15:21:45 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2023-04-14 23:02:19 +02:00
|
|
|
eprintln!(
|
|
|
|
"💥 Failed to update status: {e}; will retry in {sleep_interval} seconds..."
|
|
|
|
);
|
|
|
|
sleep_interval = back_off(sleep_interval);
|
2023-01-08 15:21:45 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2023-04-14 23:02:19 +02:00
|
|
|
sleep_interval = DEFAULT_SLEEP_INTERVAL;
|
2023-01-08 15:21:45 +01:00
|
|
|
last_updated = timestamp;
|
|
|
|
|
|
|
|
println!("⚡ Updated status to: {:#?}", status);
|
|
|
|
let mut status_guard = STATUS.lock().expect("Status mutex was poisoned");
|
|
|
|
status_guard.replace(status);
|
|
|
|
}
|
|
|
|
}
|