From 8b9eb6eb88d871309348dff1527d69b4b32a98ec Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Sat, 12 Oct 2024 18:23:46 -0500 Subject: refactor: simplify service trait, again --- src/state.rs | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) (limited to 'src/state.rs') diff --git a/src/state.rs b/src/state.rs index c442d9d..857b8af 100644 --- a/src/state.rs +++ b/src/state.rs @@ -5,7 +5,7 @@ use tokio::sync::watch::Receiver; use tokio_stream::wrappers::WatchStream; use crate::{ - service::{IntoService, ServiceConfig, ServiceKind}, + service::{IntoService, ServiceConfig}, Status, }; @@ -22,30 +22,20 @@ impl AppState { .into_iter() .map(|ServiceConfig { name, kind }| { indexes.push(name.clone()); + tracing::debug!(name, "Added service"); let (tx, rx) = tokio::sync::watch::channel(Status::default()); - tokio::spawn(Self::spawn_service(kind, tx.clone())); + tokio::spawn(kind.into_service(tx.clone())); (name, rx) }) .collect(); + tracing::debug!(?indexes, "Finished spawning services"); + AppState { rx_map, indexes } } - #[tracing::instrument(skip(tx))] - async fn spawn_service(kind: ServiceKind, tx: tokio::sync::watch::Sender) { - let mut stream = kind.into_service(); - while let Some(res) = stream.next().await { - let status = res.into(); - tx.send_if_modified(|s| { - if *s != status { - tracing::debug!(?status, "Updated service status"); - *s = status; - true - } else { - false - } - }); - } + pub fn names(&self) -> Vec { + self.indexes.clone() } pub fn status(&self, k: &str) -> Option { -- cgit v1.2.3-70-g09d2