pepper some comments and clean up a bit more

This commit is contained in:
Lily Tsuru 2024-09-30 07:42:24 -04:00
parent be1d5e2f57
commit 8c9b81de90

View file

@ -43,6 +43,7 @@ struct Config {
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
// Init tracing
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
let subscriber = FmtSubscriber::builder() let subscriber = FmtSubscriber::builder()
.with_max_level(Level::TRACE) .with_max_level(Level::TRACE)
@ -61,7 +62,9 @@ async fn main() -> anyhow::Result<()> {
let config: Config = toml::from_str(std::fs::read_to_string("./config.toml")?.as_str())?; 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::<Option<()>>(10); let (tx, _) = tokio::sync::broadcast::channel::<Option<()>>(10);
let gmt_timezone = ArcTz::new(Tz::named("GMT")?); let gmt_timezone = ArcTz::new(Tz::named("GMT")?);
@ -70,31 +73,36 @@ async fn main() -> anyhow::Result<()> {
tracing::info!("Adding node {id} : {:?}", node); tracing::info!("Adding node {id} : {:?}", node);
let mut _rx = tx.subscribe(); 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 id_clone = id.clone();
let node_clone = node.clone(); let node_clone = node.clone();
let root = config.root_path.clone(); let root = config.root_path.clone();
let tz = gmt_timezone.clone(); let tz = gmt_timezone.clone();
// Spawn the per-node task.
let _: JoinHandle<anyhow::Result<()>> = tokio::spawn(async move { let _: JoinHandle<anyhow::Result<()>> = tokio::spawn(async move {
while let Some(_) = _rx.recv().await? { while let Some(_) = _rx.recv().await? {
let span = tracing::span!(Level::INFO, "node screenshot", node = id_clone.as_str()); 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!( let date_path = root.join(format!(
"{:02}-{:02}-{:02}", "{:04}-{:02}-{:02}",
now.year(), now.year(),
now.month(), now.month(),
now.day() now.day()
)); ));
// then /yyyy-mm-dd/[node]
let node_path = date_path.join(&id_clone); let node_path = date_path.join(&id_clone);
if !node_path.exists() { if !node_path.exists() {
std::fs::create_dir_all(&node_path)?; std::fs::create_dir_all(&node_path)?;
} }
// add target webp path finally
let file_path = node_path.join(format!( let file_path = node_path.join(format!(
"{:02}-{:02}-{:02}.webp", "{:02}-{:02}-{:02}.webp",
now.hour(), now.hour(),
@ -102,6 +110,7 @@ async fn main() -> anyhow::Result<()> {
now.second() now.second()
)); ));
// do it!
match shotter::take_one_screenshot( match shotter::take_one_screenshot(
&node_clone.url, &node_clone.url,
&node_clone.origin, &node_clone.origin,
@ -126,8 +135,10 @@ async fn main() -> anyhow::Result<()> {
} }
loop { loop {
// Request a screenshot
tx.send(Some(()))?; tx.send(Some(()))?;
// Wait for the next :xx:00 minute
let dur = duration_until_next_minute(); let dur = duration_until_next_minute();
tracing::info!("Waiting {:?}", dur); tracing::info!("Waiting {:?}", dur);
tokio::time::sleep(dur).await; tokio::time::sleep(dur).await;