2023-11-27 14:09:14 +00:00
|
|
|
use std::time::Duration;
|
2023-11-29 13:27:46 +00:00
|
|
|
|
2023-12-07 03:30:33 +00:00
|
|
|
use actix::Addr;
|
2023-11-29 13:27:46 +00:00
|
|
|
use chrono::Local;
|
2023-12-07 03:30:33 +00:00
|
|
|
use emgauwa_lib::constants::RELAYS_RETRY_TIMEOUT;
|
|
|
|
use emgauwa_lib::errors::EmgauwaError;
|
|
|
|
use futures::pin_mut;
|
2023-11-27 14:09:14 +00:00
|
|
|
use tokio::time;
|
2023-12-07 03:30:33 +00:00
|
|
|
use tokio::time::timeout;
|
|
|
|
use utils::app_state_get_notifier;
|
2023-11-25 23:54:03 +00:00
|
|
|
|
2023-12-07 03:30:33 +00:00
|
|
|
use crate::app_state::AppState;
|
|
|
|
use crate::utils;
|
2023-11-29 13:27:46 +00:00
|
|
|
|
2023-12-07 03:30:33 +00:00
|
|
|
pub async fn run_relays_loop(app_state: Addr<AppState>) {
|
|
|
|
log::debug!("Spawned relays loop");
|
2023-11-27 14:09:14 +00:00
|
|
|
loop {
|
2023-12-07 03:30:33 +00:00
|
|
|
let run_result = run_relays(&app_state).await;
|
|
|
|
if let Err(err) = run_result {
|
|
|
|
log::error!("Error running relays: {}", err);
|
|
|
|
}
|
|
|
|
time::sleep(RELAYS_RETRY_TIMEOUT).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn run_relays(app_state: &Addr<AppState>) -> Result<(), EmgauwaError> {
|
|
|
|
let default_duration = Duration::new(10, 0);
|
|
|
|
let notifier = &*app_state_get_notifier(app_state).await?;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let notifier_future = notifier.notified();
|
|
|
|
pin_mut!(notifier_future);
|
|
|
|
let timeout_result = timeout(default_duration, &mut notifier_future).await;
|
|
|
|
let changed = timeout_result.is_ok();
|
|
|
|
|
|
|
|
log::debug!(
|
|
|
|
"Relay loop at {} (changed: {})",
|
|
|
|
Local::now().naive_local().time(),
|
|
|
|
changed
|
|
|
|
);
|
2023-11-27 14:09:14 +00:00
|
|
|
}
|
2023-11-27 11:49:40 +00:00
|
|
|
}
|