From 7128b9e3046653a6175c234a70fe7887476dc084 Mon Sep 17 00:00:00 2001 From: Zynh Ludwig Date: Wed, 28 Aug 2024 18:57:36 -0700 Subject: [PATCH] sweeper module --- src/main.rs | 21 ++------------------- src/sweeper.rs | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 19 deletions(-) create mode 100644 src/sweeper.rs diff --git a/src/main.rs b/src/main.rs index dff9719..fbefa22 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,6 +32,7 @@ mod cache; mod nyazoom_headers; pub mod ssr; mod state; +mod sweeper; mod util; mod views; @@ -64,25 +65,7 @@ async fn main() -> io::Result<()> { let state = cache::fetch_cache().await; - // Spawn a repeating task that will clean files periodically - tokio::spawn({ - let state = state.clone(); - async move { - loop { - tokio::time::sleep(Duration::from_secs(15 * 60)).await; - 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!("culling: {:?}", record); - records.remove_record(&key).await.unwrap(); - } - } - } - } - }); + let _ = sweeper::spawn_sweeper(); // Router Setup let app = Router::new() diff --git a/src/sweeper.rs b/src/sweeper.rs new file mode 100644 index 0000000..4485d73 --- /dev/null +++ b/src/sweeper.rs @@ -0,0 +1,23 @@ +use tokio::task::JoinHandle; + +pub fn spawn_sweeper() -> JoinHandle { + // Spawn a repeating task that will clean files periodically + tokio::spawn({ + let state = state.clone(); + async move { + loop { + tokio::time::sleep(Duration::from_secs(15 * 60)).await; + 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!("culling: {:?}", record); + records.remove_record(&key).await.unwrap(); + } + } + } + } + }) +}