torrust-tracker
torrust-tracker copied to clipboard
Tracker Checker: handle Health Check timeouts
Parent issue: https://github.com/torrust/torrust-tracker/issues/677
You can run a Tracker Checker with:
TORRUST_CHECKER_CONFIG='{
"udp_trackers": [],
"http_trackers": [],
"health_checks": ["http://127.0.0.1:3030/sleep"]
}' cargo run --bin tracker_checker
If the server does not reply in 5 seconds you will see an error like this:
$ TORRUST_CHECKER_CONFIG='{
"udp_trackers": [],
"http_trackers": [],
"health_checks": ["http://127.0.0.1:3030/sleep"]
}' cargo run --bin tracker_checker
Finished dev [optimized + debuginfo] target(s) in 0.08s
Running `target/debug/tracker_checker`
Running checks for trackers ...
UDP trackers ...
HTTP trackers ...
Health checks ...
✗ - Health API at http://127.0.0.1:3030/sleep is failing: reqwest::Error { kind: Request, url: Url { scheme: "http", cannot_be_a_base: false, username: "", password: None, host: Some(Ipv4(127.0.0.1)), port: Some(3030), path: "/sleep", query: None, fragment: None }, source: TimedOut }
You can use this example app to simulate the server.
This is the function for the check:
async fn run_health_check(url: Url, console: &Console) -> Result<(), CheckError> {
let client = HttpClient::builder().timeout(Duration::from_secs(5)).build().unwrap();
let colored_url = url.to_string().yellow();
match client.get(url.clone()).send().await {
Ok(response) => {
if response.status().is_success() {
console.println(&format!("{} - Health API at {} is OK", "✓".green(), colored_url));
Ok(())
} else {
console.eprintln(&format!(
"{} - Health API at {} is failing: {:?}",
"✗".red(),
colored_url,
response
));
Err(CheckError::HealthCheckError { url })
}
}
Err(err) => {
console.eprintln(&format!(
"{} - Health API at {} is failing: {:?}",
"✗".red(),
colored_url,
err
));
Err(CheckError::HealthCheckError { url })
}
}
}
It contains this line:
let client = HttpClient::builder().timeout(Duration::from_secs(5)).build().unwrap();
We have to catch the error instead of unwrapping. Otherwise, the Tracker Checker is halted. The Tracker Checker should finish the whole report even if one service is not available.