Fix Vault retry logic - wait up to 20 seconds for vault to start

This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2025-12-15 17:37:00 -03:00
parent 7ccca11a25
commit 3c56b6e088

View file

@ -628,13 +628,14 @@ impl BootstrapManager {
let mut status_str = String::new(); let mut status_str = String::new();
let mut parsed_status: Option<serde_json::Value> = None; let mut parsed_status: Option<serde_json::Value> = None;
for attempt in 0..5 { let mut connection_refused = false;
for attempt in 0..10 {
if attempt > 0 { if attempt > 0 {
trace!( info!(
"Waiting for Vault to be ready (attempt {}/5)...", "Waiting for Vault to be ready (attempt {}/10)...",
attempt + 1 attempt + 1
); );
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
} }
let status_output = std::process::Command::new("sh") let status_output = std::process::Command::new("sh")
@ -654,16 +655,24 @@ impl BootstrapManager {
if status_str.contains("connection refused") if status_str.contains("connection refused")
|| stderr_str.contains("connection refused") || stderr_str.contains("connection refused")
{ {
warn!("Vault is not running (connection refused)"); connection_refused = true;
return Err(anyhow::anyhow!("Vault not running - needs to be started")); // Don't return immediately - keep retrying as vault may be starting
continue;
} }
connection_refused = false;
if let Ok(status) = serde_json::from_str::<serde_json::Value>(&status_str) { if let Ok(status) = serde_json::from_str::<serde_json::Value>(&status_str) {
parsed_status = Some(status); parsed_status = Some(status);
break; break;
} }
} }
// Only return error after all retries exhausted
if connection_refused {
warn!("Vault is not running after retries (connection refused)");
return Err(anyhow::anyhow!("Vault not running - needs to be started"));
}
// Parse status - handle both success and error cases // Parse status - handle both success and error cases
if let Some(status) = parsed_status { if let Some(status) = parsed_status {
let initialized = status["initialized"].as_bool().unwrap_or(false); let initialized = status["initialized"].as_bool().unwrap_or(false);