#![allow(
clippy::print_stdout,
clippy::print_stderr,
clippy::uninlined_format_args,
reason = "example code that demonstrates library usage"
)]
use std::time::Duration;
use waitup::{wait_for_connection, Target, WaitConfig};
#[tokio::main]
async fn main() -> Result<(), waitup::WaitForError> {
println!("βΈοΈ Kubernetes Init Container: Checking dependencies...");
let db_host = std::env::var("DATABASE_HOST").unwrap_or_else(|_| "postgres-service".to_string());
let db_port = std::env::var("DATABASE_PORT")
.unwrap_or_else(|_| "5432".to_string())
.parse::<u16>()
.unwrap_or(5432);
let cache_host = std::env::var("REDIS_HOST").unwrap_or_else(|_| "redis-service".to_string());
let cache_port = std::env::var("REDIS_PORT")
.unwrap_or_else(|_| "6379".to_string())
.parse::<u16>()
.unwrap_or(6379);
let api_url = std::env::var("EXTERNAL_API_URL")
.unwrap_or_else(|_| "https://api.external-service.com/health".to_string());
let targets = vec![
Target::tcp(&db_host, db_port)?,
Target::tcp(&cache_host, cache_port)?,
Target::parse(&api_url, 200)?,
];
let config = WaitConfig::builder()
.timeout(Duration::from_secs(300)) .interval(Duration::from_secs(5)) .max_interval(Duration::from_secs(30))
.max_retries(Some(60)) .wait_for_any(false) .build();
println!("π Checking dependencies:");
for target in &targets {
println!(" β³ {}", target.display());
}
match wait_for_connection(&targets, &config).await {
Ok(result) => {
println!("β
All dependencies are ready!");
println!(
"π Completed in {:?} with {} total attempts",
result.elapsed, result.attempts
);
for target_result in &result.target_results {
println!(
" β
{}: Ready in {:?} ({} attempts)",
target_result.target.display(),
target_result.elapsed,
target_result.attempts
);
}
println!("π― Init container completed successfully. Main container can now start.");
}
Err(e) => {
eprintln!("β Dependencies not ready: {e}");
eprintln!("π§ Check your service configurations and try again.");
std::process::exit(1);
}
}
Ok(())
}