Skip to content

Commit 82f15ef

Browse files
committed
retry failed deps downloads
1 parent bc82eae commit 82f15ef

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

build.rs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use actix_rt::spawn;
2+
use actix_rt::time::sleep;
23
use libflate::gzip;
34
use std::collections::hash_map::DefaultHasher;
45
use std::fs::File;
@@ -99,21 +100,37 @@ fn copy_cached_to_opened_file(source: &Path, outfile: &mut impl std::io::Write)
99100
}
100101

101102
async fn download_url_to_path(client: &awc::Client, url: &str, path: &Path) {
102-
let mut resp = client.get(url).send().await.unwrap_or_else(|err| {
103-
let path = make_url_path(url);
104-
panic!(
105-
"We need to download external frontend dependencies to build the static frontend. \n\
106-
Could not download static asset. You can manually download the file with: \n\
107-
curl {url:?} > {path:?} \n\
108-
{err}"
109-
)
110-
});
111-
if resp.status() != 200 {
112-
panic!("Received {} status code from {}", resp.status(), url);
103+
let mut attempt = 1;
104+
let max_attempts = 2;
105+
106+
loop {
107+
match client.get(url).send().await {
108+
Ok(mut resp) => {
109+
if resp.status() != 200 {
110+
panic!("Received {} status code from {}", resp.status(), url);
111+
}
112+
let bytes = resp.body().limit(128 * 1024 * 1024).await.unwrap();
113+
std::fs::write(path, &bytes)
114+
.expect("Failed to write external frontend dependency to local file");
115+
break;
116+
}
117+
Err(err) => {
118+
if attempt >= max_attempts {
119+
let path = make_url_path(url);
120+
panic!(
121+
"We need to download external frontend dependencies to build the static frontend. \n\
122+
Could not download static asset after {} attempts. You can manually download the file with: \n\
123+
curl {url:?} > {path:?} \n\
124+
{err}",
125+
max_attempts
126+
);
127+
}
128+
sleep(Duration::from_secs(1)).await;
129+
println!("cargo:warning=Retrying download of {url} after {err}.");
130+
attempt += 1;
131+
}
132+
}
113133
}
114-
let bytes = resp.body().limit(128 * 1024 * 1024).await.unwrap();
115-
std::fs::write(path, &bytes)
116-
.expect("Failed to write external frontend dependency to local file");
117134
}
118135

119136
// Given a filename, creates a new unique filename based on the file contents

0 commit comments

Comments
 (0)