agentmux_srv/server/
old_versions_handlers.rs1use super::*;
10use crate::backend::old_versions as ov;
11
12#[derive(Debug, Default, serde::Deserialize)]
13pub(super) struct RemoveReq {
14 #[serde(default)]
15 only: Vec<String>,
16}
17
18async fn respond(state: AppState, dry_run: bool, only: Option<Vec<String>>) -> Json<serde_json::Value> {
19 let Some(layout) = ov::layout_from_env() else {
20 return Json(json!({ "error": "this AgentMux has no per-version folders (a dev build or a test home)" }));
21 };
22 let policy = crate::backend::old_versions_service::policy(&state.config_watcher, crate::server::external_host::host().is_some());
23 let shown = layout.clone();
24 let run = tokio::task::spawn_blocking(move || ov::run(&layout, &policy, dry_run, only.as_deref(), std::time::SystemTime::now())).await;
25 match run {
26 Ok(run) => Json(json!({
27 "dry_run": dry_run,
28 "running": shown.running,
29 "channel": shown.channel,
30 "versions_root": shown.versions_root,
31 "scan_ok": run.scan_ok,
32 "reclaimable_bytes": run.plan.reclaimable_bytes(),
33 "plan": run.plan,
34 "report": run.report,
35 })),
36 Err(e) => Json(json!({ "error": format!("old versions: {e}") })),
37 }
38}
39
40pub(super) async fn handle_get(State(state): State<AppState>) -> Json<serde_json::Value> {
41 respond(state, true, None).await
42}
43
44pub(super) async fn handle_post(State(state): State<AppState>, Json(req): Json<RemoveReq>) -> Json<serde_json::Value> {
45 if req.only.is_empty() {
46 return Json(json!({ "error": "name the folders to remove in \"only\" (GET shows the plan first)" }));
47 }
48 respond(state, false, Some(req.only)).await
49}