use tokio::sync::{broadcast, mpsc};
use tokio::time::{interval, Duration};
use tracing::debug;
mod actor;
mod commands;
mod handle;
pub use actor::{RegistryActor, MAX_SESSIONS};
pub use commands::{RegistryCommand, RegistryError, RemovalReason, SessionEvent};
pub use handle::RegistryHandle;
const COMMAND_BUFFER: usize = 100;
const EVENT_BUFFER: usize = 100;
const CLEANUP_INTERVAL_SECS: u64 = 2;
const GIT_REFRESH_INTERVAL_SECS: u64 = 5;
pub fn spawn_registry() -> RegistryHandle {
let (cmd_tx, cmd_rx) = mpsc::channel(COMMAND_BUFFER);
let (event_tx, _) = broadcast::channel(EVENT_BUFFER);
let actor = RegistryActor::new(cmd_rx, event_tx.clone());
tokio::spawn(actor.run());
let handle = RegistryHandle::new(cmd_tx.clone(), event_tx);
spawn_cleanup_task(cmd_tx.clone());
spawn_git_refresh_task(cmd_tx);
handle
}
fn spawn_cleanup_task(sender: mpsc::Sender<RegistryCommand>) {
tokio::spawn(async move {
let mut ticker = interval(Duration::from_secs(CLEANUP_INTERVAL_SECS));
loop {
ticker.tick().await;
if sender.send(RegistryCommand::CleanupStale).await.is_err() {
debug!("Cleanup task stopping: registry channel closed");
break;
}
debug!("Triggered stale session cleanup");
}
});
}
fn spawn_git_refresh_task(sender: mpsc::Sender<RegistryCommand>) {
tokio::spawn(async move {
let mut ticker = interval(Duration::from_secs(GIT_REFRESH_INTERVAL_SECS));
loop {
ticker.tick().await;
if sender.send(RegistryCommand::RefreshGitInfo).await.is_err() {
debug!("Git refresh task stopping: registry channel closed");
break;
}
}
});
}