From 050b4f8ac09758e1e8a920ef82350a84c72b82ed Mon Sep 17 00:00:00 2001 From: OMGeeky Date: Sat, 15 Jul 2023 17:44:10 +0200 Subject: [PATCH] fix progress handle not exiting (cancellation token?) --- Cargo.toml | 2 +- src/lib.rs | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index eb14d90..05f168d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "twitch_data" -version = "0.2.7" +version = "0.2.8" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/lib.rs b/src/lib.rs index 6f95b4b..d5caded 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ use std::{ fmt::Debug, path::{Path, PathBuf}, result::Result as StdResult, - sync::atomic::{AtomicUsize, Ordering}, + sync::atomic::{AtomicBool, AtomicUsize, Ordering}, sync::Arc, }; @@ -521,7 +521,7 @@ impl<'a> TwitchClient<'a> { } else if amount_of_threads > amount_of_parts { amount_of_threads = amount_of_parts; } - let (completed, _) = Self::create_progress_indicator( + let (completed, progress_done, progress_handle) = Self::create_progress_indicator( amount_of_parts, Duration::from_secs(5), "Downloading Parts", @@ -538,6 +538,10 @@ impl<'a> TwitchClient<'a> { .buffer_unordered(amount_of_threads) .collect::>>(); let files = files.await; + info!("downloaded parts"); + //tell the progress indicator to stop and wait for it to exit + progress_done.fetch_or(true, Ordering::Relaxed); + let _ = progress_handle.await; info!("downloaded all parts of the video"); Ok(files) @@ -547,27 +551,35 @@ impl<'a> TwitchClient<'a> { amount_of_parts: usize, report_frequency: Duration, title: impl Into, - ) -> (Arc, JoinHandle<()>) { + ) -> (Arc, Arc, JoinHandle<()>) { let completed = Arc::new(AtomicUsize::new(0)); + let canceled = Arc::new(AtomicBool::new(false)); let title = title.into(); let progress_handle = { let completed = Arc::clone(&completed); + let canceled = Arc::clone(&canceled); tokio::spawn(async move { - loop { + while canceled.load(Ordering::Relaxed) { let current_progress = completed.load(Ordering::Relaxed); info!( - "{}: {:>6.2}% ({}/{})", + "{}: {:>6.2}% ({}/{}) [{}]", title, (current_progress as f64 / amount_of_parts as f64) * 100.0, current_progress, - amount_of_parts + amount_of_parts, + Arc::strong_count(&completed) ); tokio::time::sleep(report_frequency).await; // sleep for a while } + let current_progress = completed.load(Ordering::Relaxed); + info!( + "{} Completed! ({}/{})", + title, current_progress, amount_of_parts + ); }) }; - (completed, progress_handle) + (completed, canceled, progress_handle) } async fn get_parts(&self, url: &String) -> Result<(u64, HashMap)> {