diff --git a/.forge/ci.toml b/.forge/ci.toml index 065b698..12ddc82 100644 --- a/.forge/ci.toml +++ b/.forge/ci.toml @@ -7,6 +7,7 @@ push = ["main"] pull_request = true manual = true +tags = ["v*"] [jobs.test] image = "docker.io/library/rust:1" @@ -34,3 +35,6 @@ mkdir -p dist && cp target/dist/forge dist/forge artifacts = ["dist/forge", "forge-*-linux", "SHA256SUMS"] cache = ["target"] timeout_minutes = 120 + +[release] +assets = ["build:forge-*-linux", "build:SHA256SUMS"] diff --git a/crates/forge-web/assets/nav.js b/crates/forge-web/assets/nav.js index 13330aa..34e161c 100644 --- a/crates/forge-web/assets/nav.js +++ b/crates/forge-web/assets/nav.js @@ -153,6 +153,7 @@ followTail(); syncUnread(); syncLive(); + syncTick(); } // The log tail is a terminal: it opens at its end. A reader who scrolls up keeps their place. @@ -487,12 +488,14 @@ // the stream once the status is terminal, and the swap that shows that page (no data-live) closes it. var liveStream = null; var liveUrl = null; + var liveTimer = null; // a run list's pending refresh: bursts of events (log lines) share one function closeLive() { if (liveStream) { liveStream.close(); } liveStream = null; liveUrl = null; liveDirty = false; + if (liveTimer) { clearTimeout(liveTimer); liveTimer = null; } } function syncLive() { @@ -511,7 +514,10 @@ // A job page's stream opens with the status the page already shows: nothing to re-render then. var wrap = document.querySelector('[data-live]'); if (document.querySelector('[data-live-log]') && wrap && wrap.getAttribute('data-live-state') === e.data) { return; } - refreshLive(); + // The run list changes on every log chunk of every job: refresh at most once a second. + if (document.querySelector('[data-live-log]')) { refreshLive(); return; } + if (liveTimer) { return; } + liveTimer = setTimeout(function () { liveTimer = null; refreshLive(); }, 1000); }); liveStream.addEventListener('log', function (e) { appendLog(e.data); }); } @@ -536,8 +542,92 @@ load({ target: withoutHash(here), dest: here, init: GET_INIT, how: 'live', keep: true, post: false, form: null }); } + // Local clock for what runs. Server-rendered [data-since] (unix seconds) is the start of a duration + // still counting: its text is rewritten each second. [data-tick] boxes of the run list's bars and + // the run timeline grow with the clock. This is a display clock only: no request is made. The + // timer lives while such elements are on the page and stops when a swap removes the last one. The + // server's next event brings the true state. [data-now] on the page root gives the server's clock. + var tickTimer = null; + var tickSkew = 0; // server clock minus the browser's, in ms + + function stopTick() { + if (tickTimer) { clearInterval(tickTimer); } + tickTimer = null; + } + + function syncTick() { + var root = document.querySelector('[data-now]'); + if (!root || !document.querySelector('[data-since], [data-tick]')) { stopTick(); return; } + tickSkew = (parseInt(root.getAttribute('data-now'), 10) || 0) * 1000 - Date.now(); + if (!tickTimer) { tickTimer = setInterval(tickAll, 1000); } + tickAll(); + } + + function fmtDuration(secs, root) { + secs = Math.max(0, Math.floor(secs)); + if (secs >= 60) { + return (root.getAttribute('data-fm') || '{m}m {s}s') + .replace('{m}', Math.floor(secs / 60)).replace('{s}', secs % 60); + } + return (root.getAttribute('data-fs') || '{s}s').replace('{s}', secs); + } + + function place(offset, span, width) { + if (span <= 0) { return 0; } + return Math.round(Math.min(Math.max(offset, 0), span) * width / span); + } + + // Geometry shared with the server: the timeline's track and the list bar's width. + var TRACK_X = 100, TRACK_W = 530, LIST_W = 1000; + + function tickAll() { + var root = document.querySelector('[data-now]'); + var live = document.querySelectorAll('[data-since], [data-tick]'); + if (!root || !live.length) { stopTick(); return; } + if (document.hidden) { return; } + var now = (Date.now() + tickSkew) / 1000; + Array.prototype.forEach.call(live, function (el) { + var since = el.getAttribute('data-since'); + if (since !== null) { + var text = fmtDuration(now - parseInt(since, 10), root); + var pre = el.getAttribute('data-pre'); + if (pre !== null) { text = pre + text + (el.getAttribute('data-post') || ''); } + if (el.textContent !== text) { el.textContent = text; } + return; + } + var svg = el.ownerSVGElement; + var how = el.getAttribute('data-tick'); + if (!svg || !how) { return; } + var origin = parseInt(svg.getAttribute('data-origin'), 10) || 0; + var xn, min, right; + if (svg.hasAttribute('data-len')) { + xn = TRACK_X + place(now - origin, parseInt(svg.getAttribute('data-len'), 10), TRACK_W); + min = 3; right = TRACK_X + TRACK_W; + } else { + xn = place(now - origin, parseInt(svg.getAttribute('data-scale'), 10), LIST_W); + min = 2; right = LIST_W; + } + var x = parseFloat(el.getAttribute('x')) || 0; + if (how === 'grow') { el.setAttribute('width', Math.max(min, xn - x)); } + else if (how === 'edge') { el.setAttribute('x', Math.max(0, xn - 3)); } + else if (how === 'follow' || how === 'rest') { + el.setAttribute('x', xn); + el.setAttribute('width', Math.max(0, right - xn)); + } + }); + // Labels that sit at the moving edge (a waiting job's "waiting"). + Array.prototype.forEach.call(document.querySelectorAll('text[data-follow]'), function (t) { + var svg = t.ownerSVGElement; + if (!svg || !svg.hasAttribute('data-len')) { return; } + var xn = TRACK_X + place(now - (parseInt(svg.getAttribute('data-origin'), 10) || 0), + parseInt(svg.getAttribute('data-len'), 10), TRACK_W); + t.setAttribute('x', xn + (parseInt(t.getAttribute('data-follow'), 10) || 0)); + }); + } + followTail(); syncLive(); + syncTick(); window.__forgeQuery = function (href) { var url = pageUrl(href); diff --git a/crates/forge-web/src/ci/api.rs b/crates/forge-web/src/ci/api.rs index d46ac3a..4f2fe46 100644 --- a/crates/forge-web/src/ci/api.rs +++ b/crates/forge-web/src/ci/api.rs @@ -497,6 +497,11 @@ async fn write_log( .await .map_err(api::internal)?; state.ci_live.bump_job(&job.id); + // The run page shows the last log lines, so its streams wake too. The page debounces. + state.ci_live.bump_run(&job.run_id); + if let Ok(Some(run)) = state.store.run_by_id(&job.run_id).await { + state.ci_live.bump_repo(&run.repo_id); + } Ok(LogWrite::Stored(size + len)) } diff --git a/crates/forge-web/src/pages/ci_runs.rs b/crates/forge-web/src/pages/ci_runs.rs index 9d9c0fd..ac5a601 100644 --- a/crates/forge-web/src/pages/ci_runs.rs +++ b/crates/forge-web/src/pages/ci_runs.rs @@ -196,6 +196,17 @@ fn duration_text(lang: Lang, started: Option<&str>, finished: Option<&str>) -> S } } +/// The duration formats of `lang` with `{m}` and `{s}` where the numbers go, for the client ticker: +/// the one with minutes and the seconds-only one. +fn duration_templates(lang: Lang) -> (String, String) { + ( + g::duration_minutes(lang, 111_111, 222_222) + .replace("111111", "{m}") + .replace("222222", "{s}"), + g::duration_seconds(lang, 222_222).replace("222222", "{s}"), + ) +} + /// Words for a state name, as the pages show it: `waiting_approval` reads "waiting approval". /// A state this build does not know is shown with its underscores as spaces. pub fn state_text(lang: Lang, state: &str) -> String { @@ -290,6 +301,11 @@ struct RunRow { title: String, bar: Vec, duration: String, + /// Unix seconds the duration counts from while the run is live, for the client's local ticker. + since: Option, + /// The run's start and the list's scale, which the ticker needs to grow the running segments. + origin: String, + scale: String, meta: String, } @@ -327,6 +343,8 @@ struct JobLine { detail: String, runner: String, duration: String, + /// Unix seconds a running job's duration counts from. + since: Option, } /// The last lines of the log of the job the run page points at. @@ -348,6 +366,8 @@ struct RunDetail { actor: String, when: String, duration: String, + /// Unix seconds the header duration counts from while the run is not finished. + since: Option, config_error: Option, action_base: String, can_approve: bool, @@ -365,6 +385,8 @@ struct Rect { y: String, w: String, h: String, + /// How the client's ticker moves this box with the clock: `grow`, `edge`, `follow` or `rest`. + tick: Option, } impl Rect { @@ -375,22 +397,39 @@ impl Rect { y: y.to_string(), w: w.to_string(), h: h.to_string(), + tick: None, } } + + fn ticking(mut self, how: &str) -> Rect { + self.tick = Some(how.to_owned()); + self + } } /// A text label of the timeline. +#[derive(Default)] struct Label { class: &'static str, x: String, y: String, anchor: &'static str, text: String, + /// Unix seconds of a running job's start: the ticker writes `pre` + duration + `post` as the text. + since: Option, + pre: Option, + post: Option, + /// Set when the label sits at the moving edge `now`, with this offset from it. + follow: Option, } /// The timeline of a run: its SVG viewBox, its boxes, its connectors and its labels, in draw order. struct Gantt { view_box: String, + /// The axis start and length in seconds, for the ticker. + origin: String, + len: String, + live: bool, rects: Vec, links: Vec, labels: Vec