summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorToby Vincent <tobyv@tobyvin.dev>2024-09-28 01:08:16 -0500
committerToby Vincent <tobyv@tobyvin.dev>2024-09-28 01:08:16 -0500
commite1d9c956beb6921b0d549248bea3a6853fde5f46 (patch)
treec01098af5a024b00db50b0326dc67f79d7825ef9 /src
parentcd774827dd14f68d8405c45d2d9da30b3fab050e (diff)
fix: clean up unused code and fix TCP client
Diffstat (limited to 'src')
-rw-r--r--src/service/http.rs42
-rw-r--r--src/service/systemd.rs27
-rw-r--r--src/service/tcp.rs18
3 files changed, 3 insertions, 84 deletions
diff --git a/src/service/http.rs b/src/service/http.rs
index 6d21cb7..7c875b9 100644
--- a/src/service/http.rs
+++ b/src/service/http.rs
@@ -1,10 +1,8 @@
use std::{fmt::Display, time::Duration};
use axum::http::status::StatusCode;
-use futures::Stream;
use serde::Deserialize;
use tokio::sync::watch::Sender;
-use tokio_stream::wrappers::WatchStream;
use url::Url;
use crate::{Error, Status};
@@ -22,12 +20,6 @@ pub struct Http {
pub client: Option<reqwest::Client>,
}
-impl Display for Http {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "{} {}", self.method, self.url)
- }
-}
-
impl ServiceSpawner for Http {
async fn spawn(self, tx: Sender<Status>) -> Result<(), Error> {
let client = self.client.unwrap_or_default();
@@ -54,40 +46,6 @@ impl ServiceSpawner for Http {
}
}
-impl Http {
- pub fn into_stream(self, client: reqwest::Client) -> impl Stream<Item = Status> {
- let request = client
- .request(self.method.into(), self.url)
- .build()
- .expect("Url parsing should not fail");
-
- let (tx, rx) = tokio::sync::watch::channel(Status::default());
-
- tokio::spawn(async move {
- let mut interval = tokio::time::interval(Duration::from_secs(5));
- loop {
- interval.tick().await;
- let req = request
- .try_clone()
- .expect("Clone with no body should never fail");
- let resp = client.execute(req).await;
- let status = match resp.map(|r| r.status().as_u16()) {
- Ok(code) if code == self.status_code => Status::Pass,
- Ok(code) => Status::Fail(Some(format!("Status code: {code}"))),
- Err(err) => {
- tracing::error!("HTTP request error: {err}");
- Status::Unknown
- }
- };
-
- tx.send_if_modified(|s| s.update(status));
- }
- });
-
- WatchStream::new(rx)
- }
-}
-
#[derive(Debug, Clone, Copy, Default, Deserialize)]
pub enum Method {
#[serde(alias = "get", alias = "GET")]
diff --git a/src/service/systemd.rs b/src/service/systemd.rs
index e3b4d1b..90213a0 100644
--- a/src/service/systemd.rs
+++ b/src/service/systemd.rs
@@ -1,4 +1,4 @@
-use std::{fmt::Display, process::Command, time::Duration};
+use std::{process::Command, time::Duration};
use serde::Deserialize;
use tokio::sync::watch::Sender;
@@ -12,12 +12,6 @@ pub struct Systemd {
pub service: String,
}
-impl Display for Systemd {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "{}.service", self.service.trim_end_matches(".service"))
- }
-}
-
impl ServiceSpawner for Systemd {
async fn spawn(self, tx: Sender<Status>) -> Result<(), Error> {
let mut command = Command::new("systemctl");
@@ -43,22 +37,3 @@ impl ServiceSpawner for Systemd {
}
}
}
-
-impl Systemd {
- pub async fn check(&self) -> Result<Status, Error> {
- let output = Command::new("systemctl")
- .arg("is-active")
- .arg(&self.service)
- .output()?;
-
- let status = match output.status.success() {
- true => Status::Pass,
- false => {
- let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
- Status::Fail(Some(format!("Service state: {}", stdout)))
- }
- };
-
- Ok(status)
- }
-}
diff --git a/src/service/tcp.rs b/src/service/tcp.rs
index 5ec5f36..42791bc 100644
--- a/src/service/tcp.rs
+++ b/src/service/tcp.rs
@@ -1,9 +1,7 @@
use std::{fmt::Display, net::SocketAddr, time::Duration};
-use futures::Stream;
use serde::Deserialize;
use tokio::{io::Interest, net::TcpSocket, sync::watch::Sender};
-use tokio_stream::wrappers::WatchStream;
use crate::{Error, Status};
@@ -21,7 +19,6 @@ impl Display for Tcp {
}
impl ServiceSpawner for Tcp {
- #[tracing::instrument(skip(tx))]
async fn spawn(self, tx: Sender<Status>) -> Result<(), Error> {
let mut interval = tokio::time::interval(Duration::from_secs(5));
@@ -33,14 +30,11 @@ impl ServiceSpawner for Tcp {
match sock.connect(self.address).await {
Ok(conn) => {
- tracing::info!("Connected");
+ // TODO: figure out how to wait for connection to close
+ conn.ready(Interest::READABLE).await?;
tx.send_if_modified(|s| s.update(Status::Pass));
- conn.ready(Interest::ERROR).await?;
- tx.send_replace(Status::Fail(Some("Disconnected".into())));
- tracing::info!("Disconnected");
}
Err(err) => {
- tracing::error!("Failed to connect");
tx.send_if_modified(|s| s.update(err.into()));
}
};
@@ -48,14 +42,6 @@ impl ServiceSpawner for Tcp {
}
}
-impl Tcp {
- pub fn into_stream(self) -> impl Stream<Item = Status> {
- let (tx, rx) = tokio::sync::watch::channel(Status::default());
- tokio::spawn(self.spawn(tx));
- WatchStream::new(rx)
- }
-}
-
#[cfg(test)]
mod tests {
use super::*;