Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add installation retries #142

Merged
merged 3 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ update-informer = "0.6.0"
tokio = { version = "1.24.1", features = ["full"] }
async-trait = "0.1.61"
retry = "2.0.0"
tokio-retry = "0.3.0"


[target.'cfg(target_os = "linux")'.dependencies]
Expand Down
5 changes: 1 addition & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ pub enum Error {
#[diagnostic(code(espup::toolchain::rust::failed_to_get_latest_version))]
#[error("{} Failed To serialize Json from string.", emoji::ERROR)]
FailedToSerializeJson,
#[diagnostic(code(espup::toolchain::rust::xtensa_rust_already_installed))]
#[error("{} Previous installation of Rust Toolchain exists in: '{0}'. Please, remove the directory before new installation.", emoji::ERROR)]
XtensaToolchainAlreadyInstalled(String),
#[diagnostic(code(espup::toolchain::rust::invalid_version))]
#[error(
"{} Invalid toolchain version '{0}'. Verify that the format is correct: '<major>.<minor>.<patch>.<subpatch>' or '<major>.<minor>.<patch>', and that the release exists in https://github.com/esp-rs/rust-build/releases",
Expand All @@ -63,7 +60,7 @@ pub enum Error {
#[error("{} Failed to add CMake to ESP-IDF tools", emoji::ERROR)]
FailedToInstantiateCmake,
#[diagnostic(code(espup::toolchain::espidf::failed_to_create_esp_idf_install_closure))]
#[error("{} Failed to create ESP-IDF install closure", emoji::ERROR)]
#[error("{} Failed to create ESP-IDF install closure", emoji::ERROR)]
FailedToCreateEspIdfInstallClosure,
#[diagnostic(code(espup::toolchain::espidf::failed_to_install_esp_idf))]
#[error("{} Failed to install ESP-IDF", emoji::ERROR)]
Expand Down
15 changes: 14 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::{
path::PathBuf,
};
use tokio::sync::mpsc;
use tokio_retry::{strategy::FixedInterval, Retry};

#[cfg(windows)]
const DEFAULT_EXPORT_FILE: &str = "export-esp.ps1";
Expand Down Expand Up @@ -243,8 +244,20 @@ async fn install(args: InstallOpts) -> Result<()> {
let (tx, mut rx) = mpsc::channel::<Result<Vec<String>, Error>>(installable_items);
for app in to_install {
let tx = tx.clone();
let retry_strategy = FixedInterval::from_millis(50).take(3);
tokio::spawn(async move {
let res = app.install().await;
let res = Retry::spawn(retry_strategy, || async {
let res = app.install().await;
if res.is_err() {
warn!(
"{} Installation for '{}' failed, retriying",
emoji::WARN,
app.name()
);
}
res
})
.await;
tx.send(res).await.unwrap();
});
}
Expand Down
4 changes: 4 additions & 0 deletions src/toolchain/espidf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ impl Installable for EspIdfRepo {

Ok(exports)
}

fn name(&self) -> String {
"ESP-IDF".to_string()
}
}

/// Gets the esp-idf installation path.
Expand Down
4 changes: 4 additions & 0 deletions src/toolchain/gcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ impl Installable for Gcc {

Ok(exports)
}

fn name(&self) -> String {
format!("GCC ({})", self.toolchain_name)
}
}

/// Gets the name of the GCC arch based on the host triple.
Expand Down
4 changes: 4 additions & 0 deletions src/toolchain/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,8 @@ impl Installable for Llvm {

Ok(exports)
}

fn name(&self) -> String {
"LLVM".to_string()
}
}
14 changes: 10 additions & 4 deletions src/toolchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use reqwest::header;
use retry::{delay::Fixed, retry};
use std::{
env,
fs::{create_dir_all, File},
fs::{create_dir_all, remove_file, File},
io::Write,
path::Path,
};
Expand All @@ -24,6 +24,8 @@ pub mod rust;
pub trait Installable {
/// Install some application, returning a vector of any required exports
async fn install(&self) -> Result<Vec<String>, Error>;
/// Returns the name of the toolchain being installeds
fn name(&self) -> String;
}

/// Downloads a file from a URL and uncompresses it, if necesary, to the output directory.
Expand All @@ -35,8 +37,12 @@ pub async fn download_file(
) -> Result<String, Error> {
let file_path = format!("{}/{}", output_directory, file_name);
if Path::new(&file_path).exists() {
info!("{} Using cached file: '{}'", emoji::INFO, file_path);
return Ok(file_path);
warn!(
"{} File '{}' already exists, deleting it before download.",
emoji::WARN,
file_path
);
remove_file(&file_path)?;
} else if !Path::new(&output_directory).exists() {
info!(
"{} Creating directory: '{}'",
Expand All @@ -50,7 +56,7 @@ pub async fn download_file(
info!(
"{} Downloading file '{}' from '{}'",
emoji::DOWNLOAD,
file_name,
&file_path,
url
);
let resp = reqwest::get(&url).await?;
Expand Down
21 changes: 18 additions & 3 deletions src/toolchain/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,12 @@ impl Installable for XtensaRust {
#[cfg(windows)]
let toolchain_path = self.toolchain_destination.clone().join("esp");
if toolchain_path.exists() {
return Err(Error::XtensaToolchainAlreadyInstalled(
toolchain_path.display().to_string(),
));
warn!(
"{} Previous installation of Xtensa Rust exists in: '{}'. Reusing this installation.",
emoji::WARN,
&toolchain_path.display()
);
return Ok(vec![]);
}
info!(
"{} Installing Xtensa Rust {} toolchain",
Expand Down Expand Up @@ -237,6 +240,10 @@ impl Installable for XtensaRust {

Ok(vec![]) // No exports
}

fn name(&self) -> String {
"Xtensa Rust".to_string()
}
}

#[derive(Hash, Eq, PartialEq, Debug, Clone, Serialize, Deserialize, Default)]
Expand Down Expand Up @@ -288,6 +295,10 @@ impl Installable for Crate {

Ok(vec![]) // No exports
}

fn name(&self) -> String {
format!("crate {}", self.name)
}
}

pub struct RiscVTarget {
Expand Down Expand Up @@ -352,6 +363,10 @@ impl Installable for RiscVTarget {

Ok(vec![]) // No exports
}

fn name(&self) -> String {
"RISC-V rust target".to_string()
}
}

/// Gets the artifact extension based on the host architecture.
Expand Down