diff --git a/src/main.rs b/src/main.rs index 0854aee..b9e81a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,34 +65,43 @@ async fn main() -> anyhow::Result<()> { for (id, node) in config.vms.iter() { tracing::info!("Adding node {id} : {:?}", node); - let mut clone = tx.subscribe(); - - let zone = tzfile::Tz::named("GMT")?; - let now = { chrono::Utc::now().with_timezone(&&zone) }; - - let date_path = config.root_path.join(format!( - "{:02}-{:02}-{:02}", - now.year(), - now.month(), - now.day() - )); - - if !date_path.exists() { - std::fs::create_dir_all(&date_path)?; - } - + let mut _rx = tx.subscribe(); let id_clone = id.clone(); let node_clone = node.clone(); + let root = config.root_path.clone(); let _: JoinHandle> = tokio::spawn(async move { - while let Some(_) = clone.recv().await? { + while let Some(_) = _rx.recv().await? { let span = tracing::span!(Level::INFO, "node screenshot", node = id_clone.as_str()); + let zone = tzfile::Tz::named("GMT")?; + let now = { chrono::Utc::now().with_timezone(&&zone) }; + + let date_path = root.join(format!( + "{:02}-{:02}-{:02}", + now.year(), + now.month(), + now.day() + )); + + let node_path = date_path.join(&id_clone); + + if !node_path.exists() { + std::fs::create_dir_all(&node_path)?; + } + + let file_path = node_path.join(format!( + "{:02}-{:02}-{:02}.webp", + now.hour(), + now.minute(), + now.second() + )); + match shotter::take_one_screenshot( &node_clone.url, &node_clone.origin, &id_clone, - date_path.clone(), + file_path.clone(), config.webp_quality, ) .instrument(span) diff --git a/src/shotter.rs b/src/shotter.rs index 99b3033..e2985e3 100644 --- a/src/shotter.rs +++ b/src/shotter.rs @@ -4,7 +4,6 @@ use base64::{ engine::general_purpose::{GeneralPurpose, GeneralPurposeConfig}, Engine, }; -use chrono::Timelike; use tokio::task::JoinHandle; use tokio_tungstenite::tungstenite::client::IntoClientRequest; @@ -19,7 +18,7 @@ pub async fn take_one_screenshot( url: &String, origin: &String, node_name: &String, - root_path: std::path::PathBuf, + image_path: std::path::PathBuf, webp_quality: f32, ) -> anyhow::Result<()> { // Don't particularly like this syntax but whatever @@ -84,7 +83,6 @@ pub async fn take_one_screenshot( .in_current_span(), ); - let node_name_clone = node_name.clone(); // main handler let write_guac_handler_clone = write_tx.clone(); @@ -113,18 +111,7 @@ pub async fn take_one_screenshot( // Layer 0 is the screen (it's also the only layer cvmts sends) if msg[1].as_str() == "0" { if msg[3].as_str() == "0" && msg[4].as_str() == "0" { - let zone = tzfile::Tz::named("GMT")?; - let now = { chrono::Utc::now().with_timezone(&&zone) }; - - let id_path = root_path.join(&node_name_clone); - let file_path = id_path.join(format!( - "{:02}-{:02}-{:02}.webp", - now.hour(), - now.minute(), - now.second() - )); - - let file_path_clone = file_path.clone(); + let path = image_path.clone(); let _: anyhow::Result<()> = tokio::task::spawn_blocking(move || { @@ -151,12 +138,9 @@ pub async fn take_one_screenshot( let encoder = webp::Encoder::from_image(&image) .expect("fuck you"); - if !id_path.exists() { - std::fs::create_dir_all(&id_path)?; - } let mut file = - std::fs::File::create(file_path_clone)?; + std::fs::File::create(&path)?; { let encoded = encoder @@ -171,7 +155,7 @@ pub async fn take_one_screenshot( tracing::info!( "Screenshot written to \"{}\"", - file_path.display() + image_path.display() ); // no longer needed, everyone will close now!