From 5564d23536a37c556b43ad24b29eedfa46305116 Mon Sep 17 00:00:00 2001 From: quexeky Date: Mon, 28 Oct 2024 20:57:42 +1100 Subject: [PATCH] Queue is running game downloads sequentially now --- src-tauri/src/downloads/download_agent.rs | 9 ++++++--- src-tauri/src/downloads/download_commands.rs | 4 ++-- src-tauri/src/downloads/progress.rs | 18 +++++++++++++++++- src-tauri/src/tests/progress_tests.rs | 2 +- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src-tauri/src/downloads/download_agent.rs b/src-tauri/src/downloads/download_agent.rs index 28e93b5..13b5bbb 100644 --- a/src-tauri/src/downloads/download_agent.rs +++ b/src-tauri/src/downloads/download_agent.rs @@ -67,12 +67,15 @@ impl GameDownloadAgent { self.ensure_manifest_exists().await } - pub fn begin_download(&self, max_threads: usize) -> Result<(), GameDownloadError> { + pub async fn begin_download(&self, max_threads: usize) -> Result<(), GameDownloadError> { self.change_state(GameDownloadState::Downloading); // TODO we're coping the whole context thing // It's not necessary, I just can't figure out to make the borrow checker happy - self.progress - .run_contexts_parallel(self.contexts.lock().unwrap().to_vec(), max_threads); + { + let lock = self.contexts.lock().unwrap().to_vec(); + self.progress + .run_context_parallel(lock, max_threads).await; + } Ok(()) } diff --git a/src-tauri/src/downloads/download_commands.rs b/src-tauri/src/downloads/download_commands.rs index 05a17ef..4f38514 100644 --- a/src-tauri/src/downloads/download_commands.rs +++ b/src-tauri/src/downloads/download_commands.rs @@ -46,8 +46,8 @@ pub async fn start_game_downloads( } }; info!("Downloading game"); - start_game_download(max_threads, download_agent.unwrap()).await?; { + start_game_download(max_threads, download_agent.unwrap()).await?; let mut lock = state.lock().unwrap(); lock.game_downloads.remove_entry(¤t_id); } @@ -70,7 +70,7 @@ pub async fn start_game_download( download_agent.generate_job_contexts(&local_manifest, download_agent.version.clone(), download_agent.id.clone()).unwrap(); - download_agent.begin_download(max_threads)?; + download_agent.begin_download(max_threads).await?; Ok(()) } \ No newline at end of file diff --git a/src-tauri/src/downloads/progress.rs b/src-tauri/src/downloads/progress.rs index 34dc16e..7e1c582 100644 --- a/src-tauri/src/downloads/progress.rs +++ b/src-tauri/src/downloads/progress.rs @@ -1,4 +1,6 @@ use rayon::ThreadPoolBuilder; +use uuid::timestamp::context; +use std::os::unix::thread; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; @@ -35,7 +37,7 @@ where self.counter.fetch_add(1, Ordering::Relaxed); } } - pub fn run_contexts_parallel(&self, contexts: Vec, max_threads: usize) { + pub fn run_contexts_parallel_background(&self, contexts: Vec, max_threads: usize) { let threads = ThreadPoolBuilder::new() // If max_threads == 0, then the limit will be determined // by Rayon's internal RAYON_NUM_THREADS @@ -48,6 +50,20 @@ where threads.spawn(move || f(context)); } } + pub async fn run_context_parallel(&self, contexts: Vec, max_threads: usize) { + let threads = ThreadPoolBuilder::new() + .num_threads(max_threads) + .build() + .unwrap(); + + threads.scope(|s| { + for context in contexts { + let f = self.f.clone(); + s.spawn(move |_| f(context)); + } + }); + + } pub fn get_progress(&self) -> usize { self.counter.load(Ordering::Relaxed) } diff --git a/src-tauri/src/tests/progress_tests.rs b/src-tauri/src/tests/progress_tests.rs index 1a0adc8..95b1b0a 100644 --- a/src-tauri/src/tests/progress_tests.rs +++ b/src-tauri/src/tests/progress_tests.rs @@ -13,7 +13,7 @@ fn test_progress_sequentially() { fn test_progress_parallel() { let counter = Arc::new(AtomicUsize::new(0)); let p = ProgressChecker::new(Box::new(test_fn), counter.clone()); - p.run_contexts_parallel((1..100).collect(), 10); + p.run_contexts_parallel_background((1..100).collect(), 10); } fn test_fn(int: usize) {