forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: [torrust#639] Tracker Checker: extract mod for Health checks
- Loading branch information
1 parent
70924ed
commit 873f98d
Showing
2 changed files
with
53 additions
and
48 deletions.
There are no files selected for viewing
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,51 @@ | ||
use std::time::Duration; | ||
|
||
use colored::Colorize; | ||
use reqwest::{Client as HttpClient, Url, Url as ServiceUrl}; | ||
|
||
use crate::console::clients::checker::console::Console; | ||
use crate::console::clients::checker::printer::Printer; | ||
use crate::console::clients::checker::service::{CheckError, CheckResult}; | ||
|
||
pub async fn run(health_checks: &Vec<ServiceUrl>, console: &Console, check_results: &mut Vec<CheckResult>) { | ||
console.println("Health checks ..."); | ||
|
||
for health_check_url in health_checks { | ||
match run_health_check(health_check_url.clone(), console).await { | ||
Ok(()) => check_results.push(Ok(())), | ||
Err(err) => check_results.push(Err(err)), | ||
} | ||
} | ||
} | ||
|
||
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 }) | ||
} | ||
} | ||
} |
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