use tokio::process::Command as AsyncCommand;
async fn discover_sandbox_containers() -> Vec<String> {
let output = AsyncCommand::new("container")
.args(["list", "--all", "--format", "json"])
.output()
.await
.ok()
.filter(|o| o.status.success());
match output {
Some(out) => serde_json::from_slice::<Vec<serde_json::Value>>(&out.stdout)
.ok()
.map(|items| {
items
.iter()
.filter_map(|item| {
let id = item.get("id").and_then(|v| v.as_str()).or_else(|| {
item.get("configuration")
.and_then(|v| v.get("id"))
.and_then(|v| v.as_str())
})?;
let labels = item.get("configuration").and_then(|v| v.get("labels"));
let managed = labels
.and_then(|v| v.get("muthr.managed"))
.and_then(|v| v.as_str())
.is_some_and(|v| v == "true");
let owner_project = labels
.and_then(|v| v.get("muthr.owner"))
.and_then(|v| v.as_str())
.is_some_and(|v| v == "project");
if id.starts_with("muthr-")
&& id != "muthr-services"
&& id != "muthr-searxng"
&& managed
&& owner_project
{
Some(id.to_string())
} else {
None
}
})
.collect()
})
.unwrap_or_default(),
None => Vec::new(),
}
}
async fn stop_container(name: String, timeout_secs: u64, verbose: bool) {
if verbose {
eprintln!("info: stopping container {}", name);
}
let status = AsyncCommand::new("container")
.args(["stop", "--time", &timeout_secs.to_string(), &name])
.output()
.await;
match status {
Ok(out) if out.status.success() => eprintln!("info: stopped {}", name),
Ok(_) | Err(_) => eprintln!("warning: failed to stop {}", name),
}
}
async fn stop_engine(verbose: bool) {
let mut had_any = false;
if crate::engine::is_running().await {
had_any = true;
if let Err(err) = crate::engine::stop().await {
eprintln!("warning: failed to stop inference engine: {}", err);
}
}
if had_any {
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
if crate::engine::is_running().await {
eprintln!("warning: inference engine still running after stop request; retrying");
if let Err(err) = crate::engine::stop().await {
eprintln!("warning: second stop attempt failed: {}", err);
}
}
}
if verbose && had_any && !crate::engine::is_running().await {
eprintln!("info: inference engine stopped");
}
}
pub async fn run(
verbose: bool,
timeout_secs: Option<u64>,
_yes: bool,
dry_run: bool,
) -> Result<(), color_eyre::Report> {
if dry_run {
eprintln!("info: dry run, skipping shutdown actions");
return Ok(());
}
let _lock =
crate::lifecycle::acquire("container-lifecycle", std::time::Duration::from_secs(20))
.await?;
let timeout = timeout_secs.unwrap_or(30);
if verbose {
eprintln!("info: scanning containers");
}
let sandboxes = discover_sandbox_containers().await;
for container in sandboxes {
stop_container(container.clone(), timeout, verbose).await;
}
stop_container("muthr-services".to_string(), timeout, verbose).await;
stop_container("muthr-searxng".to_string(), timeout, verbose).await;
if verbose {
eprintln!("info: stopping inference engine");
}
stop_engine(verbose).await;
if verbose {
eprintln!("info: shutdown complete");
}
Ok(())
}