trifid/tfclient/src/timerworker.rs

66 lines
2.1 KiB
Rust
Raw Normal View History

2023-03-29 13:37:03 +00:00
use std::ops::Add;
use std::sync::mpsc::{Receiver, TryRecvError};
2023-03-31 12:33:02 +00:00
use std::thread;
2023-03-29 13:37:03 +00:00
use std::time::{Duration, SystemTime};
use log::{error, info};
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-03-29 13:37:03 +00:00
pub enum TimerWorkerMessage {
Shutdown
}
pub fn timer_main(tx: ThreadMessageSender, rx: Receiver<TimerWorkerMessage>) {
let mut api_reload_timer = SystemTime::now().add(Duration::from_secs(60));
loop {
2023-03-31 12:33:02 +00:00
thread::sleep(Duration::from_secs(10));
2023-03-29 13:37:03 +00:00
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) {
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) => {
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);
}
}
2023-03-29 13:37:03 +00:00
}
}