Add notifier to break relay loop
This commit is contained in:
parent
83c1f033d5
commit
8785186dfa
12 changed files with 124 additions and 45 deletions
emgauwa-controller/src
|
@ -1,17 +1,42 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use actix::Addr;
|
||||
use chrono::Local;
|
||||
use emgauwa_lib::constants::RELAYS_RETRY_TIMEOUT;
|
||||
use emgauwa_lib::errors::EmgauwaError;
|
||||
use futures::pin_mut;
|
||||
use tokio::time;
|
||||
use tokio::time::Instant;
|
||||
use tokio::time::timeout;
|
||||
use utils::app_state_get_notifier;
|
||||
|
||||
use crate::settings::Settings;
|
||||
use crate::app_state::AppState;
|
||||
use crate::utils;
|
||||
|
||||
#[allow(unused_variables)]
|
||||
pub async fn run_relay_loop(settings: Settings) {
|
||||
let default_duration = Duration::from_millis(10000);
|
||||
pub async fn run_relays_loop(app_state: Addr<AppState>) {
|
||||
log::debug!("Spawned relays loop");
|
||||
loop {
|
||||
let next_timestamp = Instant::now() + default_duration;
|
||||
time::sleep_until(next_timestamp).await;
|
||||
log::debug!("Relay loop: {}", Local::now().naive_local().time())
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue