#![allow(
clippy::print_stdout,
clippy::uninlined_format_args,
reason = "example code that demonstrates library usage"
)]
use std::time::Duration;
use waitup::{wait_for_connection, Target, WaitConfig};
async fn wait_for_core_services() -> Result<(), waitup::WaitForError> {
println!("🏗️ Phase 1: Waiting for core infrastructure services...");
let core_services = vec![
Target::tcp("postgres", 5432)?, Target::tcp("redis", 6379)?, Target::tcp("elasticsearch", 9200)?, ];
let config = WaitConfig::builder()
.timeout(Duration::from_secs(180)) .interval(Duration::from_secs(2))
.wait_for_any(false) .build();
let result = wait_for_connection(&core_services, &config).await?;
println!("✅ Core services ready in {:?}", result.elapsed);
Ok(())
}
async fn wait_for_application_services() -> Result<(), waitup::WaitForError> {
println!("🚀 Phase 2: Waiting for application services...");
let app_services = vec![
Target::parse("http://auth-service:8001/health", 200)?,
Target::parse("http://user-service:8002/health", 200)?,
Target::parse("http://notification-service:8003/health", 200)?,
Target::parse("http://payment-service:8004/health", 200)?,
];
let config = WaitConfig::builder()
.timeout(Duration::from_secs(120)) .interval(Duration::from_secs(3))
.wait_for_any(false)
.build();
let result = wait_for_connection(&app_services, &config).await?;
println!("✅ Application services ready in {:?}", result.elapsed);
Ok(())
}
async fn wait_for_external_dependencies() -> Result<(), waitup::WaitForError> {
println!("🌐 Phase 3: Checking external dependencies (any one available)...");
let external_apis = vec![
Target::parse("https://api.stripe.com/v1", 401)?, Target::parse("https://api.sendgrid.com/v3", 401)?, Target::parse("https://hooks.slack.com/services", 404)?, ];
let config = WaitConfig::builder()
.timeout(Duration::from_secs(60)) .interval(Duration::from_secs(5))
.wait_for_any(true) .build();
let result = wait_for_connection(&external_apis, &config).await?;
println!("✅ External dependencies available in {:?}", result.elapsed);
Ok(())
}
async fn start_load_balancer() -> Result<(), waitup::WaitForError> {
println!("⚖️ Phase 4: Starting load balancer...");
let lb_target = vec![Target::parse("http://load-balancer:80/health", 200)?];
let config = WaitConfig::builder()
.timeout(Duration::from_secs(30))
.interval(Duration::from_secs(2))
.build();
let result = wait_for_connection(&lb_target, &config).await?;
println!("✅ Load balancer ready in {:?}", result.elapsed);
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), waitup::WaitForError> {
println!("🎭 Complex Multi-Service Orchestration Example");
println!("============================================");
let start_time = std::time::Instant::now();
wait_for_core_services().await?;
wait_for_application_services().await?;
wait_for_external_dependencies().await?;
start_load_balancer().await?;
let total_time = start_time.elapsed();
println!();
println!("🎉 All services are orchestrated and ready!");
println!("⏱️ Total orchestration time: {:?}", total_time);
println!("🌟 Your distributed system is now fully operational!");
Ok(())
}