From 8083b1492a0e62e20389caff9eb890527831a0ec Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Tue, 7 Apr 2020 10:41:17 -0400 Subject: [PATCH] split out front tracking --- src/front.rs | 27 +++++++++++++++++++++++++++ src/main.rs | 33 +++++---------------------------- 2 files changed, 32 insertions(+), 28 deletions(-) create mode 100644 src/front.rs diff --git a/src/front.rs b/src/front.rs new file mode 100644 index 0000000..94b1026 --- /dev/null +++ b/src/front.rs @@ -0,0 +1,27 @@ +use crate::{config::Config, MTState}; +use log::{error, info}; +use std::{thread, time}; + +const FIVE_MINUTES: u64 = 60 * 5; + +pub(crate) fn update(st: MTState, cfg: Config) { + loop { + match reqwest::blocking::get(&cfg.front_url) { + Ok(who) => { + let mut data = st.lock().unwrap(); + let who = who.text().unwrap().trim().to_string(); + if who != data.front { + data.front = who; + info!("new front: {}", data.front); + } + } + + Err(why) => { + error!("front error: {:?}", why); + } + } + + let dur = time::Duration::new(FIVE_MINUTES, 0); + thread::sleep(dur); + } +} diff --git a/src/main.rs b/src/main.rs index 0732324..ac51ad3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ mod config; +mod front; mod xsetroot; use chrono::prelude::*; @@ -13,7 +14,7 @@ use std::{ }; use xsetroot::XSetRoot; -pub type MTState = Arc>; +pub(crate) type MTState = Arc>; fn handle_client(stream: UnixStream, st: MTState) { let mut rdr = BufReader::new(&stream); @@ -41,30 +42,6 @@ fn update_every_so_often(st: MTState) { } } -const FIVE_MINUTES: u64 = 60 * 5; - -fn front_update(st: MTState, cfg: config::Config) { - loop { - match reqwest::blocking::get(&cfg.front_url) { - Ok(who) => { - let mut data = st.lock().unwrap(); - let who = who.text().unwrap().trim().to_string(); - if who != data.front { - data.front = who; - info!("new front: {}", data.front); - } - } - - Err(why) => { - error!("front error: {:?}", why); - } - } - - let dur = time::Duration::new(FIVE_MINUTES, 0); - thread::sleep(dur); - } -} - fn main() -> std::io::Result<()> { let st: State = State::init(); st.show(); @@ -82,7 +59,7 @@ fn main() -> std::io::Result<()> { { let mtst = mtst.clone(); let cfg = cfg.clone(); - thread::spawn(move || front_update(mtst, cfg)); + thread::spawn(move || front::update(mtst, cfg)); } let _ = std::fs::remove_file("/home/cadey/tmp/cabytcini.sock")?; @@ -103,7 +80,7 @@ fn main() -> std::io::Result<()> { Ok(()) } -pub struct State { +pub(crate) struct State { msg: String, front: String, } @@ -122,7 +99,7 @@ impl State { } } - fn show(&self) { + pub(crate) fn show(&self) { let now = Local::now().format("%H:%M M%m %-d %a"); let xsr = XSetRoot::init();