Files
jobboard/app/static/live.js
T
WolfandClaude Opus 5.5 59a301c351 Wire the app like the boilerplate: door, queue, schedules, eight tabs
app/main.py assembles it in the boilerplate's order, auth.connect first,
then the queue with the scheduler and the pages. TABS is Roles,
Applications, Agents, Companies, Jobs, Schedules, Messages and Account:
the alpha page's three views become templates on the boilerplate's
base.html, driven by static/roles.js (the old app.js) through api() and
toasts.js, over the unchanged JSON routes. Companies lists every board with
its open roles and the affinity control. A 422 is one sentence, so a toast
never reads [object Object].

Config takes the boilerplate's shape: APP_CONFIG or config.yaml, a missing
file is the defaults, an unknown key is an error. roles.yaml is gone;
config.yaml is the committed default and carries the starter agents.

pull is now a job that asks for the pull cascade, because a schedule
enqueues a job kind; its default schedule, every 6h, is declared beside it
in app/jobs/cascade.py. pull_batch's targets are keyword-only so the Jobs
tab offers no batch button. `app worker` is the boilerplate's; `app job`
still drains a queue of its own and fires no schedules.

test_coupling holds pipeline, auth and desk apart (desk may import
pipeline) and plugins to desk; test_architecture keeps app's layers, now
with main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 12:48:12 +02:00

85 lines
3.4 KiB
JavaScript

/* The page's one connection to the server: the queue's socket.
*
* `/ws/jobs` sends two kinds of message, and this turns each into a document
* event so that no other script opens a socket of its own — five tabs each
* holding their own would be five reconnect loops to get subtly wrong:
*
* app:jobs the whole queue: {running, queued, recent, ...}. Sent on
* connect and after every change. `window.live.jobs` holds the
* latest, for a script that loads after a message.
* app:notice one notice from the log (app/notices.py), its count
* included. toasts.js shows it.
* app:schedules the schedule list, whenever one fires, is skipped or is
* edited. `<desk-schedules>` holds its own socket and does not
* need this; it is here for a page that wants to say when the
* next run is without drawing the whole table.
*
* Nothing here dispatches by hand: the name is taken from the message, so a
* fourth type is a listener somewhere and no edit to this file.
*
* Reconnects with backoff, because the server restarting is the normal case
* and a page that needs reloading after every deploy is a page people reload
* by habit and then trust less.
*/
(function () {
"use strict";
const live = { jobs: null, connected: false };
window.live = live;
let backoff = 1000;
function connect() {
const proto = location.protocol === "https:" ? "wss:" : "ws:";
const socket = new WebSocket(`${proto}//${location.host}/ws/jobs`);
socket.onopen = () => {
backoff = 1000;
live.connected = true;
};
socket.onmessage = (ev) => {
let msg;
try { msg = JSON.parse(ev.data); } catch (e) { return; }
const { type, ...body } = msg;
if (!type) return;
if (type === "jobs") live.jobs = body;
document.dispatchEvent(new CustomEvent(`app:${type}`, { detail: body }));
};
socket.onclose = () => {
live.connected = false;
setTimeout(async () => {
if (await signedOut()) return;
connect();
}, backoff);
backoff = Math.min(backoff * 2, 30000);
};
}
/* A refused handshake and a dead server look identical from here: the
middleware in `auth/` closes the socket before accepting it, which the
server turns into an HTTP 403 on the upgrade, and the browser reports the
same abnormal close it reports for "the server is gone". So one cheap
request tells them apart, and an expired session sends the page to the
door instead of leaving it reconnecting quietly against a login form for
the rest of the afternoon. `<desk-jobs>` has the same trick as its
`probe` attribute; it is done here so that it is done on every tab. */
async function signedOut() {
let answer;
try {
answer = await fetch("/api/jobs", { headers: { Accept: "application/json" } });
} catch (e) {
return false; // offline: keep the backoff going
}
if (answer.status !== 401 && answer.status !== 403) return false;
const next = location.pathname + location.search;
location.assign(`/login?next=${encodeURIComponent(next)}`);
return true;
}
document.addEventListener("DOMContentLoaded", () => {
// The login page has no session for the socket to use, and asking would
// only be a 401 every second while somebody types a password.
if (location.pathname !== "/login") connect();
});
})();