trifid/tfclient/src/timerworker.rs

69 lines
2.2 KiB
Rust
Raw Normal View History

2023-03-29 13:37:03 +00:00
use crate::apiworker::APIWorkerMessage;
use crate::daemon::ThreadMessageSender;
2023-03-31 12:33:02 +00:00
use crate::nebulaworker::NebulaWorkerMessage;
use crate::socketworker::SocketWorkerMessage;
2023-05-14 17:47:49 +00:00
use log::{error, info};
use std::ops::Add;
use std::sync::mpsc::{Receiver, TryRecvError};
use std::thread;
use std::time::{Duration, SystemTime};
2023-03-29 13:37:03 +00:00
pub enum TimerWorkerMessage {
2023-05-14 17:47:49 +00:00
Shutdown,
2023-03-29 13:37:03 +00:00
}
2023-06-26 02:00:36 +00:00
pub fn timer_main(tx: ThreadMessageSender, rx: Receiver<TimerWorkerMessage>, disable_config_updates: bool) {
2023-03-29 13:37:03 +00:00
let mut api_reload_timer = SystemTime::now().add(Duration::from_secs(60));
loop {
2023-06-26 02:00:36 +00:00
thread::sleep(Duration::from_secs(3));
2023-03-31 12:33:02 +00:00
2023-03-29 13:37:03 +00:00
match rx.try_recv() {
2023-05-14 17:47:49 +00:00
Ok(msg) => match msg {
TimerWorkerMessage::Shutdown => {
info!("recv on command socket: shutdown, stopping");
break;
2023-03-29 13:37:03 +00:00
}
},
2023-05-14 17:47:49 +00:00
Err(e) => match e {
TryRecvError::Empty => {}
TryRecvError::Disconnected => {
error!("timerworker command socket disconnected, shutting down to prevent orphaning");
break;
2023-03-29 13:37:03 +00:00
}
2023-05-14 17:47:49 +00:00
},
2023-03-29 13:37:03 +00:00
}
2023-06-26 02:00:36 +00:00
if SystemTime::now().gt(&api_reload_timer) && !disable_config_updates {
2023-03-29 13:37:03 +00:00
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);
}
}
}
2023-03-31 12:33:02 +00:00
match tx.nebula_thread.send(NebulaWorkerMessage::WakeUp) {
Ok(_) => (),
Err(e) => {
2023-05-14 17:47:49 +00:00
error!(
"Error sending wakeup message to nebula worker thread: {}",
e
);
2023-03-31 12:33:02 +00:00
}
}
match tx.socket_thread.send(SocketWorkerMessage::WakeUp) {
Ok(_) => (),
Err(e) => {
2023-05-14 17:47:49 +00:00
error!(
"Error sending wakeup message to socket worker thread: {}",
e
);
2023-03-31 12:33:02 +00:00
}
}
2023-03-29 13:37:03 +00:00
}
2023-05-14 17:47:49 +00:00
}