Attempt to download file 3 times before crashing

This commit is contained in:
Tyler Hallada 2021-07-18 01:45:08 -04:00
parent 4e500f0b0b
commit 62c0f5295f

View File

@ -6,7 +6,7 @@ use std::{env, time::Duration};
use tempfile::tempfile; use tempfile::tempfile;
use tokio::fs::File; use tokio::fs::File;
use tokio_util::compat::FuturesAsyncReadCompatExt; use tokio_util::compat::FuturesAsyncReadCompatExt;
use tracing::{info, instrument}; use tracing::{info, instrument, warn};
use super::{rate_limit_wait_duration, GAME_NAME, USER_AGENT}; use super::{rate_limit_wait_duration, GAME_NAME, USER_AGENT};
@ -53,25 +53,33 @@ impl DownloadLinkResponse {
#[instrument(skip(self, client))] #[instrument(skip(self, client))]
pub async fn download_file(&self, client: &Client) -> Result<File> { pub async fn download_file(&self, client: &Client) -> Result<File> {
let mut tokio_file = File::from_std(tempfile()?); for attempt in 1..=3 {
let res = client let mut tokio_file = File::from_std(tempfile()?);
.get(self.link()?) let res = client
.header("apikey", env::var("NEXUS_API_KEY")?) .get(self.link()?)
.header("user-agent", USER_AGENT) .header("apikey", env::var("NEXUS_API_KEY")?)
.send() .header("user-agent", USER_AGENT)
.await? .send()
.error_for_status()?; .await?
info!(status = %res.status(), "downloading file from nexus"); .error_for_status()?;
info!(status = %res.status(), "downloading file from nexus");
// See: https://github.com/benkay86/async-applied/blob/master/reqwest-tokio-compat/src/main.rs // See: https://github.com/benkay86/async-applied/blob/master/reqwest-tokio-compat/src/main.rs
let mut byte_stream = res let mut byte_stream = res
.bytes_stream() .bytes_stream()
.map_err(|e| futures::io::Error::new(futures::io::ErrorKind::Other, e)) .map_err(|e| futures::io::Error::new(futures::io::ErrorKind::Other, e))
.into_async_read() .into_async_read()
.compat(); .compat();
tokio::io::copy(&mut byte_stream, &mut tokio_file).await?; match tokio::io::copy(&mut byte_stream, &mut tokio_file).await {
Ok(_) => {
return Ok(tokio_file); return Ok(tokio_file);
}
Err(err) => {
warn!(error = %err, attempt, "Failed to download file, trying again");
}
}
}
Err(anyhow!("Failed to download file in three attempts"))
} }
} }