forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
774 additions
and
550 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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 |
---|---|---|
@@ -1,49 +1,77 @@ | ||
use std::sync::Arc; | ||
use std::time::Duration; | ||
|
||
use reqwest::{Client as HttpClient, Url, Url as ServiceUrl}; | ||
use anyhow::Result; | ||
use hyper::StatusCode; | ||
use reqwest::{Client as HttpClient, Response}; | ||
use serde::Serialize; | ||
use thiserror::Error; | ||
use url::Url; | ||
|
||
use super::structs::{CheckerOutput, Status}; | ||
use crate::console::clients::checker::service::{CheckError, CheckResult}; | ||
#[derive(Debug, Clone, Error, Serialize)] | ||
#[serde(into = "String")] | ||
pub enum Error { | ||
#[error("Failed to Build a Http Client: {err:?}")] | ||
ClientBuildingError { err: Arc<reqwest::Error> }, | ||
#[error("Heath check failed to get a response: {err:?}")] | ||
ResponseError { err: Arc<reqwest::Error> }, | ||
#[error("Http check returned a non-success code: \"{code}\" with the response: \"{response:?}\"")] | ||
UnsuccessfulResponse { code: StatusCode, response: Arc<Response> }, | ||
} | ||
|
||
impl From<Error> for String { | ||
fn from(value: Error) -> Self { | ||
value.to_string() | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize)] | ||
pub struct Checks { | ||
url: Url, | ||
result: Result<String, Error>, | ||
} | ||
|
||
#[allow(clippy::missing_panics_doc)] | ||
pub async fn run(health_checks: &Vec<ServiceUrl>, check_results: &mut Vec<CheckResult>) -> Vec<CheckerOutput> { | ||
let mut health_checkers: Vec<CheckerOutput> = Vec::new(); | ||
pub async fn run(health_checks: Vec<Url>, timeout: Duration) -> Vec<Result<Checks, Checks>> { | ||
let mut results = Vec::default(); | ||
|
||
for health_check_url in health_checks { | ||
let mut health_checker = CheckerOutput { | ||
url: health_check_url.to_string(), | ||
status: Status { | ||
code: String::new(), | ||
message: String::new(), | ||
}, | ||
tracing::debug!("Health checks ..."); | ||
|
||
for url in health_checks { | ||
let result = match run_health_check(url.clone(), timeout).await { | ||
Ok(response) => Ok(response.status().to_string()), | ||
Err(err) => Err(err), | ||
}; | ||
match run_health_check(health_check_url.clone()).await { | ||
Ok(()) => { | ||
check_results.push(Ok(())); | ||
health_checker.status.code = "ok".to_string(); | ||
} | ||
Err(err) => { | ||
check_results.push(Err(err)); | ||
health_checker.status.code = "error".to_string(); | ||
health_checker.status.message = "Health API is failing.".to_string(); | ||
} | ||
|
||
let check = Checks { url, result }; | ||
|
||
if check.result.is_err() { | ||
results.push(Err(check)); | ||
} else { | ||
results.push(Ok(check)); | ||
} | ||
health_checkers.push(health_checker); | ||
} | ||
health_checkers | ||
|
||
results | ||
} | ||
|
||
async fn run_health_check(url: Url) -> Result<(), CheckError> { | ||
let client = HttpClient::builder().timeout(Duration::from_secs(5)).build().unwrap(); | ||
async fn run_health_check(url: Url, timeout: Duration) -> Result<Response, Error> { | ||
let client = HttpClient::builder() | ||
.timeout(timeout) | ||
.build() | ||
.map_err(|e| Error::ClientBuildingError { err: e.into() })?; | ||
|
||
match client.get(url.clone()).send().await { | ||
Ok(response) => { | ||
if response.status().is_success() { | ||
Ok(()) | ||
} else { | ||
Err(CheckError::HealthCheckError { url }) | ||
} | ||
} | ||
Err(_) => Err(CheckError::HealthCheckError { url }), | ||
let response = client | ||
.get(url.clone()) | ||
.send() | ||
.await | ||
.map_err(|e| Error::ResponseError { err: e.into() })?; | ||
|
||
if response.status().is_success() { | ||
Ok(response) | ||
} else { | ||
Err(Error::UnsuccessfulResponse { | ||
code: response.status(), | ||
response: response.into(), | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.