From fccc22cd4a4a84211d8b61cf561d0f4463f500f1 Mon Sep 17 00:00:00 2001 From: jakka Date: Mon, 7 Jul 2025 13:18:57 +0300 Subject: better threading handling --- src/files.rs | 53 +++++++++++++++++++---------------------------------- src/main.rs | 6 +++++- 2 files changed, 24 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/files.rs b/src/files.rs index 6b5c20e..9793243 100644 --- a/src/files.rs +++ b/src/files.rs @@ -1,6 +1,7 @@ use anyhow::{Result, anyhow}; #[cfg(not(test))] use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle}; +use rayon::prelude::*; use rusqlite::Connection; use std::{ error::Error, @@ -133,7 +134,7 @@ pub fn index_files_recursively( Ok(()) } -pub fn reencode_files(conn: Connection, handler: Arc, threads: usize) -> Result<()> { +pub fn reencode_files(conn: Connection, handler: Arc) -> Result<()> { #[cfg(not(test))] let bar = ProgressBar::with_draw_target( Some(conn.get_toencode_number()?), @@ -144,41 +145,25 @@ pub fn reencode_files(conn: Connection, handler: Arc, threads: usize let files = conn.get_toencode_files()?; - let pool = rayon::ThreadPoolBuilder::new() - .num_threads(threads) - .build()?; - let lock = Arc::new(Mutex::new(conn)); - pool.scope(|scope| { - for file in files { - if handler.load(Ordering::SeqCst) { - scope.spawn(|_| { - let newconn = if let Ok(conn) = lock.lock() { - conn - } else { - eprintln!("Error setting up lock on file:\t{}", file.to_string_lossy()); - return; - }; - if !file.exists() { - let _ = newconn.remove_file(&file); - #[cfg(not(test))] - bar.dec_length(1); - return; - } - - if let Err(error) = handle_encode(&file) { - eprintln!("{}", FileError::new(&file, error)); - } else { - if let Err(error) = newconn.update_file(&file) { - eprintln!("{}", FileError::new(file, error)); - } - #[cfg(not(test))] - bar.inc(1) - } - }); + files.par_iter().for_each(|file| { + if handler.load(Ordering::SeqCst) { + let conn = match lock.lock() { + Ok(conn) => conn, + Err(_) => { + eprintln!("Lock poisoned on file:\t{}", file.to_string_lossy()); + return; + } + }; + if let Err(error) = handle_encode(file) { + eprintln!("{}", FileError::new(file, error)); } else { - break; + if let Err(error) = conn.update_file(file) { + eprintln!("{}", FileError::new(file, error)); + } + #[cfg(not(test))] + bar.inc(1) } } }); @@ -258,7 +243,7 @@ mod tests { let temp = handler.clone(); index_files_recursively(Path::new("./testfiles"), &conn, temp).unwrap(); println!("\n{}", conn.get_toencode_number().unwrap()); - reencode_files(conn, handler, 2).unwrap(); + reencode_files(conn, handler).unwrap(); let conn = Connection::new(Some(&dbname)).unwrap(); println!("\n{}", conn.get_toencode_number().unwrap()); std::fs::remove_file(dbname).unwrap(); diff --git a/src/main.rs b/src/main.rs index d1e4562..122ce60 100644 --- a/src/main.rs +++ b/src/main.rs @@ -116,7 +116,11 @@ fn main() -> Result<()> { if args.get_flag("doit") { let hanlder = running.clone(); let threads = *args.get_one::("threads").unwrap(); - files::reencode_files(conn, hanlder, threads)?; + let pool = rayon::ThreadPoolBuilder::new() + .num_threads(threads) + .build()?; + + pool.install(|| files::reencode_files(conn, hanlder))?; } Ok::<(), anyhow::Error>(()) } -- cgit v1.3.1