-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): autostart dev container if "run-locally = true" (#176)
- Loading branch information
1 parent
e0d97be
commit c1ad01a
Showing
6 changed files
with
280 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use soroban_cli::{commands as cli, CommandParser}; | ||
use std::error::Error; | ||
|
||
pub async fn start_local_stellar() -> Result<(), Box<dyn Error>> { | ||
let result = cli::container::StartCmd::parse_arg_vec(&["local"])? | ||
.run(&soroban_cli::commands::global::Args::default()) | ||
.await; | ||
|
||
match result { | ||
Ok(()) => { | ||
tokio::time::sleep(std::time::Duration::from_secs(10)).await; | ||
} | ||
Err(e) => { | ||
if e.to_string().contains("already in use") | ||
|| e.to_string().contains("port is already allocated") | ||
{ | ||
eprintln!("Container is already running, proceeding to health check..."); | ||
} else { | ||
return Err(Box::new(e)); | ||
} | ||
} | ||
} | ||
|
||
wait_for_stellar_health().await?; | ||
Ok(()) | ||
} | ||
async fn wait_for_stellar_health() -> Result<(), Box<dyn Error>> { | ||
let client = reqwest::Client::new(); | ||
let start_time = std::time::Instant::now(); | ||
let timeout = std::time::Duration::from_secs(60); | ||
loop { | ||
let elapsed_time = start_time.elapsed(); | ||
if elapsed_time > timeout { | ||
eprintln!("Timeout reached: stopping health checks."); | ||
return Err("Health check timed out".into()); | ||
} | ||
let res = client | ||
.post("http://localhost:8000/rpc") | ||
.header("Content-Type", "application/json") | ||
.body(r#"{"jsonrpc": "2.0", "id": 1, "method": "getHealth"}"#) | ||
.send() | ||
.await?; | ||
if res.status().is_success() { | ||
let health_status: serde_json::Value = res.json().await?; | ||
if health_status["result"]["status"] == "healthy" { | ||
break; | ||
} | ||
eprintln!("Stellar status is not healthy: {health_status:?}"); | ||
} else { | ||
eprintln!("Health check request failed with status: {}", res.status()); | ||
} | ||
tokio::time::sleep(std::time::Duration::from_secs(5)).await; | ||
eprintln!("Retrying health check."); | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.