split out front tracking

This commit is contained in:
Cadey Ratio 2020-04-07 10:41:17 -04:00
parent f48cc9874a
commit 8083b1492a
2 changed files with 32 additions and 28 deletions

27
src/front.rs Normal file
View File

@ -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);
}
}

View File

@ -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<Mutex<State>>;
pub(crate) type MTState = Arc<Mutex<State>>;
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();