Add retry logic to metadata fetch, don't crash on 500

This commit is contained in:
Tyler Hallada 2021-07-25 18:03:54 -04:00
parent 39ae7703b0
commit 523f3eeb3f
2 changed files with 42 additions and 43 deletions

View File

@ -304,26 +304,13 @@ pub async fn main() -> Result<()> {
continue; continue;
} }
} else { } else {
warn!("file has no metadata link"); warn!("file has no metadata link, continuing with download");
} }
Ok(())
} }
Err(err) => { Err(err) => {
if let Some(reqwest_err) = err.downcast_ref::<reqwest::Error>() { warn!(error = %err, "error retreiving metadata for file, continuing with download");
if reqwest_err.status() == Some(StatusCode::NOT_FOUND) {
warn!(
status = ?reqwest_err.status(),
"metadata for file not found on server"
);
Ok(())
} else {
Err(err)
} }
} else { };
Err(err)
}
}
}?;
let download_link_resp = let download_link_resp =
nexus_api::download_link::get(&client, db_mod.nexus_mod_id, api_file.file_id) nexus_api::download_link::get(&client, db_mod.nexus_mod_id, api_file.file_id)

View File

@ -2,7 +2,8 @@ use anyhow::{anyhow, Result};
use reqwest::Client; use reqwest::Client;
use serde_json::Value; use serde_json::Value;
use std::env; use std::env;
use tracing::{info, instrument}; use tokio::time::sleep;
use tracing::{info, instrument, warn};
use super::files::ApiFile; use super::files::ApiFile;
use super::USER_AGENT; use super::USER_AGENT;
@ -44,21 +45,30 @@ fn has_plugin(json: &Value) -> Result<bool> {
#[instrument(skip(client, api_file), fields(metadata_link = api_file.content_preview_link.unwrap_or("null")))] #[instrument(skip(client, api_file), fields(metadata_link = api_file.content_preview_link.unwrap_or("null")))]
pub async fn contains_plugin(client: &Client, api_file: &ApiFile<'_>) -> Result<Option<bool>> { pub async fn contains_plugin(client: &Client, api_file: &ApiFile<'_>) -> Result<Option<bool>> {
for attempt in 1..=3 {
if let Some(metadata_link) = api_file.content_preview_link { if let Some(metadata_link) = api_file.content_preview_link {
let res = client let res = match client
.get(metadata_link) .get(metadata_link)
.header("accept", "application/json") .header("accept", "application/json")
.header("apikey", env::var("NEXUS_API_KEY")?) .header("apikey", env::var("NEXUS_API_KEY")?)
.header("user-agent", USER_AGENT) .header("user-agent", USER_AGENT)
.send() .send()
.await? .await?
.error_for_status()?; .error_for_status()
{
Ok(res) => res,
Err(err) => {
warn!(error = %err, attempt, "Failed to get metadata for file, trying again after 1 second");
sleep(std::time::Duration::from_secs(1)).await;
continue;
}
};
info!(status = %res.status(), "fetched file metadata from API"); info!(status = %res.status(), "fetched file metadata from API");
let json = res.json::<Value>().await?; let json = res.json::<Value>().await?;
match json.get("children") { match json.get("children") {
None => Ok(Some(false)), None => return Ok(Some(false)),
Some(children) => { Some(children) => {
let children = children let children = children
.as_array() .as_array()
@ -68,10 +78,12 @@ pub async fn contains_plugin(client: &Client, api_file: &ApiFile<'_>) -> Result<
return Ok(Some(true)); return Ok(Some(true));
} }
} }
Ok(Some(false)) return Ok(Some(false));
} }
} }
} else { } else {
Ok(None) return Ok(None);
} }
}
Err(anyhow!("Failed to get metadata for file in three attempts"))
} }