From e6c996d7d843ab83f4e74e8d6cb876de2a46b0a2 Mon Sep 17 00:00:00 2001 From: Zynh0722 Date: Thu, 27 Jul 2023 08:58:13 -0700 Subject: [PATCH] actually cull files? --- src/main.rs | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 206ab3a..ce741de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,19 +57,34 @@ async fn main() -> io::Result<()> { .with(tracing_subscriber::fmt::layer()) .init(); - // Spawn a repeating task that will clean files periodically - tokio::spawn(async { - loop { - tracing::info!("Cleaning Sweep!"); - tokio::time::sleep(Duration::from_secs(15 * 60)).await - } - }); - // uses create_dir_all to create both .cache and serve inside it in one go util::make_dir(".cache/serve").await?; let state = cache::fetch_cache().await; + // Spawn a repeating task that will clean files periodically + tokio::spawn({ + let state = state.clone(); + async move { + loop { + tracing::info!("Cleaning Sweep!"); + + let mut records = state.records.lock().await; + + for (key, record) in records.clone().into_iter() { + if !record.can_be_downloaded() { + tracing::info!("{:?} should be culled", record); + let _ = tokio::fs::remove_file(&record.file).await; + records.remove(key.as_str()); + cache::write_to_cache(&records).await.unwrap(); + } + } + + tokio::time::sleep(Duration::from_secs(15 * 60)).await + } + } + }); + // Router Setup let app = Router::new() .route("/", get(welcome))