/* The queue, on screen, for an app that has not written a UI yet. * * * * * * That is the whole of it. `` finds `/ws/jobs` itself, shows what * is running with a progress bar and a Kill button each, says how many are * waiting, and reconnects when the server restarts under you. `` * is the same socket as a chip for a top bar, with the list in a popover; * see its own comment further down. * * One file, and the styles are in it rather than beside it, because the * shadow root scopes them anyway and a second file is a second route, a * second cache header and a second thing to forget to copy. Themed from * outside through the custom properties at the top of `CSS` — set them on * `desk-jobs` or on `:root` and nothing here has to be forked. * * What it deliberately does not do is talk about failures. It raises a * `desk-jobs:notice` event and leaves the wording to the app, because a * useful sentence names things only the app knows — its backends, its feeds, * its config keys. `pipeline/notices.py` is the same split on the server. * * Attributes, all optional: * base="/api" prefix for the HTTP calls, if the router is mounted deeper * socket="..." the socket's URL, if it is not `/ws/jobs` on this origin * probe="/api/status" * an authenticated endpoint to try when the socket closes. A * refused handshake and a dead server look identical from * here — the server rejects an unauthenticated upgrade before * completing it, so the browser reports the same abnormal * close for "your session expired" as for "the server is * gone". One cheap request tells them apart. Without this the * element assumes the server went away, which is the more * common reason and the less confusing thing to be wrong about. * idle="hide" with nothing running or queued, take up no space. The * default, since a chip that is always there is furniture. * idle="show" stay, and say "Nothing running." — for a page with a * heading over the element, which reads as broken over an * empty panel. */ const CSS = ` :host { --desk-jobs-bg: #16181d; --desk-jobs-fg: #e7e9ee; --desk-jobs-dim: #9aa1ad; --desk-jobs-accent: #4c8dff; --desk-jobs-error: #ff6b6b; --desk-jobs-radius: 10px; --desk-jobs-font: system-ui, -apple-system, "Segoe UI", sans-serif; --desk-jobs-gap: 8px; display: block; font-family: var(--desk-jobs-font); font-size: 13px; color: var(--desk-jobs-fg); } :host([hidden]) { display: none; } .strip { display: flex; flex-direction: column; gap: var(--desk-jobs-gap); background: var(--desk-jobs-bg); border-radius: var(--desk-jobs-radius); padding: 10px 12px; } .job { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; align-items: center; } .what { font-weight: 600; } .said { color: var(--desk-jobs-dim); grid-column: 1; } .count { color: var(--desk-jobs-dim); font-variant-numeric: tabular-nums; } /* A bar only when there is a fraction to draw. A handler that reports no * total is working on one thing, and an empty trough reads as stalled. */ .bar { grid-column: 1 / -1; height: 3px; border-radius: 2px; background: color-mix(in srgb, var(--desk-jobs-fg) 15%, transparent); overflow: hidden; } .bar > i { display: block; height: 100%; background: var(--desk-jobs-accent); transition: width .2s; } button { font: inherit; color: var(--desk-jobs-dim); background: none; border: 1px solid currentColor; border-radius: 6px; padding: 1px 8px; cursor: pointer; } button:hover { color: var(--desk-jobs-error); } button[disabled] { opacity: .4; cursor: default; } .offline { color: var(--desk-jobs-error); } .idle { color: var(--desk-jobs-dim); } /* A finger: Kill is a thumb's height. Sizes only; the layout is the width * query below, and neither reads the other. */ @media (pointer: coarse) { button { min-height: 2.5rem; padding: 4px 12px; } } /* A phone: the app's card around the strip is the gutter, and a second one * inside it wasted a fifth of the width. 40rem is app.css's number. */ @media (max-width: 40rem) { .strip { padding: 0; } } `; /** Backing off rather than hammering. The usual reason a socket closed is the * server restarting under --reload, which comes back inside a second; the * ceiling is for when it is not coming back at all. */ const FIRST_WAIT = 500; const LONGEST_WAIT = 30000; /** How long down is long enough to say so. A reload blinks the socket every * time a file is saved, and a warning that flashes on every save is one you * stop reading — which would cost us the case that matters, where the server * really is gone and the strip is quietly showing state from a minute ago. */ const QUIET_ABOUT_IT_FOR = 4000; /** The socket half of both elements: connecting, backing off, the probe, and * the snapshot as it last arrived. Each element holds its own connection, as * `` does; sharing one between elements on a page would be a * registry of subscribers to save a socket the broadcaster does not notice. * A subclass draws `state` in `render()`. */ class Follower extends HTMLElement { root; state = { running: [], queued: [], recent: [], cascades: [] }; offline = false; killing = new Set(); #socket = null; #wait = FIRST_WAIT; #timer = null; constructor(css) { super(); this.root = this.attachShadow({ mode: "open" }); const style = document.createElement("style"); style.textContent = css; this.root.append(style, document.createElement("div")); } connectedCallback() { this.#connect(); this.render(); } disconnectedCallback() { clearTimeout(this.#timer); this.#socket?.close(); this.#socket = null; } get base() { return this.getAttribute("base") ?? ""; } /** Whether a notice on the socket is raised as `desk-jobs:notice`. */ get raisesNotices() { return true; } get #socketUrl() { const given = this.getAttribute("socket"); if (given) return given; const scheme = location.protocol === "https:" ? "wss" : "ws"; return `${scheme}://${location.host}${this.base}/ws/jobs`; } render() {} #connect() { let socket; try { socket = new WebSocket(this.#socketUrl); } catch { this.#reconnect(); // a malformed URL, or no network return; } this.#socket = socket; socket.onopen = () => { this.#wait = FIRST_WAIT; this.offline = false; this.render(); }; socket.onmessage = (event) => { let message; try { message = JSON.parse(event.data); } catch { return; } if (message.type === "jobs") { const { type, ...state } = message; this.state = state; this.render(); } else if (message.type === "notice" && this.raisesNotices) { // The app's to word. Composed, so a listener on `document` hears it // through the shadow boundary. this.dispatchEvent(new CustomEvent("desk-jobs:notice", { detail: message, bubbles: true, composed: true, })); } }; socket.onerror = () => socket.close(); socket.onclose = async () => { if (this.#socket !== socket) return; // a later connection owns us now this.#socket = null; await this.#whyClosed(); this.#reconnect(); }; } /** Ask before assuming. See the `probe` attribute. */ async #whyClosed() { const probe = this.getAttribute("probe"); if (!probe) return; try { const answer = await fetch(this.base + probe, { credentials: "same-origin" }); if (answer.status === 401 || answer.status === 403) { this.dispatchEvent(new CustomEvent("desk-jobs:signed-out", { bubbles: true, composed: true, })); } } catch { /* offline; the element says so below */ } } #reconnect() { if (this.#wait >= QUIET_ABOUT_IT_FOR) { this.offline = true; this.render(); } clearTimeout(this.#timer); this.#timer = setTimeout(() => this.#connect(), this.#wait); this.#wait = Math.min(this.#wait * 2, LONGEST_WAIT); } async kill(id) { this.killing.add(id); this.render(); try { await fetch(`${this.base}/api/jobs/${id}/cancel`, { method: "POST", credentials: "same-origin" }); } finally { // The socket reports the actual ending — a running job stops at its next // checkpoint, so the button going quiet is the whole of what happens here. this.killing.delete(id); } } } class DeskJobs extends Follower { constructor() { super(CSS); } render() { const { running = [], queued = [] } = this.state; const strip = this.root.lastElementChild; const idle = !running.length && !queued.length && !this.offline; const show = this.getAttribute("idle") === "show"; this.hidden = idle && !show; strip.className = "strip"; strip.replaceChildren(); if (this.offline) { const said = document.createElement("div"); said.className = "offline"; said.textContent = "Not connected — what you see here may be out of date."; strip.append(said); } if (idle && show) { const said = document.createElement("div"); said.className = "idle"; said.textContent = "Nothing running."; strip.append(said); } for (const job of running) { strip.append(this.#row(job)); } if (queued.length) { const waiting = document.createElement("div"); waiting.className = "count"; waiting.textContent = `${queued.length} waiting`; strip.append(waiting); } } #row(job) { const row = document.createElement("div"); row.className = "job"; const what = document.createElement("span"); what.className = "what"; what.textContent = job.label || job.kind; row.append(what); const kill = document.createElement("button"); kill.textContent = "Kill"; kill.disabled = this.killing.has(job.id) || job.cancel_requested; kill.addEventListener("click", () => this.kill(job.id)); row.append(kill); if (job.message) { const said = document.createElement("span"); said.className = "said"; said.textContent = job.message; row.append(said); } if (job.total > 0) { const bar = document.createElement("div"); bar.className = "bar"; const fill = document.createElement("i"); fill.style.width = `${Math.min(100, (job.done / job.total) * 100)}%`; bar.append(fill); row.append(bar); } return row; } } /* : the queue as a chip for a top bar, after newsdesk's queue * light. * * * * The chip is a dot, how many jobs are running, and the first one's label and * progress: "Embedding roles, 6 of 6, +2 waiting". Its message is the chip's * tooltip, since a sentence does not fit in a top bar. Grey and "Idle" when * nothing runs, a red dot and "Offline" when the socket is down. Pressing it opens a popover that is the queue as * the queue holds it: one line per running or queued job with its message, * done/total, how long it has been at it, when it is due if it is waiting out * a retry, and Kill; one line per live cascade with Stop run; and, with * `href`, a link to the app's full page. Anywhere else, or Escape, closes it. * * Colours are the `--desk-jobs-*` properties read with fallbacks rather than * set on `:host` as `` sets them, so a theme on `:root` or on an * ancestor reaches it, and with none at all the chip takes the colour and * font of the bar it sits in. `--desk-jobs-warn` is its own: the colour of a * retry's countdown. * * Notices on the socket are not re-raised: this sits on every page, next to a * `` on some of them, and one failure would toast twice. * * Attributes, all optional: * href="/jobs" a link at the foot of the popover. Its text is the * element's content, "All runs" when it has none. * idle="hide" with nothing running or queued, take up no space. The * default is a grey chip, because a light that disappears * cannot say the queue is idle. * base, socket, probe * as for ``. */ const QUEUE_CSS = ` :host { display: inline-block; position: relative; font: inherit; color: inherit; } :host([hidden]) { display: none; } [hidden] { display: none !important; } .chip { display: inline-flex; align-items: center; gap: .4em; max-width: 22rem; font: inherit; font-size: .85em; font-variant-numeric: tabular-nums; color: var(--desk-jobs-dim, currentColor); background: none; border: 1px solid color-mix(in srgb, currentColor 30%, transparent); border-radius: 999px; padding: .2em .7em; cursor: pointer; } .chip.busy { color: var(--desk-jobs-fg, currentColor); } .chip:focus-visible { outline: 2px solid var(--desk-jobs-accent, #4c8dff); outline-offset: 1px; } .dot { flex: none; width: .55em; height: .55em; border-radius: 50%; background: currentColor; opacity: .45; } .chip.busy .dot { background: var(--desk-jobs-accent, #4c8dff); opacity: 1; animation: beat 1.4s ease-in-out infinite; } .chip.offline .dot { background: var(--desk-jobs-error, #d33); opacity: 1; animation: none; } @keyframes beat { 50% { opacity: .35; } } @media (prefers-reduced-motion: reduce) { .chip.busy .dot { animation: none; } } .chip .what { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .chip .what:empty, .n:empty, .n.one { display: none; } .pop { position: absolute; top: calc(100% + 6px); right: 0; z-index: 20; min-width: 22rem; max-width: min(30rem, 90vw); max-height: 60vh; overflow: auto; box-sizing: border-box; padding: 6px; text-align: left; font-family: var(--desk-jobs-font, inherit); font-size: 13px; font-weight: normal; color: var(--desk-jobs-fg, CanvasText); background: var(--desk-jobs-bg, Canvas); border: 1px solid color-mix(in srgb, var(--desk-jobs-fg, CanvasText) 15%, transparent); border-radius: var(--desk-jobs-radius, 10px); box-shadow: 0 .5rem 1.5rem rgba(0, 0, 0, .18); } /* A process list: the label, what it says, how far, how long, and Kill. The * digits line up so the one line at 5:20 among lines at 0:0x is the one to * look at. Queued lines are faint; they are not being run. */ .line, .run { display: flex; align-items: center; gap: 8px; padding: 4px 6px; } .line + .line, .run + .run { border-top: 1px dashed color-mix(in srgb, var(--desk-jobs-fg, CanvasText) 15%, transparent); } .line.queued, .line.waiting { color: var(--desk-jobs-dim, GrayText); } .line .what, .run .what { font-weight: 600; white-space: nowrap; } .line.queued .what { font-weight: 400; } .said { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--desk-jobs-dim, GrayText); } .pct, .age { font-variant-numeric: tabular-nums; white-space: nowrap; color: var(--desk-jobs-dim, GrayText); } .age { min-width: 2.6em; text-align: right; } /* A job asleep on a retry stamp is queued and would read as next in line; * the countdown says a wait was imposed and how much of it is left. */ .due { font-variant-numeric: tabular-nums; white-space: nowrap; color: var(--desk-jobs-warn, #b26b00); } .due:empty { display: none; } .heading { padding: 8px 6px 2px; font-size: 11px; font-weight: 600; color: var(--desk-jobs-dim, GrayText); } .idle, .offline { padding: 6px; color: var(--desk-jobs-dim, GrayText); } .offline { color: var(--desk-jobs-error, #d33); } .all { display: block; padding: 8px 6px 4px; color: var(--desk-jobs-accent, LinkText); } .pop button { flex: none; font: inherit; color: var(--desk-jobs-dim, GrayText); background: none; border: 1px solid currentColor; border-radius: 6px; padding: 1px 8px; cursor: pointer; } .pop button:hover:not([disabled]) { color: var(--desk-jobs-error, #d33); } .pop button[disabled] { opacity: .4; cursor: default; } @media (pointer: coarse) { .chip { min-height: 2.5rem; } .pop button { min-height: 2.5rem; padding: 4px 12px; } } /* A phone: the bar has no room for a sentence, so the chip is the dot and * the count, and the popover spans the screen under the bar. Its top is set * from the chip's position when it opens. */ @media (max-width: 40rem) { .chip .what { display: none; } .n.one { display: inline; } .pop { position: fixed; top: var(--desk-queue-top, 3.5rem); left: .5rem; right: .5rem; min-width: 0; max-width: none; } } `; /** How long, as a player writes it: 0:04, 5:20, 1:02:07. */ function clock(seconds) { const total = Math.max(0, Math.round(seconds || 0)); const h = Math.floor(total / 3600), m = Math.floor((total % 3600) / 60), s = total % 60; const mm = h ? String(m).padStart(2, "0") : String(m); return (h ? `${h}:` : "") + `${mm}:${String(s).padStart(2, "0")}`; } const age = (stamp) => stamp ? clock((Date.now() - new Date(stamp)) / 1000) : ""; /** The other way round: how long until a stamp, for a retry's next attempt. */ const due = (stamp) => stamp ? `due in ${clock((new Date(stamp) - Date.now()) / 1000)}` : ""; /** A running job with nothing left to count: the bar reaches its total well * before the handler returns, and "12 of 12" for a minute reads as stuck. */ const finishing = (job) => job.state === "running" && job.total > 0 && job.done >= job.total; const progress = (job) => !job.total ? "" : finishing(job) ? "finishing" : `${job.done} of ${job.total}`; /** Keep the rows, write the numbers into them. The snapshot arrives four * times a second while anything runs, and a list rebuilt that often takes * the Kill button out from under the pointer on its way to it. Keyed by the * server's id, so "the same row" means the same job. */ function sync(parent, entries, make, fill) { const had = new Map(); for (const el of parent.children) had.set(el.dataset.key, el); let last = null; for (const [key, item] of entries) { let el = had.get(String(key)); if (el) had.delete(String(key)); else { el = make(item); el.dataset.key = String(key); } fill(el, item); const at = last ? last.nextElementSibling : parent.firstElementChild; if (el !== at) parent.insertBefore(el, at); last = el; } for (const el of had.values()) el.remove(); } function span(className) { const el = document.createElement("span"); el.className = className; return el; } const tally = (t) => t.done + t.failed + t.cancelled + t.running + t.queued; class DeskQueue extends Follower { #open = false; #ticker = null; #stopping = new Set(); #chip; #pop; static get observedAttributes() { return ["href", "idle"]; } constructor() { super(QUEUE_CSS); const host = this.root.lastElementChild; this.#chip = document.createElement("button"); this.#chip.type = "button"; this.#chip.className = "chip"; this.#chip.setAttribute("aria-haspopup", "true"); this.#chip.setAttribute("aria-expanded", "false"); this.#chip.append(span("dot"), span("n"), span("what")); this.#chip.addEventListener("click", () => this.#toggle()); this.#pop = document.createElement("div"); this.#pop.className = "pop"; this.#pop.hidden = true; const offline = document.createElement("div"); offline.className = "offline"; offline.textContent = "Not connected — this may be out of date."; const lines = document.createElement("div"); lines.className = "lines"; const heading = document.createElement("div"); heading.className = "heading"; heading.textContent = "Runs"; const runs = document.createElement("div"); runs.className = "runs"; const all = document.createElement("a"); all.className = "all"; const slot = document.createElement("slot"); slot.textContent = "All runs"; all.append(slot); this.#pop.append(offline, lines, heading, runs, all); host.append(this.#chip, this.#pop); } get raisesNotices() { return false; } connectedCallback() { super.connectedCallback(); document.addEventListener("click", this.#outside); document.addEventListener("keydown", this.#escape); } disconnectedCallback() { super.disconnectedCallback(); document.removeEventListener("click", this.#outside); document.removeEventListener("keydown", this.#escape); clearInterval(this.#ticker); this.#ticker = null; } attributeChangedCallback() { this.render(); } // Arrow functions, so that the same function is added and removed. #outside = (e) => { if (this.#open && !e.composedPath().includes(this)) this.#toggle(false); }; #escape = (e) => { if (e.key !== "Escape" || !this.#open) return; this.#toggle(false); this.#chip.focus(); }; #toggle(open = !this.#open) { this.#open = open; if (open) { this.style.setProperty("--desk-queue-top", `${this.#chip.getBoundingClientRect().bottom + 6}px`); } this.render(); } async #stop(id) { this.#stopping.add(id); this.render(); try { await fetch(`${this.base}/api/cascades/${id}/cancel`, { method: "POST", credentials: "same-origin" }); } finally { this.#stopping.delete(id); } } render() { if (!this.#chip) return; // an attribute, before the constructor ran const { running = [], queued = [] } = this.state; const idle = !running.length && !queued.length; this.hidden = this.getAttribute("idle") === "hide" && idle && !this.offline; const chip = this.#chip; chip.classList.toggle("busy", running.length > 0); chip.classList.toggle("offline", this.offline); const n = chip.querySelector(".n"); n.textContent = running.length || ""; n.classList.toggle("one", running.length === 1); const first = running[0]; const parts = first ? [first.label || first.kind, progress(first)] : []; if (queued.length) parts.push(first ? `+${queued.length} waiting` : `${queued.length} waiting`); // Said in a word as well as a colour: a grey dot alone reads as a switch. if (!parts.length) parts.push(this.offline ? "Offline" : "Idle"); chip.querySelector(".what").textContent = parts.filter(Boolean).join(", "); chip.title = this.offline ? "Not connected" : first ? [first.label || first.kind, first.message].filter(Boolean).join(": ") : queued.length ? `${queued.length} waiting` : "Nothing running"; chip.setAttribute("aria-expanded", String(this.#open)); this.#pop.hidden = !this.#open; if (!this.#open) { clearInterval(this.#ticker); this.#ticker = null; return; } this.#drawPop(running, queued); // The ages are the one column no push carries: a job that has said // nothing for five minutes is the one whose age you want. A retry's // countdown is the same, the other way round. this.#ticker ??= setInterval(() => { for (const el of this.#pop.querySelectorAll(".age")) el.textContent = age(el.dataset.since); for (const el of this.#pop.querySelectorAll(".due")) el.textContent = due(el.dataset.until); }, 1000); } #drawPop(running, queued) { const pop = this.#pop; pop.querySelector(".offline").hidden = !this.offline; const jobs = [...running, ...queued]; sync(pop.querySelector(".lines"), jobs.length ? jobs.map((j) => [j.id, j]) : [["idle", null]], (j) => j ? this.#line() : Object.assign(document.createElement("div"), { className: "idle", textContent: "Nothing running." }), (el, j) => { if (j) this.#fillLine(el, j); }); // A cascade of one job is that job, and its Kill already says Stop. const live = (this.state.cascades || []).filter((c) => c.tasks.some((t) => t.running || t.queued) && !(c.tasks.length === 1 && tally(c.tasks[0]) <= 1)); pop.querySelector(".heading").hidden = !live.length; sync(pop.querySelector(".runs"), live.map((c) => [c.id, c]), () => this.#run(), (el, c) => this.#fillRun(el, c)); const all = pop.querySelector(".all"); const href = this.getAttribute("href"); all.hidden = !href; if (href) all.href = href; } #line() { const el = document.createElement("div"); el.className = "line"; const kill = document.createElement("button"); kill.type = "button"; kill.textContent = "Kill"; kill.addEventListener("click", () => this.kill(Number(el.dataset.key))); el.append(span("what"), span("said"), span("pct"), span("due"), span("age"), kill); return el; } #fillLine(el, job) { // Running: since it started. Queued: since it was asked for, which is how // long it has waited behind the others. const running = job.state === "running"; const since = running ? job.started_at : job.created_at; el.className = `line ${job.state}${job.waiting ? " waiting" : ""}`; el.title = `${running ? "running" : "waiting"} since ${since || "?"}` + (job.waiting && job.error ? ` — last try: ${job.error}` : ""); el.querySelector(".what").textContent = job.label || job.kind; el.querySelector(".said").textContent = job.message || ""; el.querySelector(".pct").textContent = progress(job); const when = el.querySelector(".age"); when.dataset.since = since || ""; when.textContent = age(since); const until = el.querySelector(".due"); until.dataset.until = job.waiting ? job.run_after : ""; until.textContent = due(until.dataset.until); el.querySelector("button").disabled = this.killing.has(job.id) || !!job.cancel_requested; } #run() { const el = document.createElement("div"); el.className = "run"; const stop = document.createElement("button"); stop.type = "button"; stop.textContent = "Stop run"; stop.addEventListener("click", () => this.#stop(Number(el.dataset.key))); el.append(span("what"), span("said"), stop); return el; } #fillRun(el, cascade) { const tasks = cascade.tasks; const done = tasks.reduce((sum, t) => sum + t.done, 0); const total = tasks.reduce((sum, t) => sum + tally(t), 0); const now = tasks.find((t) => t.running || t.queued); el.querySelector(".what").textContent = cascade.name || `run ${cascade.id}`; el.querySelector(".said").textContent = [now && (now.label || now.name), `${done} of ${total} done`].filter(Boolean).join(", "); el.querySelector("button").disabled = this.#stopping.has(cascade.id); } } if (!customElements.get("desk-jobs")) { customElements.define("desk-jobs", DeskJobs); } if (!customElements.get("desk-queue")) { customElements.define("desk-queue", DeskQueue); } export { DeskJobs, DeskQueue };