use std::collections::HashMap;
use std::path::PathBuf;
use crate::lock_file::{self, LockClaim};
pub struct AgentInstanceRegistry {
root: PathBuf,
open: HashMap<String, LockClaim>,
}
impl AgentInstanceRegistry {
pub fn new(root: PathBuf) -> std::io::Result<Self> {
std::fs::create_dir_all(&root)?;
Ok(Self {
root,
open: HashMap::new(),
})
}
pub fn path_for(&self, hier: &str) -> PathBuf {
self.root.join(hier.replace('/', "_"))
}
pub fn observe(&mut self, hier: &str) {
if self.open.contains_key(hier) {
return;
}
let path = self.path_for(hier);
if let Some(claim) = lock_file::try_acquire(&path) {
self.open.insert(hier.to_string(), claim);
}
}
pub fn destroy(&mut self, hier: &str) {
self.open.remove(hier);
}
}