From b9687c54f289ace2f6156f97c7cdcfa989fb3b62 Mon Sep 17 00:00:00 2001 From: atlas Date: Sat, 28 Nov 2026 06:58:15 +0100 Subject: [PATCH] swarm-controller: alerts and jobq-graph handlers take their own sub-state via FromRef --- swarm-controller/src/observability/alerts.rs | 5 ++-- swarm-controller/src/observability/mod.rs | 23 ++++++++++++++++++ swarm-controller/src/routes/jobq_graph.rs | 25 ++++++++++++++++++-- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/swarm-controller/src/observability/alerts.rs b/swarm-controller/src/observability/alerts.rs index d66d78dd9..f345dbe70 100644 --- a/swarm-controller/src/observability/alerts.rs +++ b/swarm-controller/src/observability/alerts.rs @@ -23,7 +23,8 @@ use problem_details::ProblemDetails; use serde::{Deserialize, Serialize}; use utoipa::ToSchema; -use crate::routes::{AppState, error_problem}; +use super::AlertsState; +use crate::routes::error_problem; /// The env var `swarm-controller.nix` sets. const URL_ENV: &str = "SWARM_CONTROLLER_GRAFANA_URL"; @@ -214,7 +215,7 @@ pub(crate) fn from_env() -> Option> { tag = "alerts" )] pub(crate) async fn get_alerts( - State(state): State, + State(state): State, ) -> Result>, ProblemDetails> { let Some(grafana) = state.alerts.as_ref() else { return Err(error_problem( diff --git a/swarm-controller/src/observability/mod.rs b/swarm-controller/src/observability/mod.rs index 97071b5ee..38902fbe8 100644 --- a/swarm-controller/src/observability/mod.rs +++ b/swarm-controller/src/observability/mod.rs @@ -5,3 +5,26 @@ pub(crate) mod alerts; pub(crate) mod held_deploys; pub(crate) mod otel_http_client; pub(crate) mod vcs_metrics; + +use std::sync::Arc; + +use axum::extract::FromRef; + +use crate::routes::AppState; + +/// What the alerts route may reach of [`AppState`]: Grafana's alerts client. +/// +/// Derived per request from [`AppState`] (`FromRef`), so the field is a clone +/// of the same `Arc` and nothing is held twice. +#[derive(Clone)] +pub(crate) struct AlertsState { + pub(crate) alerts: Option>, +} + +impl FromRef for AlertsState { + fn from_ref(state: &AppState) -> Self { + Self { + alerts: state.alerts.clone(), + } + } +} diff --git a/swarm-controller/src/routes/jobq_graph.rs b/swarm-controller/src/routes/jobq_graph.rs index 760029fa7..38828429b 100644 --- a/swarm-controller/src/routes/jobq_graph.rs +++ b/swarm-controller/src/routes/jobq_graph.rs @@ -1,11 +1,32 @@ //! Read-only views of the swarm-level job graph. +use std::sync::{Arc, Mutex}; + +use axum::extract::FromRef; use axum::{Json, extract::State}; use hive_jobq_wire::GraphWire as _; use serde::Deserialize; +use crate::jobq::{SwarmNodeKind, SwarmResourceKind}; use crate::routes::AppState; +/// What the job-graph routes may reach of [`AppState`]: the scheduler. +/// +/// Derived per request from [`AppState`] (`FromRef`), so the field is a clone +/// of the same `Arc` and nothing is held twice. +#[derive(Clone)] +pub(crate) struct JobqState { + pub(crate) jobq: Arc>>, +} + +impl FromRef for JobqState { + fn from_ref(state: &AppState) -> Self { + Self { + jobq: Arc::clone(&state.jobq), + } + } +} + /// Query params for `GET /api/jobq/graph` — `?states=` narrows to root /// groups in the named states, same shape `hive_jobq_wire::parse_states` /// parses. @@ -28,7 +49,7 @@ pub(crate) struct JobqGraphQuery { tag = "jobq" )] pub(crate) async fn get_jobq_graph( - State(state): State, + State(state): State, axum::extract::Query(q): axum::extract::Query, ) -> Json> { let states = hive_jobq_wire::parse_states(q.states.as_deref()); @@ -53,7 +74,7 @@ pub(crate) async fn get_jobq_graph( tag = "jobq" )] pub(crate) async fn get_jobq_rollup( - State(state): State, + State(state): State, ) -> Json> { let sched = state .jobq