Improve retry on download failure

This commit is contained in:
Taiki Endo
2023-01-15 19:55:57 +09:00
parent d538488fe5
commit f6e4dc6296
3 changed files with 16 additions and 6 deletions

View File

@@ -145,6 +145,8 @@ jobs:
- name: Install Rust - name: Install Rust
run: rustup toolchain add nightly --no-self-update && rustup default nightly run: rustup toolchain add nightly --no-self-update && rustup default nightly
- run: tools/manifest.sh - run: tools/manifest.sh
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: git add -N . && git diff --exit-code - run: git add -N . && git diff --exit-code
if: github.repository_owner != 'taiki-e' || github.event_name != 'schedule' && !(github.event_name == 'push' && github.ref == 'refs/heads/main') if: github.repository_owner != 'taiki-e' || github.event_name != 'schedule' && !(github.event_name == 'push' && github.ref == 'refs/heads/main')
- id: diff - id: diff

View File

@@ -11,7 +11,7 @@ x() {
) )
} }
retry() { retry() {
for i in {1..5}; do for i in {1..10}; do
if "$@"; then if "$@"; then
return 0 return 0
else else

View File

@@ -385,24 +385,32 @@ fn replace_vars(s: &str, package: &str, version: &str, platform: HostPlatform) -
} }
fn download(url: &str) -> Result<ureq::Response> { fn download(url: &str) -> Result<ureq::Response> {
let token = env::var("INTERNAL_CODEGEN_GH_PAT").ok(); let mut token1 = env::var("INTERNAL_CODEGEN_GH_PAT").ok().filter(|v| !v.is_empty());
let mut token2 = env::var("GITHUB_TOKEN").ok().filter(|v| !v.is_empty());
let mut retry = 0; let mut retry = 0;
let mut last_error; let mut last_error;
loop { loop {
let mut req = ureq::get(url); let mut req = ureq::get(url);
if let Some(token) = &token { if let Some(token) = &token1 {
req = req.set("Authorization", token);
} else if let Some(token) = &token2 {
req = req.set("Authorization", token); req = req.set("Authorization", token);
} }
match req.call() { match req.call() {
Ok(res) => return Ok(res), Ok(res) => return Ok(res),
Err(e) => last_error = Some(e), Err(e) => last_error = Some(e),
} }
if token1.is_some() {
token1 = None;
} else if token2.is_some() {
token2 = None;
}
retry += 1; retry += 1;
if retry > 5 { if retry > 10 {
break; break;
} }
eprintln!("download failed; retrying ({retry}/5)"); eprintln!("download failed; retrying ({retry}/10)");
std::thread::sleep(Duration::from_secs(retry * 2)); std::thread::sleep(Duration::from_secs(retry * 4));
} }
Err(last_error.unwrap().into()) Err(last_error.unwrap().into())
} }