diff --git a/hive-ag3nt/src/events.rs b/hive-ag3nt/src/events.rs index 33efeb5..47ba13e 100644 --- a/hive-ag3nt/src/events.rs +++ b/hive-ag3nt/src/events.rs @@ -114,7 +114,6 @@ impl EventStore { out.reverse(); Ok(out) } - } #[derive(Clone)] @@ -172,7 +171,6 @@ impl Bus { }; store.recent(HISTORY_CAPACITY).unwrap_or_default() } - } impl Default for Bus { diff --git a/hive-c0re/src/dashboard.rs b/hive-c0re/src/dashboard.rs index 2a540e3..44b8715 100644 --- a/hive-c0re/src/dashboard.rs +++ b/hive-c0re/src/dashboard.rs @@ -456,15 +456,51 @@ async fn post_rebuild(State(state): State, AxumPath(name): AxumPath( + state: &AppState, + name: &str, + kind: crate::coordinator::TransientKind, + verb: &str, + body: F, + extra: impl FnOnce(&AppState, &str), +) -> Response +where + F: FnOnce(String) -> Fut, + Fut: std::future::Future>, +{ + let logical = strip_container_prefix(name); + state.coord.set_transient(&logical, kind); + let result = body(logical.clone()).await; state.coord.clear_transient(&logical); match result { - Ok(()) => Redirect::to("/").into_response(), - Err(e) => error_response(&format!("rebuild {logical} failed: {e:#}")), + Ok(()) => { + extra(state, &logical); + Redirect::to("/").into_response() + } + Err(e) => error_response(&format!("{verb} {logical} failed: {e:#}")), } } @@ -473,49 +509,44 @@ async fn post_kill(State(state): State, AxumPath(name): AxumPath { - state.coord.unregister_agent(&logical); - state - .coord - .notify_manager(&hive_sh4re::HelperEvent::Killed { - agent: logical.clone(), - }); - Redirect::to("/").into_response() - } - Err(e) => error_response(&format!("kill {logical} failed: {e:#}")), - } + lifecycle_action( + &state, + &name, + crate::coordinator::TransientKind::Stopping, + "kill", + |n| async move { lifecycle::kill(&n).await }, + |s, n| { + s.coord.unregister_agent(n); + s.coord.notify_manager(&hive_sh4re::HelperEvent::Killed { + agent: n.to_owned(), + }); + }, + ) + .await } async fn post_restart(State(state): State, AxumPath(name): AxumPath) -> Response { - let logical = strip_container_prefix(&name); - state - .coord - .set_transient(&logical, crate::coordinator::TransientKind::Restarting); - let result = lifecycle::restart(&logical).await; - state.coord.clear_transient(&logical); - match result { - Ok(()) => Redirect::to("/").into_response(), - Err(e) => error_response(&format!("restart {logical} failed: {e:#}")), - } + lifecycle_action( + &state, + &name, + crate::coordinator::TransientKind::Restarting, + "restart", + |n| async move { lifecycle::restart(&n).await }, + |_, _| {}, + ) + .await } async fn post_start(State(state): State, AxumPath(name): AxumPath) -> Response { - let logical = strip_container_prefix(&name); - state - .coord - .set_transient(&logical, crate::coordinator::TransientKind::Starting); - let result = lifecycle::start(&logical).await; - state.coord.clear_transient(&logical); - match result { - Ok(()) => Redirect::to("/").into_response(), - Err(e) => error_response(&format!("start {logical} failed: {e:#}")), - } + lifecycle_action( + &state, + &name, + crate::coordinator::TransientKind::Starting, + "start", + |n| async move { lifecycle::start(&n).await }, + |_, _| {}, + ) + .await } async fn post_update_all(State(state): State) -> Response { diff --git a/hive-c0re/src/main.rs b/hive-c0re/src/main.rs index cbdb45d..fde97e0 100644 --- a/hive-c0re/src/main.rs +++ b/hive-c0re/src/main.rs @@ -13,8 +13,8 @@ mod broker; mod client; mod coordinator; mod dashboard; -mod lifecycle; mod events_vacuum; +mod lifecycle; mod manager_server; mod operator_questions; mod server;