From e8470cb8b96dad498e2aeb5e72be018391741f4f Mon Sep 17 00:00:00 2001 From: blank X Date: Mon, 11 Jan 2021 13:46:56 +0700 Subject: [PATCH] Download workers only commit suicide when all chapters are retrieved --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/commands/download.rs | 43 +++++++++++++++++++++++----------------- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c201470..353b8a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -330,7 +330,7 @@ dependencies = [ [[package]] name = "mangafetchi" -version = "0.1.4" +version = "0.1.5" dependencies = [ "quick-xml", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index b4e0e38..5e6563b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mangafetchi" -version = "0.1.4" +version = "0.1.5" authors = ["blank X "] edition = "2018" diff --git a/src/commands/download.rs b/src/commands/download.rs index 49f9be9..7127680 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -13,6 +13,7 @@ extern crate reqwest; const DOWNLOAD_WORKERS: usize = 5; const NON_IMAGE_WAIT_TIME: u64 = 5000; +const NO_ITEM_WAIT_TIME: u64 = 1000; pub async fn run(mut args: env::Args) { let manga_id = match args.next() { @@ -50,8 +51,8 @@ pub async fn run(mut args: env::Args) { exit(1); } }; - let mutex: Arc>> = Arc::new(Mutex::new(Vec::new())); - let mut handles: Option>> = None; + let mutex = Arc::new(Mutex::new(DownloadData { data: Vec::new(), is_done: false })); + let handles: Vec> = summon_handles(client.clone(), Arc::clone(&mutex)).await; for chapter in chapters { let cloned_mutex = Arc::clone(&mutex); let chapter_pages = utils::get_pages(client.clone(), &chapter, &manga_id).await.unwrap(); @@ -64,22 +65,18 @@ pub async fn run(mut args: env::Args) { } } if !to_extend.is_empty() { - cloned_mutex.lock().await.extend(to_extend); - if handles.is_none() { - handles = Some(summon_handles(client.clone(), cloned_mutex).await); - } + cloned_mutex.lock().await.data.extend(to_extend); } } - if handles.is_some() { - for handle in handles.unwrap() { - handle.await.unwrap(); - } - } else { - eprintln!("Everything has already been downloaded"); + { + mutex.lock().await.is_done = true; + } + for handle in handles { + handle.await.unwrap(); } } -async fn summon_handles(client: reqwest::Client, mutex: Arc>>) -> Vec> { +async fn summon_handles(client: reqwest::Client, mutex: Arc>) -> Vec> { let mut handles = Vec::with_capacity(DOWNLOAD_WORKERS); for worker_id in 0..DOWNLOAD_WORKERS { let tcloned_mutex = Arc::clone(&mutex); @@ -89,12 +86,17 @@ async fn summon_handles(client: reqwest::Client, mutex: Arc, + pub is_done: bool +}