Add retry logic to metadata fetch, don't crash on 500
This commit is contained in:
parent
39ae7703b0
commit
523f3eeb3f
19
src/main.rs
19
src/main.rs
@ -304,26 +304,13 @@ pub async fn main() -> Result<()> {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
warn!("file has no metadata link");
|
||||
warn!("file has no metadata link, continuing with download");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Err(err) => {
|
||||
if let Some(reqwest_err) = err.downcast_ref::<reqwest::Error>() {
|
||||
if reqwest_err.status() == Some(StatusCode::NOT_FOUND) {
|
||||
warn!(
|
||||
status = ?reqwest_err.status(),
|
||||
"metadata for file not found on server"
|
||||
);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(err)
|
||||
warn!(error = %err, "error retreiving metadata for file, continuing with download");
|
||||
}
|
||||
} else {
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
}?;
|
||||
};
|
||||
|
||||
let download_link_resp =
|
||||
nexus_api::download_link::get(&client, db_mod.nexus_mod_id, api_file.file_id)
|
||||
|
@ -2,7 +2,8 @@ use anyhow::{anyhow, Result};
|
||||
use reqwest::Client;
|
||||
use serde_json::Value;
|
||||
use std::env;
|
||||
use tracing::{info, instrument};
|
||||
use tokio::time::sleep;
|
||||
use tracing::{info, instrument, warn};
|
||||
|
||||
use super::files::ApiFile;
|
||||
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")))]
|
||||
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 {
|
||||
let res = client
|
||||
let res = match client
|
||||
.get(metadata_link)
|
||||
.header("accept", "application/json")
|
||||
.header("apikey", env::var("NEXUS_API_KEY")?)
|
||||
.header("user-agent", USER_AGENT)
|
||||
.send()
|
||||
.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");
|
||||
let json = res.json::<Value>().await?;
|
||||
|
||||
match json.get("children") {
|
||||
None => Ok(Some(false)),
|
||||
None => return Ok(Some(false)),
|
||||
Some(children) => {
|
||||
let children = children
|
||||
.as_array()
|
||||
@ -68,10 +78,12 @@ pub async fn contains_plugin(client: &Client, api_file: &ApiFile<'_>) -> Result<
|
||||
return Ok(Some(true));
|
||||
}
|
||||
}
|
||||
Ok(Some(false))
|
||||
return Ok(Some(false));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Ok(None)
|
||||
return Ok(None);
|
||||
}
|
||||
}
|
||||
Err(anyhow!("Failed to get metadata for file in three attempts"))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user