Skip to content

Commit 873f98d

Browse files
committed
refactor: [#639] Tracker Checker: extract mod for Health checks
1 parent 70924ed commit 873f98d

File tree

2 files changed

+53
-48
lines changed

2 files changed

+53
-48
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use std::time::Duration;
2+
3+
use colored::Colorize;
4+
use reqwest::{Client as HttpClient, Url, Url as ServiceUrl};
5+
6+
use crate::console::clients::checker::console::Console;
7+
use crate::console::clients::checker::printer::Printer;
8+
use crate::console::clients::checker::service::{CheckError, CheckResult};
9+
10+
pub async fn run(health_checks: &Vec<ServiceUrl>, console: &Console, check_results: &mut Vec<CheckResult>) {
11+
console.println("Health checks ...");
12+
13+
for health_check_url in health_checks {
14+
match run_health_check(health_check_url.clone(), console).await {
15+
Ok(()) => check_results.push(Ok(())),
16+
Err(err) => check_results.push(Err(err)),
17+
}
18+
}
19+
}
20+
21+
async fn run_health_check(url: Url, console: &Console) -> Result<(), CheckError> {
22+
let client = HttpClient::builder().timeout(Duration::from_secs(5)).build().unwrap();
23+
24+
let colored_url = url.to_string().yellow();
25+
26+
match client.get(url.clone()).send().await {
27+
Ok(response) => {
28+
if response.status().is_success() {
29+
console.println(&format!("{} - Health API at {} is OK", "✓".green(), colored_url));
30+
Ok(())
31+
} else {
32+
console.eprintln(&format!(
33+
"{} - Health API at {} is failing: {:?}",
34+
"✗".red(),
35+
colored_url,
36+
response
37+
));
38+
Err(CheckError::HealthCheckError { url })
39+
}
40+
}
41+
Err(err) => {
42+
console.eprintln(&format!(
43+
"{} - Health API at {} is failing: {:?}",
44+
"✗".red(),
45+
colored_url,
46+
err
47+
));
48+
Err(CheckError::HealthCheckError { url })
49+
}
50+
}
51+
}
+2-48
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use std::net::SocketAddr;
22
use std::sync::Arc;
3-
use std::time::Duration;
43

5-
use colored::Colorize;
6-
use reqwest::{Client as HttpClient, Url};
4+
use reqwest::Url;
75

86
use super::checks;
97
use super::config::Configuration;
@@ -37,52 +35,8 @@ impl Service {
3735

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

40-
self.run_health_checks(&mut check_results).await;
38+
checks::health::run(&self.config.health_checks, &self.console, &mut check_results).await;
4139

4240
check_results
4341
}
44-
45-
async fn run_health_checks(&self, check_results: &mut Vec<CheckResult>) {
46-
self.console.println("Health checks ...");
47-
48-
for health_check_url in &self.config.health_checks {
49-
match self.run_health_check(health_check_url.clone()).await {
50-
Ok(()) => check_results.push(Ok(())),
51-
Err(err) => check_results.push(Err(err)),
52-
}
53-
}
54-
}
55-
56-
async fn run_health_check(&self, url: Url) -> Result<(), CheckError> {
57-
let client = HttpClient::builder().timeout(Duration::from_secs(5)).build().unwrap();
58-
59-
let colored_url = url.to_string().yellow();
60-
61-
match client.get(url.clone()).send().await {
62-
Ok(response) => {
63-
if response.status().is_success() {
64-
self.console
65-
.println(&format!("{} - Health API at {} is OK", "✓".green(), colored_url));
66-
Ok(())
67-
} else {
68-
self.console.eprintln(&format!(
69-
"{} - Health API at {} is failing: {:?}",
70-
"✗".red(),
71-
colored_url,
72-
response
73-
));
74-
Err(CheckError::HealthCheckError { url })
75-
}
76-
}
77-
Err(err) => {
78-
self.console.eprintln(&format!(
79-
"{} - Health API at {} is failing: {:?}",
80-
"✗".red(),
81-
colored_url,
82-
err
83-
));
84-
Err(CheckError::HealthCheckError { url })
85-
}
86-
}
87-
}
8842
}

0 commit comments

Comments
 (0)