trifid/tfclient/src/timerworker.rs

69 lines
2.2 KiB
Rust

use crate::apiworker::APIWorkerMessage;
use crate::daemon::ThreadMessageSender;
use crate::nebulaworker::NebulaWorkerMessage;
use crate::socketworker::SocketWorkerMessage;
use log::{error, info};
use std::ops::Add;
use std::sync::mpsc::{Receiver, TryRecvError};
use std::thread;
use std::time::{Duration, SystemTime};
pub enum TimerWorkerMessage {
Shutdown,
}
pub fn timer_main(tx: ThreadMessageSender, rx: Receiver<TimerWorkerMessage>, disable_config_updates: bool) {
let mut api_reload_timer = SystemTime::now().add(Duration::from_secs(60));
loop {
thread::sleep(Duration::from_secs(3));
match rx.try_recv() {
Ok(msg) => match msg {
TimerWorkerMessage::Shutdown => {
info!("recv on command socket: shutdown, stopping");
break;
}
},
Err(e) => match e {
TryRecvError::Empty => {}
TryRecvError::Disconnected => {
error!("timerworker command socket disconnected, shutting down to prevent orphaning");
break;
}
},
}
if SystemTime::now().gt(&api_reload_timer) && !disable_config_updates {
info!("Timer triggered: API_RELOAD_TIMER");
api_reload_timer = SystemTime::now().add(Duration::from_secs(60));
match tx.api_thread.send(APIWorkerMessage::Timer) {
Ok(_) => (),
Err(e) => {
error!("Error sending timer message to api worker thread: {}", e);
}
}
}
match tx.nebula_thread.send(NebulaWorkerMessage::WakeUp) {
Ok(_) => (),
Err(e) => {
error!(
"Error sending wakeup message to nebula worker thread: {}",
e
);
}
}
match tx.socket_thread.send(SocketWorkerMessage::WakeUp) {
Ok(_) => (),
Err(e) => {
error!(
"Error sending wakeup message to socket worker thread: {}",
e
);
}
}
}
}