Skip to content

Commit

Permalink
refactor: [torrust#639] Tracker Checker: extract mod for Health checks
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Feb 1, 2024
1 parent 70924ed commit 873f98d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 48 deletions.
51 changes: 51 additions & 0 deletions src/console/clients/checker/checks/health.rs
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 })
}
}
}
50 changes: 2 additions & 48 deletions src/console/clients/checker/service.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;

use colored::Colorize;
use reqwest::{Client as HttpClient, Url};
use reqwest::Url;

use super::checks;
use super::config::Configuration;
Expand Down Expand Up @@ -37,52 +35,8 @@ impl Service {

checks::http::run(&self.config.http_trackers, &self.console, &mut check_results).await;

self.run_health_checks(&mut check_results).await;
checks::health::run(&self.config.health_checks, &self.console, &mut check_results).await;

check_results
}

async fn run_health_checks(&self, check_results: &mut Vec<CheckResult>) {
self.console.println("Health checks ...");

for health_check_url in &self.config.health_checks {
match self.run_health_check(health_check_url.clone()).await {
Ok(()) => check_results.push(Ok(())),
Err(err) => check_results.push(Err(err)),
}
}
}

async fn run_health_check(&self, url: Url) -> 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() {
self.console
.println(&format!("{} - Health API at {} is OK", "✓".green(), colored_url));
Ok(())
} else {
self.console.eprintln(&format!(
"{} - Health API at {} is failing: {:?}",
"✗".red(),
colored_url,
response
));
Err(CheckError::HealthCheckError { url })
}
}
Err(err) => {
self.console.eprintln(&format!(
"{} - Health API at {} is failing: {:?}",
"✗".red(),
colored_url,
err
));
Err(CheckError::HealthCheckError { url })
}
}
}
}

0 comments on commit 873f98d

Please sign in to comment.