feat(server): scheduled jobs on the server and a Scheduled tab in the admin panel
Owner review 2, R10: nothing may run out of sight, so every timer the server ran becomes a system schedule the admin panel shows and manages, and administrators create their own. - SchedulerHost runs the shared Scheduler on the job engine (started after JobEngineHost, stopped before it); ServerScheduleHost gives it the container's time zone (UTC with a warning when TZ is unknown), the archive refreshed from archive.json at every run (what ArchivePolling did), the clean-up steps and the system schedules: archive-scan every 10 minutes (no catch-up, also run once at start like the polling), upload-cleanup every 30 minutes, index-backup at 03:00 (SQLite only), housekeeping (job history) at 04:00 and audit-cleanup at 04:30. - ArchivePolling and DatabaseBackupService are gone; UploadMaintenance only recovers commits at start, its sweep is the "uploads" clean-up step. - /api/v1/schedules (ManageArchive, audited): list with actions and time zone, create, PUT/PATCH, delete (409 schedule_system), run now (409 schedule_pending), reset (409 schedule_no_defaults), runs paged by cursor; invalid fields are 400 invalid_schedule naming the field. - CHRONOS_AUDIT_RETENTION_DAYS (AuditRetentionDays, default 365) seeds the audit clean-up; set by the environment it is applied at every start and locked. Job history leaves Settings: it is the housekeeping schedule's age limit, and the setting is what a reset restores. - Admin panel: Jobs has Queue and Scheduled tabs; schedules with when (in the panel's language and the server's zone), next run, last result, a switch, Run now, Edit, Delete or Reset, their runs linking to the jobs, and a side panel for the action, recurrence and parameters; job rows a schedule queued carry a Scheduled pill. Strings in en and it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 808ce94ae29e3bf4522cb4d40dfd76d4f8529c64)
This commit is contained in:
1 parent
e5508e7e69
commit
75daab7275
66 files changed
+1985
-290
No files matched your search
@@ -1,18 +1,60 @@
|
||||
// Jobs (BRIEF §14.8): queue and history with cancel/pause/resume/retry/priority, log tails, render nodes.
|
||||
// Jobs (BRIEF §14.8): two tabs in the header. Queue: queue and history with cancel/pause/resume/retry/priority, log tails,
|
||||
// render nodes; #/jobs?job=<id> opens that job's log. Scheduled (#/jobs?tab=schedules, owner review 2, R10): the server's
|
||||
// schedules, its system ones included (schedules.js).
|
||||
import { t, plural, applyStrings } from "../i18n.js";
|
||||
import { escapeHtml, attr, formatTime, formatDateTime, toast, errorText, confirmDialog, emptyState, redraw, fitColumns } from "../ui.js";
|
||||
import { escapeHtml, attr, formatTime, formatDateTime, toast, errorText, confirmDialog, emptyState, redraw, fitColumns, routeParams } from "../ui.js";
|
||||
import { kindLabel, nodeLabel, progressCell, stateCell, stateLabel, encoders, logText } from "../jobs.js";
|
||||
import { renderSchedules } from "../schedules.js";
|
||||
|
||||
const TABS = ["queue", "schedules"];
|
||||
const FILTERS = ["all", "running", "queued", "paused", "failed", "done", "cancelled"];
|
||||
const PRIORITIES = ["background", "normal", "user"];
|
||||
|
||||
export async function render(root, { api, setMeta, onEvent }) {
|
||||
export async function render(root, context) {
|
||||
const { actions, onEvent } = context;
|
||||
let tab = TABS.includes(routeParams().get("tab")) ? routeParams().get("tab") : "queue";
|
||||
// The open tab's event handler: the page subscribes once, a tab that was left hears nothing more.
|
||||
let handler = null;
|
||||
onEvent((e) => handler?.(e));
|
||||
|
||||
actions.innerHTML = `<div class="segmented" role="group" aria-label="${attr(t("Admin.Nav.Jobs"))}">${TABS.map((name) =>
|
||||
`<button class="btn" data-tab="${name}" aria-pressed="${name === tab}" data-i18n="Admin.Jobs.Tab.${cap(name)}" data-i18n-title="Admin.Jobs.Tab.${cap(name)}.Tip"></button>`).join("")}</div>
|
||||
<span class="actions" id="tab-actions"></span>`;
|
||||
applyStrings(actions);
|
||||
const tabActions = actions.querySelector("#tab-actions");
|
||||
actions.querySelectorAll("[data-tab]").forEach((b) => b.addEventListener("click", () => {
|
||||
if (b.dataset.tab === tab) return;
|
||||
tab = b.dataset.tab;
|
||||
actions.querySelectorAll("[data-tab]").forEach((x) => x.setAttribute("aria-pressed", String(x === b)));
|
||||
history.replaceState(null, "", tab === "queue" ? "#/jobs" : "#/jobs?tab=schedules");
|
||||
show();
|
||||
}));
|
||||
|
||||
async function show() {
|
||||
handler = null;
|
||||
tabActions.innerHTML = "";
|
||||
// Each tab draws into a body of its own, so a late answer of the tab that was left never touches the new one.
|
||||
root.innerHTML = `<div class="tab-body"></div>`;
|
||||
const body = root.firstElementChild;
|
||||
const scoped = { ...context, actions: tabActions, onEvent: (fn) => { handler = fn; } };
|
||||
if (tab === "queue") await renderQueue(body, scoped, routeParams().get("job"));
|
||||
else await renderSchedules(body, scoped, routeParams().get("schedule"));
|
||||
}
|
||||
|
||||
await show();
|
||||
}
|
||||
|
||||
/** The Queue tab; <paramref name="focusJob"/> (a job id from a schedule's runs) is shown first with its log open. */
|
||||
async function renderQueue(root, { api, setMeta, onEvent }, focusJob) {
|
||||
let filter = "all";
|
||||
let jobs = [];
|
||||
let nodes = [];
|
||||
let running = 0;
|
||||
let cursor = null;
|
||||
const expanded = new Set();
|
||||
// The job a schedule's run links to stays first while the list does not hold it; it gets the focus once.
|
||||
const pinId = focusJob;
|
||||
let pinned = null;
|
||||
const expanded = new Set(pinId ? [pinId] : []);
|
||||
let timer = null;
|
||||
|
||||
root.innerHTML = `
|
||||
@@ -47,6 +89,7 @@ export async function render(root, { api, setMeta, onEvent }) {
|
||||
}
|
||||
|
||||
async function load(more = false) {
|
||||
if (!root.isConnected) return;
|
||||
try {
|
||||
const state = filter === "all" ? "" : `&state=${filter}`;
|
||||
const [page, list, active] = await Promise.all([
|
||||
@@ -59,20 +102,29 @@ export async function render(root, { api, setMeta, onEvent }) {
|
||||
cursor = page.nextCursor;
|
||||
nodes = list;
|
||||
running = active.items.length;
|
||||
pinned = pinId && !jobs.some((j) => j.id === pinId) ? await api.get(`/api/v1/jobs/${pinId}`).catch(() => null) : null;
|
||||
} catch (err) {
|
||||
toast(errorText(err), true);
|
||||
return;
|
||||
}
|
||||
if (!root.isConnected) return;
|
||||
setMeta(`${plural("Admin.Jobs.RunningCount", running)} · ${plural("Admin.Jobs.NodesOnline", nodes.filter((n) => n.state !== "offline").length)}`);
|
||||
draw();
|
||||
if (focusJob) {
|
||||
root.querySelector(`[data-toggle="${CSS.escape(focusJob)}"]`)?.focus();
|
||||
root.querySelector(`[data-key="${CSS.escape(focusJob)}"]`)?.scrollIntoView({ block: "center" });
|
||||
focusJob = null;
|
||||
}
|
||||
}
|
||||
|
||||
function draw() {
|
||||
const host = root.querySelector("#rows");
|
||||
const shown = pinned ? [pinned, ...jobs] : jobs;
|
||||
redraw(host, () => {
|
||||
host.innerHTML = jobs.map((j) => `
|
||||
host.innerHTML = shown.map((j) => `
|
||||
<div class="row" role="row" data-key="${attr(j.id)}">
|
||||
<span role="cell"><button class="link ellipsis" data-toggle="${attr(j.id)}" aria-expanded="${expanded.has(j.id)}" aria-controls="log-${attr(j.id)}" data-i18n-title="Admin.Jobs.Log.Tip">${escapeHtml(kindLabel(j.kind))}</button></span>
|
||||
<span class="name-cell" role="cell"><button class="link ellipsis" data-toggle="${attr(j.id)}" aria-expanded="${expanded.has(j.id)}" aria-controls="log-${attr(j.id)}" data-i18n-title="Admin.Jobs.Log.Tip">${escapeHtml(kindLabel(j.kind))}</button>${j.scheduleId
|
||||
? `<a class="pill" href="#/jobs?tab=schedules&schedule=${attr(j.scheduleId)}" data-i18n="Admin.Jobs.Scheduled" data-i18n-title="Admin.Jobs.Scheduled.Tip"></a>` : ""}</span>
|
||||
<span class="ellipsis" role="cell" dir="auto" title="${attr(j.targetLabel ?? "")}">${escapeHtml(j.targetLabel ?? "")}</span>
|
||||
<span class="col-node ellipsis" role="cell" dir="auto">${escapeHtml(nodeLabel(j, nodes))}</span>
|
||||
<span class="progress" role="cell">${progressCell(j)}</span>
|
||||
@@ -175,7 +227,7 @@ export async function render(root, { api, setMeta, onEvent }) {
|
||||
|
||||
onEvent((e) => {
|
||||
if (e.type === "jobProgress") {
|
||||
const job = jobs.find((j) => j.id === e.jobId);
|
||||
const job = jobs.find((j) => j.id === e.jobId) ?? (pinned?.id === e.jobId ? pinned : null);
|
||||
if (job) {
|
||||
job.progressDone = e.done;
|
||||
job.progressTotal = e.total;
|
||||
|
||||
Reference in new issue
Block a user