diff --git a/crates/forge-core/src/store/ci.rs b/crates/forge-core/src/store/ci.rs index 1a71f3e..7e53ee1 100644 --- a/crates/forge-core/src/store/ci.rs +++ b/crates/forge-core/src/store/ci.rs @@ -452,6 +452,19 @@ impl Store { Ok(token) } + /// Whether the registration token with this hash can no longer register a runner: used or gone. + pub async fn registration_token_used(&self, token_hash: &str) -> Result { + let conn = self.inner.conn.lock().await; + let mut rows = conn + .query( + "SELECT 1 FROM ci_registration_tokens WHERE token_hash = ?1 AND used_at IS NULL", + vec![text(token_hash)], + ) + .await + .map_err(db_err)?; + Ok(rows.next().await.map_err(db_err)?.is_none()) + } + /// Consumes a registration token and creates a runner. Returns the runner and its secret, /// which is shown once. `Invalid` when the token is unknown, used or expired. pub async fn register_runner( diff --git a/crates/forge-web/assets/nav.js b/crates/forge-web/assets/nav.js index 8045d42..7bd7541 100644 --- a/crates/forge-web/assets/nav.js +++ b/crates/forge-web/assets/nav.js @@ -2777,6 +2777,10 @@ liveTimer = setTimeout(function () { liveTimer = null; refreshLive(); }, 1000); }); es.addEventListener('log', function (e) { appendLog(e.data, e.lastEventId); }); + es.addEventListener('go', function (e) { + var url = new URL(e.data, location.href); + load({ target: url, dest: url, init: GET_INIT, how: 'nav', keep: false, post: false, form: null }); + }); return es; } diff --git a/crates/forge-web/src/pages/admin_runners.rs b/crates/forge-web/src/pages/admin_runners.rs index 61b47a7..1ff2156 100644 --- a/crates/forge-web/src/pages/admin_runners.rs +++ b/crates/forge-web/src/pages/admin_runners.rs @@ -41,6 +41,7 @@ use crate::views::repo_list::{PageLink, pager, pager_links}; use crate::views::{alert, enc, now_unix, relative_time, shell}; path_param!(runner_id); +path_param!(token_hash); /// Runners per page on the list. const PER_PAGE: usize = 6; @@ -486,8 +487,13 @@ async fn load_meter(cx: &Cx, lang: Lang, running: u32, max: u32) -> Result Result { + let live = format!( + "/admin/runners/tokens/{}/events", + forge_core::token::hash_token(&token) + ); Ok(view! { head(title: g::t(lang, K::RegistrationToken)) +

(g::t(lang, K::CopyTokenNow))

(g::t(lang, K::Token)) (g::t(lang, K::BackToRunners))
@@ -1770,6 +1776,38 @@ async fn runners_events(cx: &Cx) -> Result { events_response(cx, events) } +/// The token page's stream: one `go` event to the runner list once its token has registered a +/// runner, so the page does not keep showing a spent token. +#[route(GET "/admin/runners/tokens/{token_hash}/events")] +async fn token_events(cx: &Cx) -> Result { + admin_only(cx).await?; + let state = context::(cx).clone(); + let hash = token_hash(cx).to_owned(); + let wake = state.ci_live.subscribe_runners(); + let events = futures_util::stream::unfold(Some(wake), move |wake| { + let state = state.clone(); + let hash = hash.clone(); + async move { + let mut wake = wake?; + loop { + if state + .store + .registration_token_used(&hash) + .await + .unwrap_or(false) + { + let go = topcoat::router::content::sse::Event::new() + .event("go") + .data("/admin/runners"); + return Some((Ok(go), None)); + } + wake.changed().await.ok()?; + } + } + }); + events_response(cx, events) +} + /// Sets or resets one config field of a runner. Concurrency and labels are live; the rest are /// stored for the runner's next start. Read-only fields and bad values change nothing. #[route(POST "/admin/runners/{runner_id}/config")] @@ -1988,6 +2026,33 @@ mod tests { store.list_runners().await.expect("list").remove(0) } + #[tokio::test] + async fn a_used_token_sends_its_page_to_the_runner_list() { + let dir = tempfile::tempdir().expect("tempdir"); + let (store, router) = fixture(dir.path()).await; + let (_, created) = send( + &router, + "POST", + "/admin/runners/tokens", + Some(ADMIN_TOKEN), + csrf_form(""), + ) + .await; + let token = token_from(&created); + let live = format!( + "/admin/runners/tokens/{}/events", + forge_core::token::hash_token(&token) + ); + assert!(created.contains(&live)); + store + .register_runner(&token, "box", &[]) + .await + .expect("register"); + let (status, body) = send(&router, "GET", &live, Some(ADMIN_TOKEN), None).await; + assert_eq!(status, 200); + assert!(body.contains("event: go\ndata: /admin/runners"), "{body}"); + } + // Scenario: the page gates, the one-time token, and labels as chips with a state badge. #[tokio::test] async fn gates_token_once_and_labels_render_as_chips() {