use crate::configuration::Configuration;
use anyhow::Error;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::Relaxed;
use tokio::sync::watch::Receiver;
#[derive(Clone)]
pub struct AppState {
id: u32,
pub namespace: Option<String>,
pub etcd_client: detcd::client::Client,
pub mb_builder: kmailbox::Builder,
pub running: Arc<AtomicBool>,
pub quit_rx: Receiver<bool>,
}
impl AppState {
pub async fn build(
configuration: &Configuration,
quit_rx: Receiver<bool>,
) -> Result<Self, Error> {
Ok(Self {
id: 0,
namespace: None,
etcd_client: detcd::client::Builder::new()
.build(&configuration.etcd.endpoints)
.await
.map_err(|e| Error::new(e).context("connect etcd error"))?,
mb_builder: kmailbox::Builder::new(&configuration.kafka.servers),
running: Arc::new(AtomicBool::new(true)),
quit_rx,
})
}
pub fn get_running(&self) -> bool {
self.running.load(Relaxed)
}
pub fn set_running(&self, r: bool) -> &Self {
self.running.store(r, Relaxed);
self
}
pub fn set_id(&mut self, id: u32) -> &mut Self {
self.id = id;
self
}
pub fn set_namespace(&mut self, ns: Option<String>) -> &mut Self {
self.namespace = ns;
self
}
}