Watch
0
0
Fork
You've already forked hyperhive
0

swarm-controller: bash-task handler takes its own sub-state via FromRef

This commit is contained in:
atlas 2026-11-28 18:05:38 +01:00 • committed by mara
commit 37ba403992

View file

@ -11,10 +11,11 @@
//! every key of the bucket for the ones with its prefix. That is fine at this
//! size; a filtered watch would avoid it if the bucket grows.
use std::sync::Arc;
use std::time::Duration;
use axum::Json;
use axum::extract::{Path, State};
use axum::extract::{FromRef, Path, State};
use axum::http::StatusCode;
use futures_util::{StreamExt as _, TryStreamExt as _};
use serde::Serialize;
@ -22,6 +23,24 @@ use swarm_wire::bash_task::{self, State as TaskState, Status};
use utoipa::ToSchema;
use crate::routes::AppState;
use crate::status::StatusReader;
/// What the bash-task route may reach of [`AppState`]: the queue connection.
///
/// 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 BashTasksState {
pub(crate) status: Option<Arc<StatusReader>>,
}
impl FromRef<AppState> for BashTasksState {
fn from_ref(state: &AppState) -> Self {
Self {
status: state.status.clone(),
}
}
}
/// How long one read of a key may take.
const READ_TIMEOUT: Duration = Duration::from_secs(2);
@ -131,7 +150,7 @@ fn newest_first(mut entries: Vec<BashTaskEntry>) -> Vec<BashTaskEntry> {
tag = "agents"
)]
pub(crate) async fn list_bash_tasks(
State(state): State<AppState>,
State(state): State<BashTasksState>,
Path(agent): Path<String>,
) -> Result<Json<Vec<BashTaskEntry>>, problem_details::ProblemDetails> {
let agent = hive_types::Ident::parse(&agent)
@ -244,6 +263,41 @@ fn read_failed(agent: &str, e: &dyn std::error::Error) -> problem_details::Probl
#[cfg(test)]
mod tests {
use super::*;
use crate::routes::tests::{state_with_roster, state_with_roster_queued};
async fn get_status(state: AppState, path: &str) -> (StatusCode, String) {
use axum::body::Body;
use axum::http::Request;
use tower::ServiceExt as _;
let res = crate::startup::build_app(state)
.oneshot(Request::get(path).body(Body::empty()).expect("a request"))
.await
.expect("answers");
let code = res.status();
let body = axum::body::to_bytes(res.into_body(), usize::MAX)
.await
.expect("a body");
(code, String::from_utf8_lossy(&body).into_owned())
}
/// Through the real router, so `FromRef` runs: with the queue the app state
/// holds, an agent with no bucket yet lists nothing (200, `[]`).
#[tokio::test]
async fn lists_nothing_through_the_queue_the_app_state_holds() {
let (_nats, state, _sched) = state_with_roster_queued().await;
let (code, body) = get_status(state, "/api/agents/atlas/bash-tasks").await;
assert_eq!((code, body.as_str()), (StatusCode::OK, "[]"));
}
/// Presence control for the test above: without a queue the same request
/// is 503.
#[tokio::test]
async fn answers_503_without_a_queue() {
let (state, _sched) = state_with_roster();
let (code, _) = get_status(state, "/api/agents/atlas/bash-tasks").await;
assert_eq!(code, StatusCode::SERVICE_UNAVAILABLE);
}
fn status(state: TaskState, created: i64, ts: i64) -> Status {
Status {