Skip to content

Commit 8354cc9

Browse files
downloader: Add convinience methods to send updates and result
1 parent c550480 commit 8354cc9

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

src/downloader/request.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,32 @@ impl DownloadRequest {
6565
self.status.send(Status::InProgress(progress)).ok();
6666
}
6767
}
68+
69+
pub fn fail(self, e: Error) {
70+
let status = if matches!(e, Error::Download(DownloadError::Cancelled)) {
71+
Status::Cancelled
72+
} else {
73+
Status::Failed
74+
};
75+
self.status.send(status).ok();
76+
self.result.send(Err(e)).ok();
77+
}
78+
79+
pub fn retry(&self) {
80+
self.status.send(Status::Retrying).ok();
81+
}
82+
83+
pub fn cancel(self) {
84+
self.status.send(Status::Cancelled).ok();
85+
self.result
86+
.send(Err(Error::Download(DownloadError::Cancelled)))
87+
.ok();
88+
}
89+
90+
pub fn complete(self, file: File) {
91+
self.status.send(Status::Completed).ok();
92+
self.result.send(Ok(file)).ok();
93+
}
6894
}
6995

7096
#[derive(Debug)]

src/downloader/worker.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{DownloadProgress, DownloadRequest};
2-
use crate::{downloader::Status, error::DownloadError, Error};
2+
use crate::{error::DownloadError, Error};
33
use reqwest::Client;
44
use std::time::Duration;
55
use tokio::{fs::File, io::AsyncWriteExt};
@@ -25,38 +25,33 @@ pub(super) async fn download_thread(client: Client, mut req: DownloadRequest) {
2525
let max_attempts = req.config().max_retries();
2626
for attempt in 0..=(max_attempts + 1) {
2727
if attempt > max_attempts {
28-
req.status.send(Status::Failed).ok();
29-
req.result
30-
.send(Err(Error::Download(DownloadError::RetriesExhausted {
31-
last_error_msg: last_error
32-
.as_ref()
33-
.map(ToString::to_string)
34-
.unwrap_or_else(|| "Unknown Error".to_string()),
35-
})))
36-
.ok();
28+
req.fail(Error::Download(DownloadError::RetriesExhausted {
29+
last_error_msg: last_error
30+
.as_ref()
31+
.map(ToString::to_string)
32+
.unwrap_or_else(|| "Unknown Error".to_string()),
33+
}));
3734
return;
3835
}
3936

4037
if attempt > 0 {
41-
req.status.send(Status::Retrying).ok();
38+
req.retry();
4239
// Basic exponential backoff
4340
let delay_ms = 1000 * 2u64.pow(attempt as u32 - 1);
4441
let delay = Duration::from_millis(delay_ms);
4542

4643
tokio::select! {
4744
_ = tokio::time::sleep(delay) => {},
4845
_ = req.cancel.cancelled() => {
49-
req.status.send(Status::Failed).ok();
50-
req.result.send(Err(Error::Download(DownloadError::Cancelled))).ok();
46+
req.cancel();
5147
return;
5248
}
5349
}
5450
}
5551

5652
match download(client.clone(), &mut req).await {
5753
Ok(file) => {
58-
req.status.send(Status::Completed).ok();
59-
req.result.send(Ok(file)).ok();
54+
req.complete(file);
6055
return;
6156
}
6257
Err(e) => {
@@ -65,13 +60,7 @@ pub(super) async fn download_thread(client: Client, mut req: DownloadRequest) {
6560
continue;
6661
}
6762

68-
let status = if matches!(e, Error::Download(DownloadError::Cancelled)) {
69-
Status::Cancelled
70-
} else {
71-
Status::Failed
72-
};
73-
req.status.send(status).ok();
74-
req.result.send(Err(e)).ok();
63+
req.fail(e);
7564
return;
7665
}
7766
}

0 commit comments

Comments
 (0)