Download workers only commit suicide when all chapters are retrieved

This commit is contained in:
blank X 2021-01-11 13:46:56 +07:00
parent 05cc8ed95b
commit e8470cb8b9
3 changed files with 27 additions and 20 deletions

2
Cargo.lock generated
View File

@ -330,7 +330,7 @@ dependencies = [
[[package]]
name = "mangafetchi"
version = "0.1.4"
version = "0.1.5"
dependencies = [
"quick-xml",
"reqwest",

View File

@ -1,6 +1,6 @@
[package]
name = "mangafetchi"
version = "0.1.4"
version = "0.1.5"
authors = ["blank X <theblankx@protonmail.com>"]
edition = "2018"

View File

@ -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<Mutex<Vec<(String, PathBuf, String)>>> = Arc::new(Mutex::new(Vec::new()));
let mut handles: Option<Vec<JoinHandle<()>>> = None;
let mutex = Arc::new(Mutex::new(DownloadData { data: Vec::new(), is_done: false }));
let handles: Vec<JoinHandle<()>> = 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<Mutex<Vec<(String, PathBuf, String)>>>) -> Vec<JoinHandle<()>> {
async fn summon_handles(client: reqwest::Client, mutex: Arc<Mutex<DownloadData>>) -> Vec<JoinHandle<()>> {
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<Mutex<Vec<(String, P
loop {
let cloned_mutex = Arc::clone(&tcloned_mutex);
let cloned_client = tcloned_client.clone();
let mut vec = cloned_mutex.lock().await;
if vec.is_empty() {
break;
let mut download_data = cloned_mutex.lock().await;
if download_data.data.is_empty() {
if download_data.is_done {
break;
}
drop(download_data);
sleep(Duration::from_millis(NO_ITEM_WAIT_TIME)).await;
continue;
}
let (url, file_name, referer) = vec.remove(0);
drop(vec);
let (url, file_name, referer) = download_data.data.remove(0);
drop(download_data);
eprintln!("[DW{}] Downloading {} to {}", worker_id, &url, file_name.display());
loop {
if utils::download_file(cloned_client.clone(), &url, &file_name, &referer).await.unwrap() {
@ -109,3 +111,8 @@ async fn summon_handles(client: reqwest::Client, mutex: Arc<Mutex<Vec<(String, P
}
handles
}
struct DownloadData {
pub data: Vec<(String, PathBuf, String)>,
pub is_done: bool
}