feat: health checker
This commit is contained in:
parent
0a35a6425c
commit
f305cb5a85
1 changed files with 60 additions and 0 deletions
60
src/health.rs
Normal file
60
src/health.rs
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
use http_body_util::Empty;
|
||||||
|
use hyper::{Request, body::Bytes};
|
||||||
|
use hyper_util::rt::TokioIo;
|
||||||
|
use log::error;
|
||||||
|
use tokio::net::TcpStream;
|
||||||
|
|
||||||
|
use crate::db::{BoxyDatabase, Endpoint};
|
||||||
|
|
||||||
|
pub async fn check(db: &mut BoxyDatabase) {
|
||||||
|
let endpoints = Endpoint::get_all(db).await.unwrap();
|
||||||
|
|
||||||
|
for endpoint in endpoints {
|
||||||
|
let address = format!("{}:{}", endpoint.address, endpoint.port);
|
||||||
|
|
||||||
|
let url = format!("http://{}{}", address, endpoint.callback)
|
||||||
|
.parse::<hyper::Uri>()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let stream = match TcpStream::connect(address).await {
|
||||||
|
Ok(x) => x,
|
||||||
|
Err(e) => {
|
||||||
|
error!("Could not reach endpoint {}: {e}", endpoint.id.unwrap());
|
||||||
|
|
||||||
|
endpoint.delete(db).await.unwrap();
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let io = TokioIo::new(stream);
|
||||||
|
|
||||||
|
let (mut sender, conn) = hyper::client::conn::http1::handshake(io).await.unwrap();
|
||||||
|
|
||||||
|
tokio::task::spawn(async move {
|
||||||
|
if let Err(err) = conn.await {
|
||||||
|
println!("Connection failed: {:?}", err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let req = Request::builder()
|
||||||
|
.uri(url)
|
||||||
|
.body(Empty::<Bytes>::new())
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let res = match sender.send_request(req).await {
|
||||||
|
Ok(x) => x,
|
||||||
|
Err(e) => {
|
||||||
|
error!("Could not reach endpoint {}: {e}", endpoint.id.unwrap());
|
||||||
|
|
||||||
|
endpoint.delete(db).await.unwrap();
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if !res.status().is_success() {
|
||||||
|
endpoint.delete(db).await.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue