use std::{path::PathBuf, sync::Arc};
use tokio::sync::RwLock;
use crate::orchestration::Orchestrator;
#[derive(Clone)]
pub struct ServerState {
orchestrator: Arc<RwLock<Orchestrator>>,
}
impl ServerState {
pub async fn new(home_dir: PathBuf, supervisor_path: PathBuf) -> crate::MonocoreResult<Self> {
let orchestrator = match Orchestrator::load(&home_dir, &supervisor_path).await {
Ok(orchestrator) => {
tracing::info!("Loaded existing orchestrator state");
orchestrator
}
Err(e) => {
tracing::info!("Creating new orchestrator: {}", e);
Orchestrator::new(&home_dir, &supervisor_path).await?
}
};
Ok(Self {
orchestrator: Arc::new(RwLock::new(orchestrator)),
})
}
pub fn orchestrator(&self) -> &Arc<RwLock<Orchestrator>> {
&self.orchestrator
}
}