/* 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. `` 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. `` 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(); }); })();