Files
spacedrive/core/src/infra/daemon/instance.rs
T

67 lines
1.9 KiB
Rust

use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::RwLock;
use crate::Core;
use crate::infra::daemon::state::SessionStateService;
/// Manages lifecycle of Core instances (by name)
pub struct CoreInstanceManager {
instances: Arc<RwLock<HashMap<String, Arc<Core>>>>,
default_data_dir: PathBuf,
enable_networking: bool,
session_state: Arc<SessionStateService>,
}
impl CoreInstanceManager {
pub fn new(default_data_dir: PathBuf, enable_networking: bool, session_state: Arc<SessionStateService>) -> Self {
Self {
instances: Arc::new(RwLock::new(HashMap::new())),
default_data_dir,
enable_networking,
session_state,
}
}
/// Get or start the default instance
pub async fn get_default(&self) -> Result<Arc<Core>, Box<dyn std::error::Error>> {
self.get_or_start("default".to_string(), None).await
}
/// Get or start a named instance, optionally with a specific data_dir
pub async fn get_or_start(
&self,
name: String,
data_dir: Option<PathBuf>,
) -> Result<Arc<Core>, Box<dyn std::error::Error>> {
if let Some(existing) = self.instances.read().await.get(&name).cloned() {
return Ok(existing);
}
let data_dir = data_dir.unwrap_or_else(|| self.default_data_dir.clone());
let core = Arc::new(Core::new_with_config(data_dir, self.session_state.clone()).await?);
if self.enable_networking {
let core_with_networking = Core::init_networking_shared(core.clone(), self.session_state.clone()).await?;
self.instances
.write()
.await
.insert(name, core_with_networking.clone());
Ok(core_with_networking)
} else {
self.instances.write().await.insert(name, core.clone());
Ok(core)
}
}
/// Shutdown a named instance
pub async fn shutdown(&self, name: &str) -> Result<(), Box<dyn std::error::Error>> {
if let Some(core) = self.instances.write().await.remove(name) {
core.shutdown().await?;
}
Ok(())
}
}