The registration token page moves to the runner list once its token has registered a runner, instead of showing a spent token until reloaded
Browse files at this commitb6feaae7a2f7ec84cc6b82f91d73dea9fe08fb3dParent: f4a7addSome checks failed
Show checks
ci/buildworkspace: workspace io: No such file or directory (os error 2)Details
- feat
- the registration token page moves to the runner list once its token has registered a runner, instead of showing a spent token until reloaded
3 changed files +82 -0
- crates/forge-core/src/store/ci.rs +13 -0
- crates/forge-web/assets/nav.js +4 -0
- crates/forge-web/src/pages/admin_runners.rs +65 -0
crates/forge-core/src/store/ci.rsmodified+13 -0
| @@ -452,6 +452,19 @@ impl Store { | ||
| 452 | 452 | Ok(token) |
| 453 | 453 | } |
| 454 | 454 | |
| 455 | + /// Whether the registration token with this hash can no longer register a runner: used or gone. | |
| 456 | + pub async fn registration_token_used(&self, token_hash: &str) -> Result<bool> { | |
| 457 | + let conn = self.inner.conn.lock().await; | |
| 458 | + let mut rows = conn | |
| 459 | + .query( | |
| 460 | + "SELECT 1 FROM ci_registration_tokens WHERE token_hash = ?1 AND used_at IS NULL", | |
| 461 | + vec![text(token_hash)], | |
| 462 | + ) | |
| 463 | + .await | |
| 464 | + .map_err(db_err)?; | |
| 465 | + Ok(rows.next().await.map_err(db_err)?.is_none()) | |
| 466 | + } | |
| 467 | + | |
| 455 | 468 | /// Consumes a registration token and creates a runner. Returns the runner and its secret, |
| 456 | 469 | /// which is shown once. `Invalid` when the token is unknown, used or expired. |
| 457 | 470 | pub async fn register_runner( |
crates/forge-web/assets/nav.jsmodified+4 -0
| @@ -2777,6 +2777,10 @@ | ||
| 2777 | 2777 | liveTimer = setTimeout(function () { liveTimer = null; refreshLive(); }, 1000); |
| 2778 | 2778 | }); |
| 2779 | 2779 | es.addEventListener('log', function (e) { appendLog(e.data, e.lastEventId); }); |
| 2780 | + es.addEventListener('go', function (e) { | |
| 2781 | + var url = new URL(e.data, location.href); | |
| 2782 | + load({ target: url, dest: url, init: GET_INIT, how: 'nav', keep: false, post: false, form: null }); | |
| 2783 | + }); | |
| 2780 | 2784 | return es; |
| 2781 | 2785 | } |
| 2782 | 2786 |
crates/forge-web/src/pages/admin_runners.rsmodified+65 -0
| @@ -41,6 +41,7 @@ use crate::views::repo_list::{PageLink, pager, pager_links}; | ||
| 41 | 41 | use crate::views::{alert, enc, now_unix, relative_time, shell}; |
| 42 | 42 | |
| 43 | 43 | path_param!(runner_id); |
| 44 | +path_param!(token_hash); | |
| 44 | 45 | |
| 45 | 46 | /// Runners per page on the list. |
| 46 | 47 | const PER_PAGE: usize = 6; |
| @@ -486,8 +487,13 @@ async fn load_meter(cx: &Cx, lang: Lang, running: u32, max: u32) -> Result<impl | ||
| 486 | 487 | |
| 487 | 488 | #[component] |
| 488 | 489 | async fn token_view(cx: &Cx, lang: Lang, token: String, command: String) -> Result<impl View> { |
| 490 | + let live = format!( | |
| 491 | + "/admin/runners/tokens/{}/events", | |
| 492 | + forge_core::token::hash_token(&token) | |
| 493 | + ); | |
| 489 | 494 | Ok(view! { |
| 490 | 495 | head(title: g::t(lang, K::RegistrationToken)) |
| 496 | + <div hidden="" data-live=(live.as_str())></div> | |
| 491 | 497 | <p class="state" role="status"><strong>(g::t(lang, K::CopyTokenNow))</strong></p> |
| 492 | 498 | <div class="sheet"> |
| 493 | 499 | <div class="sheet-tools"><strong>(g::t(lang, K::Token))</strong> <a class="act" href="/admin/runners">(g::t(lang, K::BackToRunners))</a></div> |
| @@ -1770,6 +1776,38 @@ async fn runners_events(cx: &Cx) -> Result<Response> { | ||
| 1770 | 1776 | events_response(cx, events) |
| 1771 | 1777 | } |
| 1772 | 1778 | |
| 1779 | +/// The token page's stream: one `go` event to the runner list once its token has registered a | |
| 1780 | +/// runner, so the page does not keep showing a spent token. | |
| 1781 | +#[route(GET "/admin/runners/tokens/{token_hash}/events")] | |
| 1782 | +async fn token_events(cx: &Cx) -> Result<Response> { | |
| 1783 | + admin_only(cx).await?; | |
| 1784 | + let state = context::<AppState>(cx).clone(); | |
| 1785 | + let hash = token_hash(cx).to_owned(); | |
| 1786 | + let wake = state.ci_live.subscribe_runners(); | |
| 1787 | + let events = futures_util::stream::unfold(Some(wake), move |wake| { | |
| 1788 | + let state = state.clone(); | |
| 1789 | + let hash = hash.clone(); | |
| 1790 | + async move { | |
| 1791 | + let mut wake = wake?; | |
| 1792 | + loop { | |
| 1793 | + if state | |
| 1794 | + .store | |
| 1795 | + .registration_token_used(&hash) | |
| 1796 | + .await | |
| 1797 | + .unwrap_or(false) | |
| 1798 | + { | |
| 1799 | + let go = topcoat::router::content::sse::Event::new() | |
| 1800 | + .event("go") | |
| 1801 | + .data("/admin/runners"); | |
| 1802 | + return Some((Ok(go), None)); | |
| 1803 | + } | |
| 1804 | + wake.changed().await.ok()?; | |
| 1805 | + } | |
| 1806 | + } | |
| 1807 | + }); | |
| 1808 | + events_response(cx, events) | |
| 1809 | +} | |
| 1810 | + | |
| 1773 | 1811 | /// Sets or resets one config field of a runner. Concurrency and labels are live; the rest are |
| 1774 | 1812 | /// stored for the runner's next start. Read-only fields and bad values change nothing. |
| 1775 | 1813 | #[route(POST "/admin/runners/{runner_id}/config")] |
| @@ -1988,6 +2026,33 @@ mod tests { | ||
| 1988 | 2026 | store.list_runners().await.expect("list").remove(0) |
| 1989 | 2027 | } |
| 1990 | 2028 | |
| 2029 | + #[tokio::test] | |
| 2030 | + async fn a_used_token_sends_its_page_to_the_runner_list() { | |
| 2031 | + let dir = tempfile::tempdir().expect("tempdir"); | |
| 2032 | + let (store, router) = fixture(dir.path()).await; | |
| 2033 | + let (_, created) = send( | |
| 2034 | + &router, | |
| 2035 | + "POST", | |
| 2036 | + "/admin/runners/tokens", | |
| 2037 | + Some(ADMIN_TOKEN), | |
| 2038 | + csrf_form(""), | |
| 2039 | + ) | |
| 2040 | + .await; | |
| 2041 | + let token = token_from(&created); | |
| 2042 | + let live = format!( | |
| 2043 | + "/admin/runners/tokens/{}/events", | |
| 2044 | + forge_core::token::hash_token(&token) | |
| 2045 | + ); | |
| 2046 | + assert!(created.contains(&live)); | |
| 2047 | + store | |
| 2048 | + .register_runner(&token, "box", &[]) | |
| 2049 | + .await | |
| 2050 | + .expect("register"); | |
| 2051 | + let (status, body) = send(&router, "GET", &live, Some(ADMIN_TOKEN), None).await; | |
| 2052 | + assert_eq!(status, 200); | |
| 2053 | + assert!(body.contains("event: go\ndata: /admin/runners"), "{body}"); | |
| 2054 | + } | |
| 2055 | + | |
| 1991 | 2056 | // Scenario: the page gates, the one-time token, and labels as chips with a state badge. |
| 1992 | 2057 | #[tokio::test] |
| 1993 | 2058 | async fn gates_token_once_and_labels_render_as_chips() { |