swarm-controller: config-pr handlers take their own sub-state via FromRef
This commit is contained in:
parent
37ba403992
commit
53382d9c78
2 changed files with 65 additions and 3 deletions
|
|
@ -173,7 +173,7 @@ const POLL_INTERVAL: std::time::Duration = std::time::Duration::from_mins(5);
|
|||
pub struct ConfigPrCache(Mutex<HashMap<String, ConfigPrStatus>>);
|
||||
|
||||
impl ConfigPrCache {
|
||||
fn new() -> Self {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self(Mutex::new(HashMap::new()))
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,14 +1,36 @@
|
|||
//! Agents' open config PRs.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
use axum::extract::FromRef;
|
||||
use axum::{
|
||||
Json,
|
||||
extract::{Path, State},
|
||||
};
|
||||
|
||||
use crate::config_pr::ConfigPrCache;
|
||||
use crate::forge;
|
||||
use crate::jobq::Late;
|
||||
use crate::routes::{AppState, StatusUnavailable};
|
||||
|
||||
/// What the config-PR routes may reach of [`AppState`]: the config-PR cache.
|
||||
///
|
||||
/// 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 ConfigPrsState {
|
||||
pub(crate) config_prs: Late<Arc<ConfigPrCache>>,
|
||||
}
|
||||
|
||||
impl FromRef<AppState> for ConfigPrsState {
|
||||
fn from_ref(state: &AppState) -> Self {
|
||||
Self {
|
||||
config_prs: Arc::clone(&state.config_prs),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `agent`'s open config PR, from [`ConfigPrCache`](crate::config_pr::ConfigPrCache) — kept
|
||||
/// current by a webhook nudge (near-real-time) backstopped by a periodic
|
||||
/// scan (worst case: up to one `config_pr::POLL_INTERVAL` stale if a
|
||||
|
|
@ -32,7 +54,7 @@ use crate::routes::{AppState, StatusUnavailable};
|
|||
tag = "agents"
|
||||
)]
|
||||
pub(crate) async fn get_agent_config_pr(
|
||||
State(state): State<AppState>,
|
||||
State(state): State<ConfigPrsState>,
|
||||
Path(name): Path<String>,
|
||||
) -> Result<Json<Option<forge::ConfigPrStatus>>, StatusUnavailable> {
|
||||
let Some(cache) = state.config_prs.get() else {
|
||||
|
|
@ -70,7 +92,7 @@ pub(crate) async fn get_agent_config_pr(
|
|||
tag = "agents"
|
||||
)]
|
||||
pub(crate) async fn get_config_prs(
|
||||
State(state): State<AppState>,
|
||||
State(state): State<ConfigPrsState>,
|
||||
) -> Result<Json<std::collections::HashMap<String, forge::ConfigPrStatus>>, StatusUnavailable> {
|
||||
let Some(cache) = state.config_prs.get() else {
|
||||
return Err(StatusUnavailable(
|
||||
|
|
@ -79,3 +101,43 @@ pub(crate) async fn get_config_prs(
|
|||
};
|
||||
Ok(Json(cache.snapshot()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use axum::body::Body;
|
||||
use axum::http::{Request, StatusCode};
|
||||
use tower::ServiceExt as _;
|
||||
|
||||
use super::*;
|
||||
|
||||
async fn get_config_prs_status(cache: Option<ConfigPrCache>) -> StatusCode {
|
||||
let (state, _) = crate::routes::tests::state_with_roster();
|
||||
if let Some(cache) = cache {
|
||||
assert!(state.config_prs.set(Arc::new(cache)).is_ok());
|
||||
}
|
||||
let req = Request::get("/api/config-prs")
|
||||
.body(Body::empty())
|
||||
.expect("a request");
|
||||
crate::startup::build_app(state)
|
||||
.oneshot(req)
|
||||
.await
|
||||
.expect("infallible")
|
||||
.status()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn serves_the_cache_the_app_state_holds() {
|
||||
assert_eq!(
|
||||
get_config_prs_status(Some(ConfigPrCache::new())).await,
|
||||
StatusCode::OK
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn is_unavailable_without_a_cache() {
|
||||
assert_eq!(
|
||||
get_config_prs_status(None).await,
|
||||
StatusCode::SERVICE_UNAVAILABLE
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue