swarm-controller: track agent config-PR status independently of hives
This commit is contained in:
parent
3c390f8132
commit
66c494697f
3 changed files with 217 additions and 2 deletions
91
swarm-controller/src/config_pr.rs
Normal file
91
swarm-controller/src/config_pr.rs
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
//! Swarm-level config-PR status, polled independently of any one hive.
|
||||
//!
|
||||
//! `hive-c0re::forge::config_pr_poll` already scans `agent-configs/*` for
|
||||
//! open PRs — but it does that once per hive, to queue that hive's own
|
||||
//! `MergeConfigPr` approval, and nothing at swarm level reads the result.
|
||||
//! swarm-ui's config-PR panel needs a *swarm*-level answer to "does agent X
|
||||
//! have an open config PR" that does not depend on which hive currently
|
||||
//! hosts X being reachable.
|
||||
//!
|
||||
//! This mirrors `hive-c0re`'s poll shape (period, idempotent full rescan)
|
||||
//! rather than consuming the swarm-wide `pull_request` webhook
|
||||
//! (`crate::webhook::DeliveryKind::ConfigPr`) that already lands here: that
|
||||
//! delivery is unparsed today, and building a reliable event path means
|
||||
//! building this poll as a missed-delivery backstop anyway (the same reason
|
||||
//! `hive-c0re`'s own poller exists). Shipping the backstop alone first
|
||||
//! avoids paying for both at once; wiring the webhook as a low-latency nudge
|
||||
//! on top is a cheap follow-up once this cache is the source of truth.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crate::forge::{Client, ConfigPrStatus};
|
||||
|
||||
/// How often to rescan `agent-configs/*`. Matches the interval named in
|
||||
/// `hive-c0re::forge::config_pr_poll`'s own doc comment — same org, same
|
||||
/// staleness tolerance, no reason for the two to disagree.
|
||||
const POLL_INTERVAL: std::time::Duration = std::time::Duration::from_mins(5);
|
||||
|
||||
/// The latest full scan, replaced atomically each cycle.
|
||||
///
|
||||
/// A full replace rather than an incremental merge: `Client::list_open_config_prs`
|
||||
/// already returns the complete current set (an agent with no open PR is
|
||||
/// simply absent), so merging would need its own stale-entry eviction to
|
||||
/// avoid an agent's long-closed PR lingering forever — the same reconcile
|
||||
/// problem `hive-c0re`'s poller solves for its approvals. A full replace
|
||||
/// sidesteps needing that logic twice: the map at any moment is nothing more
|
||||
/// than "the last successful scan's answer."
|
||||
pub struct ConfigPrCache(Mutex<HashMap<String, ConfigPrStatus>>);
|
||||
|
||||
impl ConfigPrCache {
|
||||
fn new() -> Self {
|
||||
Self(Mutex::new(HashMap::new()))
|
||||
}
|
||||
|
||||
/// `agent`'s open PR, if the last successful scan found one.
|
||||
///
|
||||
/// Returns `None` both when the agent has no open PR and when no scan
|
||||
/// has completed yet — the caller (`GET /api/agents/<name>/config-pr`)
|
||||
/// treats both as "nothing to show," which is the honest answer for a
|
||||
/// value that's a best-effort cache, not a live read.
|
||||
pub fn get(&self, agent: &str) -> Option<ConfigPrStatus> {
|
||||
self.0
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.get(agent)
|
||||
.cloned()
|
||||
}
|
||||
|
||||
fn replace(&self, scan: HashMap<String, ConfigPrStatus>) {
|
||||
*self
|
||||
.0
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner) = scan;
|
||||
}
|
||||
}
|
||||
|
||||
/// Build an empty cache and spawn the periodic scan that keeps it current.
|
||||
///
|
||||
/// The scan runs immediately on the first tick (`tokio::time::interval`'s
|
||||
/// default), so the cache is populated on startup rather than staying empty
|
||||
/// for a full [`POLL_INTERVAL`] after boot.
|
||||
pub fn spawn(client: Arc<Client>) -> Arc<ConfigPrCache> {
|
||||
let cache = Arc::new(ConfigPrCache::new());
|
||||
let cache_for_task = Arc::clone(&cache);
|
||||
tokio::spawn(async move {
|
||||
let mut ticker = tokio::time::interval(POLL_INTERVAL);
|
||||
loop {
|
||||
ticker.tick().await;
|
||||
match client.list_open_config_prs().await {
|
||||
Ok(scan) => cache_for_task.replace(scan),
|
||||
Err(e) => {
|
||||
tracing::warn!(
|
||||
error = %format!("{e:#}"),
|
||||
"config-pr poll: scan failed, cache keeps its last value"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
cache
|
||||
}
|
||||
Loading…
Reference in a new issue