diff --git a/src/main.rs b/src/main.rs index 986c885..2dfedcb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,6 +43,7 @@ struct Config { #[tokio::main] async fn main() -> anyhow::Result<()> { + // Init tracing #[cfg(debug_assertions)] let subscriber = FmtSubscriber::builder() .with_max_level(Level::TRACE) @@ -54,14 +55,16 @@ async fn main() -> anyhow::Result<()> { .with_max_level(Level::INFO) .compact() .finish(); - + tracing::subscriber::set_global_default(subscriber)?; let _ = rustls::crypto::aws_lc_rs::default_provider().install_default(); let config: Config = toml::from_str(std::fs::read_to_string("./config.toml")?.as_str())?; - // Essentially this is meant to be a sentinel for "SCREENSHOT NOW!" + // Essentially this is meant to be a sentinel for "SCREENSHOT NOW!". + // None will stop the task immediately, so basically this is a really bad way of implementing cancellation + // (that isn't used currently) let (tx, _) = tokio::sync::broadcast::channel::>(10); let gmt_timezone = ArcTz::new(Tz::named("GMT")?); @@ -70,31 +73,36 @@ async fn main() -> anyhow::Result<()> { tracing::info!("Adding node {id} : {:?}", node); let mut _rx = tx.subscribe(); + + // These clones are scary, but they're only done once. + // Additionally, in the case of the timezone, no actual clone is performed. let id_clone = id.clone(); let node_clone = node.clone(); let root = config.root_path.clone(); - let tz = gmt_timezone.clone(); + // Spawn the per-node task. let _: JoinHandle> = tokio::spawn(async move { while let Some(_) = _rx.recv().await? { let span = tracing::span!(Level::INFO, "node screenshot", node = id_clone.as_str()); + let now = chrono::Utc::now().with_timezone(&tz); - let now = { chrono::Utc::now().with_timezone(&tz) }; - + // start with /yyyy-mm-dd let date_path = root.join(format!( - "{:02}-{:02}-{:02}", + "{:04}-{:02}-{:02}", now.year(), now.month(), now.day() )); + // then /yyyy-mm-dd/[node] let node_path = date_path.join(&id_clone); if !node_path.exists() { std::fs::create_dir_all(&node_path)?; } + // add target webp path finally let file_path = node_path.join(format!( "{:02}-{:02}-{:02}.webp", now.hour(), @@ -102,6 +110,7 @@ async fn main() -> anyhow::Result<()> { now.second() )); + // do it! match shotter::take_one_screenshot( &node_clone.url, &node_clone.origin, @@ -126,8 +135,10 @@ async fn main() -> anyhow::Result<()> { } loop { + // Request a screenshot tx.send(Some(()))?; + // Wait for the next :xx:00 minute let dur = duration_until_next_minute(); tracing::info!("Waiting {:?}", dur); tokio::time::sleep(dur).await;