Compare commits

...

2 Commits

Author SHA1 Message Date
Zynh Ludwig 772bda8e93 move logging for clarity 2024-08-28 19:03:47 -07:00
Zynh Ludwig 7444fc4f9b fixed sweeper module 2024-08-28 19:02:30 -07:00
2 changed files with 19 additions and 19 deletions

View File

@ -17,7 +17,7 @@ use nyazoom_headers::ForwardedFor;
use sanitize_filename_reader_friendly::sanitize; use sanitize_filename_reader_friendly::sanitize;
use std::{io, net::SocketAddr, path::Path, time::Duration}; use std::{io, net::SocketAddr, path::Path};
use tokio_util::{ use tokio_util::{
compat::FuturesAsyncWriteCompatExt, compat::FuturesAsyncWriteCompatExt,
@ -65,7 +65,7 @@ async fn main() -> io::Result<()> {
let state = cache::fetch_cache().await; let state = cache::fetch_cache().await;
let _ = sweeper::spawn_sweeper(); sweeper::spawn_sweeper(state.clone());
// Router Setup // Router Setup
let app = Router::new() let app = Router::new()
@ -87,7 +87,6 @@ async fn main() -> io::Result<()> {
// Server creation // Server creation
let addr = SocketAddr::from(([0, 0, 0, 0], 3000)); let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
tracing::debug!("listening on http://{}/", addr);
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap(); let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
axum::serve( axum::serve(
listener, listener,
@ -96,6 +95,8 @@ async fn main() -> io::Result<()> {
.await .await
.unwrap(); .unwrap();
tracing::debug!("listening on http://{}/", addr);
Ok(()) Ok(())
} }

View File

@ -1,23 +1,22 @@
use tokio::task::JoinHandle; use std::time::Duration;
pub fn spawn_sweeper() -> JoinHandle<!> { use crate::state::{AppState, AsyncRemoveRecord};
// 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; /// Spawn a repeating task that will clean files periodically
pub fn spawn_sweeper(state: AppState) {
tokio::spawn(async move {
loop {
tokio::time::sleep(Duration::from_secs(15 * 60)).await;
tracing::info!("Cleaning Sweep!");
for (key, record) in records.clone().into_iter() { let mut records = state.records.lock().await;
if !record.can_be_downloaded() {
tracing::info!("culling: {:?}", record); for (key, record) in records.clone().into_iter() {
records.remove_record(&key).await.unwrap(); if !record.can_be_downloaded() {
} tracing::info!("culling: {:?}", record);
records.remove_record(&key).await.unwrap();
} }
} }
} }
}) });
} }