churn-v2/crates/churn/src/agent/agent_state.rs
kjuulh 83294306a4
All checks were successful
continuous-integration/drone/push Build is passing
feat: enable having get variable from local setup
Signed-off-by: kjuulh <contact@kjuulh.io>
2025-01-04 01:28:32 +01:00

61 lines
1.5 KiB
Rust

use std::{ops::Deref, sync::Arc};
use crate::api::Discovery;
use super::{
config::AgentConfig, discovery_client::DiscoveryClient, grpc_client::GrpcClient,
handlers::scheduled_tasks::ScheduledTasks, plugins::PluginStore, queue::AgentQueue,
scheduler::Scheduler,
};
#[derive(Clone)]
pub struct AgentState(Arc<State>);
impl AgentState {
pub async fn new() -> anyhow::Result<Self> {
Ok(Self(Arc::new(State::new().await?)))
}
}
impl From<&AgentState> for AgentState {
fn from(value: &AgentState) -> Self {
value.clone()
}
}
impl Deref for AgentState {
type Target = Arc<State>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub struct State {
pub grpc: GrpcClient,
pub config: AgentConfig,
pub discovery: Discovery,
pub queue: AgentQueue,
pub plugin_store: PluginStore,
}
impl State {
pub async fn new() -> anyhow::Result<Self> {
let config = AgentConfig::new().await?;
let discovery = DiscoveryClient::new(&config.discovery).discover().await?;
let grpc = GrpcClient::new(&discovery.process_host);
let plugin_store = PluginStore::new(config.clone())?;
let scheduled_tasks = ScheduledTasks::new(plugin_store.clone());
let scheduler = Scheduler::new(scheduled_tasks);
let queue = AgentQueue::new(scheduler);
Ok(Self {
grpc,
config,
discovery,
queue,
plugin_store,
})
}
}