fix(server): archive.json on its own timer, a live Scheduled tab in one time zone, schedule audit in words
- SchedulerHost reads archive.json every 10 minutes on its own timer. The archive-scan runs that used to do it are skipped while a long scan is pending, and the schedule may be off or daily, so uploads kept a desktop's old folder template or proxy size. - Every schedule change and run, a skipped run included, is published as a scheduleChanged event. The Scheduled tab reloads on it, so skipped runs, next-run moves and other administrators' edits show without waiting for a job. - The Scheduled tab still draws when the schedule a job links to, or an expanded one, no longer exists: its runs are dropped instead of failing the whole load. - Every time on the Scheduled tab (next run, last result, runs) is in the server's time zone, as its "Times in" line says. - Audit details name a system schedule after its key (system:<key> · name · ...). The dashboard and the Logs page show it by its localized name and the recurrence code in words, as the Scheduled tab does. Older entries keep their recorded name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit c20f573d0bdf1cdcf3a86e9702ac76a4bf9508d9)
This commit is contained in:
1 parent
e384564784
commit
3f0e6357b1
10 files changed
+339
-43
No files matched your search
@@ -87,7 +87,7 @@ Dependencies point one way: `Core` ← `Core.Windows`, `Data`, `Protocol` ← `S
|
||||
| `Ui/Visuals` | `DrawingElement` (hover readout, item keyboard and activation) and its charts (`BarChart`, `SeriesChart`, `StackedBar`, `HeatmapStrip`, `TimelineBands`), `Filmstrip`, `PreviewViewport`, `CurveEditor`, `SequenceCard`, `VirtualizingWrapPanel`, `SegmentedControl`, `SplitButton` |
|
||||
| `Server/Api`, `Server/Events` | Endpoint groups (`XxxEndpoints.Map`; `ScheduleEndpoints`: `/api/v1/schedules`), API errors, the WebSocket `EventHub` |
|
||||
| `Server/Security` | Accounts, password hashing, tokens, session cookies, login guard, access rules, audit log |
|
||||
| `Server/Storage`, `Server/Jobs` | Archive and processing modules, uploads (`UploadMaintenance`: recovery at start, and the sweep that is the `uploads` clean-up step), render store; `JobEngineHost`, `SchedulerHost` (the `Scheduler` on the engine, started after it) and `ServerScheduleHost` (time zone, archive refreshed from archive.json, system schedules), render node service, monitor and availability |
|
||||
| `Server/Storage`, `Server/Jobs` | Archive and processing modules, uploads (`UploadMaintenance`: recovery at start, and the sweep that is the `uploads` clean-up step), render store; `JobEngineHost`, `SchedulerHost` (the `Scheduler` on the engine, started after it; archive.json read every 10 minutes whatever the scan schedule does; every schedule change and run published as a `scheduleChanged` event) and `ServerScheduleHost` (time zone, archive refreshed from archive.json, system schedules), render node service, monitor and availability |
|
||||
| `Server/Hosting`, `Server/Admin/web` | `ServerHost`, `ServerLogging` (console and file levels the logging filters read at every line, the file sink's limits, the engine's level; applied again when Settings › Logs is saved), certificates, discovery beacon, embedded static files, Windows service (start and stop failures in the Application event log); the admin panel (ES modules, one per page; `schedules.js` is the Jobs page's Scheduled tab) |
|
||||
|
||||
### 2.5 Hosts
|
||||
@@ -95,7 +95,7 @@ Dependencies point one way: `Core` ← `Core.Windows`, `Data`, `Protocol` ← `S
|
||||
| Host | Composition root | What it runs |
|
||||
|---|---|---|
|
||||
| Chronos (desktop) | `App/Infrastructure/AppServices` | SQLite index in `%LOCALAPPDATA%\Chronos` (or `.\data` when portable, unless the copy is in a OneDrive folder); `ArchiveService` with WIC first, then managed decoders; `ProcessingService` with Media Foundation, then managed encoders; one `JobEngine` with the handlers of both; thumbnails; `ServerSession`; `SyncHost` while signed in (sync engine and agent, upload and download handlers, metered hold, editors of server sequences); the render node while opted in and signed in; update check and download (`About/UpdateCheck`, `About/UpdateDownload`, `About/UpdateFlow`: the setup, or the portable zip for a portable copy, verified against `SHA256SUMS`, resumable, cancellable, failing after 60 s without data); diagnostics bundle; the `Scheduler` on `DesktopScheduleHost`, started once the archive was reopened (an unplugged one is looked for every minute), which then runs the archive scan once (again when an unplugged archive is back), and whose system schedules replaced the rescan timer, the daily backup loop and the job-history purge (scans follow Settings › Analysis › After a scan at every run); the restore staged in Settings › Advanced (`DatabaseBackups`) |
|
||||
| Chronos Server | `Server/Hosting/ServerHost.Build` | Options from `server.json` + `CHRONOS_*`; PostgreSQL (or SQLite on Windows); `ArchiveModule` then `ProcessingModule` register job handler providers; `JobEngineHost` (renders only managed formats and MP4 jobs no render node can take, BRIEF §14.7); `RenderNodeService`/`RenderNodeMonitor`; `UploadService`; `VolumeSpace` (free space of the archive, the space path and `/data`, file system and mount from `/proc/self/mountinfo`; tests replace it); `EventHub` (WebSocket); embedded admin panel; LAN discovery beacon; Windows service host; `SchedulerHost` with the system schedules `archive-scan` (every 10 minutes, also once at start), `upload-cleanup` (every 6 hours), `index-backup` (SQLite only, 03:00), `housekeeping` (job history, 04:00) and `audit-cleanup` (04:30; Settings › Logs shows and sets its age limit, 0 while it is off; seeded and reset from `AuditRetentionDays`, 0 by default: off, the audit log kept; `CHRONOS_AUDIT_RETENTION_DAYS` fixes it before the first pass), no other timer queues work |
|
||||
| Chronos Server | `Server/Hosting/ServerHost.Build` | Options from `server.json` + `CHRONOS_*`; PostgreSQL (or SQLite on Windows); `ArchiveModule` then `ProcessingModule` register job handler providers; `JobEngineHost` (renders only managed formats and MP4 jobs no render node can take, BRIEF §14.7); `RenderNodeService`/`RenderNodeMonitor`; `UploadService`; `VolumeSpace` (free space of the archive, the space path and `/data`, file system and mount from `/proc/self/mountinfo`; tests replace it); `EventHub` (WebSocket); embedded admin panel; LAN discovery beacon; Windows service host; `SchedulerHost` with the system schedules `archive-scan` (every 10 minutes, also once at start), `upload-cleanup` (every 6 hours), `index-backup` (SQLite only, 03:00), `housekeeping` (job history, 04:00) and `audit-cleanup` (04:30; Settings › Logs shows and sets its age limit, 0 while it is off; seeded and reset from `AuditRetentionDays`, 0 by default: off, the audit log kept; `CHRONOS_AUDIT_RETENTION_DAYS` fixes it before the first pass), no other timer queues work (a 10-minute timer only reads archive.json again) |
|
||||
| Chronos Uploader | `Uploader/App.xaml.cs` | `ChronosClient`, persisted queue, watch folders; no database |
|
||||
| Chronos Setup | `Setup/Program` | TPK1 payload extraction, registry, shortcuts, file types named in setup's language, silent mode, uninstaller; only what starts a program its log or payload lists is changed: Windows services of those programs stopped once the programs have closed, started again (also after a failure) or deleted through the Service Control Manager (an elevated `/SERVICES … /NAMES` step when needed), sign-in entries of those programs moved to the program an upgrade renamed or removed, shared shortcuts handed to the product that keeps the program; the Server only for all users inside Program Files; "Remove my data" limited to the programs no other installed product uses |
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Chronos.Protocol;
|
||||
|
||||
/// <summary>
|
||||
/// Messages pushed on <c>WS /api/v1/events</c>. Each frame is one JSON object with a <c>type</c> discriminator:
|
||||
/// jobProgress, jobState, sequenceChanged, cardEvent, nodeState.
|
||||
/// jobProgress, jobState, sequenceChanged, cardEvent, nodeState, scheduleChanged.
|
||||
/// </summary>
|
||||
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
|
||||
[JsonDerivedType(typeof(JobProgressEvent), "jobProgress")]
|
||||
@@ -12,6 +12,7 @@ namespace Chronos.Protocol;
|
||||
[JsonDerivedType(typeof(SequenceChangedEvent), "sequenceChanged")]
|
||||
[JsonDerivedType(typeof(CardEvent), "cardEvent")]
|
||||
[JsonDerivedType(typeof(NodeStateEvent), "nodeState")]
|
||||
[JsonDerivedType(typeof(ScheduleChangedEvent), "scheduleChanged")]
|
||||
public abstract record ServerEvent;
|
||||
|
||||
public sealed record JobProgressEvent(Guid JobId, long Done, long Total, double? ItemsPerSecond, double? EtaSeconds) : ServerEvent;
|
||||
@@ -41,3 +42,9 @@ public static class CardImportStates
|
||||
}
|
||||
|
||||
public sealed record NodeStateEvent(Guid NodeId, string ClientName, string State) : ServerEvent;
|
||||
|
||||
/// <summary>
|
||||
/// A schedule of the server was created, edited, reset, deleted (<see cref="Deleted"/>) or ran, a run that queued nothing
|
||||
/// included, or its next run moved (owner review 2, R10): the admin panel's Scheduled tab reads the list again.
|
||||
/// </summary>
|
||||
public sealed record ScheduleChangedEvent(Guid ScheduleId, bool Deleted) : ServerEvent;
|
||||
@@ -1,9 +1,12 @@
|
||||
// Audit entries as the dashboard and the Logs page show them: server codes (actions, job kinds, roles, changed
|
||||
// fields) in words; names, titles and templates stay as recorded.
|
||||
// fields, schedule recurrences) in words, system schedules by their names in the panel's language; other names, titles
|
||||
// and templates stay as recorded.
|
||||
import { t, has, plural } from "./i18n.js";
|
||||
import { formatBytes } from "./ui.js";
|
||||
import { kindLabel, pascal } from "./jobs.js";
|
||||
import { actionLabel as scheduleAction, outcomeLabel } from "./schedules.js";
|
||||
import { actionLabel as scheduleAction, outcomeLabel, scheduleName, recurrenceText, parseRecurrence } from "./schedules.js";
|
||||
|
||||
const SYSTEM = "system:";
|
||||
|
||||
export function actionLabel(action) {
|
||||
const key = `Admin.Logs.Action.${pascal(action)}`;
|
||||
@@ -37,16 +40,26 @@ export function detailText(entry) {
|
||||
case "sequence_updated":
|
||||
return details.split(", ").map(changeLabel).join(", ");
|
||||
case "schedule_run": {
|
||||
const [name, outcome] = splitLast(details);
|
||||
return outcome === undefined ? details : `${name} · ${outcomeLabel(outcome)}`;
|
||||
// "[system:<key> · ]name · outcome"
|
||||
const parts = details.split(" · ");
|
||||
if (parts.length < 2) return details;
|
||||
const outcome = parts.pop();
|
||||
return `${scheduleNameIn(parts)} · ${outcomeLabel(outcome)}`;
|
||||
}
|
||||
case "schedule_created":
|
||||
case "schedule_updated":
|
||||
case "schedule_deleted":
|
||||
case "schedule_reset": {
|
||||
// "name · action · recurrence[ · off]": the action in words, the name as typed (system schedules: as stored).
|
||||
const [name, action, ...rest] = details.split(" · ");
|
||||
return action === undefined ? details : [name, scheduleAction(action), ...rest.map((r) => r === "off" ? t("Admin.Schedules.Off") : r)].join(" · ");
|
||||
// "[system:<key> · ]name · action · recurrence[ · off]", read from the end: a name may hold the separator.
|
||||
const parts = details.split(" · ");
|
||||
const off = parts.at(-1) === "off";
|
||||
if (off) parts.pop();
|
||||
if (parts.length < 3) return details;
|
||||
const code = parts.pop();
|
||||
const action = parts.pop();
|
||||
const recurrence = parseRecurrence(code);
|
||||
const shown = [scheduleNameIn(parts), scheduleAction(action), recurrence ? recurrenceText(recurrence) : code];
|
||||
return (off ? [...shown, t("Admin.Schedules.Off")] : shown).join(" · ");
|
||||
}
|
||||
default:
|
||||
return details;
|
||||
@@ -66,6 +79,15 @@ function changeLabel(change) {
|
||||
return has(key) ? t(key) : change;
|
||||
}
|
||||
|
||||
/**
|
||||
* A schedule's name from the leading parts of audit details: after "system:<key>" a system schedule's in the panel's
|
||||
* language, as the Scheduled tab names it; else (a user's, or one recorded before keys were) as recorded.
|
||||
*/
|
||||
function scheduleNameIn(parts) {
|
||||
const systemKey = parts.length > 1 && parts[0].startsWith(SYSTEM) ? parts[0].slice(SYSTEM.length) : null;
|
||||
return scheduleName({ systemKey, name: (systemKey ? parts.slice(1) : parts).join(" · ") });
|
||||
}
|
||||
|
||||
function splitLast(details) {
|
||||
const at = details.lastIndexOf(" · ");
|
||||
return at < 0 ? [details] : [details.slice(0, at), details.slice(at + 3)];
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
// Scheduled jobs (owner review 2, R10): the Scheduled tab of the Jobs page (#/jobs?tab=schedules). Every schedule of the
|
||||
// server, its system schedules first (the timers it runs, shown so nothing runs out of sight), with when it runs in the
|
||||
// server's time zone, its next run, its last result and a switch; Run now, Edit, Delete (user schedules) and Reset
|
||||
// server, its system schedules first (the timers it runs, shown so nothing runs out of sight), with when it runs, its next
|
||||
// run, its last result (every time in the server's time zone) and a switch; Run now, Edit, Delete (user schedules) and Reset
|
||||
// (system schedules). A schedule's name opens its last runs, each linking to its job on the Queue tab. A side panel
|
||||
// creates and edits schedules: the action (those the server cannot run are disabled, their reason in the tooltip), the
|
||||
// recurrence, the action's parameters, priority, catch-up and the switch.
|
||||
import { t, plural, has, applyStrings, culture } from "./i18n.js";
|
||||
import { escapeHtml, attr, formatNumber, formatTime, formatDateTime, toast, errorText, confirmDialog, emptyState, redraw, focusPlace, restoreFocus, fitColumns,
|
||||
import { escapeHtml, attr, formatNumber, toast, errorText, confirmDialog, emptyState, redraw, focusPlace, restoreFocus, fitColumns,
|
||||
markInvalid, clearInvalid, invalidText } from "./ui.js";
|
||||
import { kindLabel, pascal, stateCell } from "./jobs.js";
|
||||
|
||||
const KINDS = ["every", "daily", "weekly", "monthly"];
|
||||
const UNITS = ["minutes", "hours", "days"];
|
||||
/** The unit letters of an interval's server code (every:10m). */
|
||||
const UNIT_CODES = { m: "minutes", h: "hours", d: "days" };
|
||||
/** Day names of the API, Monday first; 1 January 2001 was a Monday. */
|
||||
const DAYS = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
|
||||
const PRIORITIES = ["background", "normal", "user"];
|
||||
@@ -68,6 +70,29 @@ export function recurrenceText(r) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The recurrence a server code describes (ScheduleRecurrence.Format: every:10m, daily:03:00, weekly:mon,thu@02:30,
|
||||
* monthly:1@04:00), in the API's shape for recurrenceText; null for anything else.
|
||||
*/
|
||||
export function parseRecurrence(code) {
|
||||
const match = /^(every|daily|weekly|monthly):(.+)$/.exec(String(code ?? ""));
|
||||
if (!match) return null;
|
||||
const [, kind, rest] = match;
|
||||
const isTime = (s) => /^\d{2}:\d{2}$/.test(s ?? "");
|
||||
if (kind === "every") {
|
||||
const every = /^(\d+)([mhd])$/.exec(rest);
|
||||
return every ? { kind, every: Number(every[1]), unit: UNIT_CODES[every[2]] } : null;
|
||||
}
|
||||
if (kind === "daily") return isTime(rest) ? { kind, time: rest } : null;
|
||||
const [what, time] = rest.split("@");
|
||||
if (!isTime(time)) return null;
|
||||
if (kind === "weekly") {
|
||||
const days = what.split(",");
|
||||
return days.every((d) => DAYS.includes(d)) ? { kind, days, time } : null;
|
||||
}
|
||||
return /^\d+$/.test(what) ? { kind, dayOfMonth: Number(what), time } : null;
|
||||
}
|
||||
|
||||
export async function renderSchedules(root, { api, setMeta, actions, onEvent }, focusSchedule) {
|
||||
let schedules = [];
|
||||
let available = [];
|
||||
@@ -116,11 +141,18 @@ export async function renderSchedules(root, { api, setMeta, actions, onEvent },
|
||||
steps = list.steps ?? [];
|
||||
zone = list.timeZone;
|
||||
locked = settings?.locked ?? [];
|
||||
// A schedule deleted meanwhile (here or by another administrator), or the one a job of the Queue tab links to after
|
||||
// its deletion, is simply not shown: its runs are not asked for.
|
||||
for (const id of expanded) {
|
||||
if (!find(id)) forget(id);
|
||||
}
|
||||
if (focusSchedule && !find(focusSchedule)) focusSchedule = null;
|
||||
// The state of each schedule's last job, for the Last result column (a job the history no longer has shows the outcome).
|
||||
const ids = [...new Set(schedules.filter((s) => s.lastJobId && s.lastOutcome === "queued").map((s) => s.lastJobId))];
|
||||
const jobs = await Promise.all(ids.map((id) => api.get(`/api/v1/jobs/${id}`).catch(() => null)));
|
||||
lastJobs = new Map(jobs.filter(Boolean).map((j) => [j.id, j]));
|
||||
await Promise.all([...expanded].map((id) => loadRuns(id)));
|
||||
// One schedule's runs that cannot be read (deleted since the list) close it; the table is drawn all the same.
|
||||
await Promise.all([...expanded].map((id) => loadRuns(id).catch(() => forget(id))));
|
||||
} catch (err) {
|
||||
toast(errorText(err), true);
|
||||
return;
|
||||
@@ -163,6 +195,12 @@ export async function renderSchedules(root, { api, setMeta, actions, onEvent },
|
||||
return schedules.find((s) => s.id === id);
|
||||
}
|
||||
|
||||
/** Closes a schedule's runs and drops them. */
|
||||
function forget(id) {
|
||||
expanded.delete(id);
|
||||
runs.delete(id);
|
||||
}
|
||||
|
||||
function rowHtml(s) {
|
||||
const name = scheduleName(s);
|
||||
const button = (act, key, extra = "") => `<button class="btn small${extra}" data-act="${act}" data-i18n="Admin.Schedules.${key}" data-i18n-title="Admin.Schedules.${key}.Tip"></button>`;
|
||||
@@ -187,8 +225,8 @@ export async function renderSchedules(root, { api, setMeta, actions, onEvent },
|
||||
/** The last result: the last job's state when the run queued one, else what the run did; when, in the tooltip's words. */
|
||||
function lastResult(s) {
|
||||
if (!s.lastRun) return `<span class="muted" data-i18n="Admin.Schedules.Never"></span>`;
|
||||
const when = `<span class="muted"> · ${escapeHtml(formatTime(s.lastRun))}</span>`;
|
||||
const tip = `${outcomeTip(s.lastOutcome)} · ${formatDateTime(s.lastRun)}`;
|
||||
const when = `<span class="muted"> · ${escapeHtml(zonedShort(s.lastRun, zone))}</span>`;
|
||||
const tip = `${outcomeTip(s.lastOutcome)} · ${zonedDateTime(s.lastRun, zone)}`;
|
||||
const job = s.lastOutcome === "queued" ? lastJobs.get(s.lastJobId) : null;
|
||||
if (job) return `<span title="${attr(tip)}">${stateCell(job.state)}${when}</span>`;
|
||||
const failed = String(s.lastOutcome).startsWith("failed:");
|
||||
@@ -203,7 +241,7 @@ export async function renderSchedules(root, { api, setMeta, actions, onEvent },
|
||||
}
|
||||
return `<div class="runs" role="list" aria-label="${attr(t("Admin.Schedules.RunsOf", scheduleName(s)))}">${page.items.map((j) => `
|
||||
<div class="run" role="listitem"><span class="ellipsis">${escapeHtml(kindLabel(j.kind))}</span><span>${stateCell(j.state)}</span>
|
||||
<span class="muted nowrap num">${escapeHtml(formatDateTime(j.created))}</span>
|
||||
<span class="muted nowrap num">${escapeHtml(zonedDateTime(j.created, zone))}</span>
|
||||
<a class="btn small" href="#/jobs?job=${attr(j.id)}" data-i18n="Admin.Schedules.OpenJob" data-i18n-title="Admin.Schedules.OpenJob.Tip"></a></div>`).join("")}
|
||||
${page.cursor ? `<button class="btn small more" data-more-runs="${attr(s.id)}" data-i18n="Admin.Common.More" data-i18n-title="Admin.Common.More.Tip"></button>` : ""}</div>`;
|
||||
}
|
||||
@@ -217,6 +255,7 @@ export async function renderSchedules(root, { api, setMeta, actions, onEvent },
|
||||
try {
|
||||
await loadRuns(id);
|
||||
} catch (err) {
|
||||
forget(id);
|
||||
toast(errorText(err), true);
|
||||
}
|
||||
draw();
|
||||
@@ -250,7 +289,7 @@ export async function renderSchedules(root, { api, setMeta, actions, onEvent },
|
||||
});
|
||||
if (!confirmed) return;
|
||||
await api.del(`/api/v1/schedules/${schedule.id}`);
|
||||
expanded.delete(schedule.id);
|
||||
forget(schedule.id);
|
||||
} else if (action === "reset") {
|
||||
const confirmed = await confirmDialog({
|
||||
title: t("Admin.Schedules.ResetTitle", scheduleName(schedule)),
|
||||
@@ -424,8 +463,9 @@ export async function renderSchedules(root, { api, setMeta, actions, onEvent },
|
||||
|
||||
setMeta("");
|
||||
onEvent((e) => {
|
||||
// A schedule's run queues jobs; their states change the Last result column.
|
||||
if (e.type === "jobState") {
|
||||
// A schedule changed or ran (also a run that queued nothing, or an edit by another administrator), or a job it queued
|
||||
// changed state (the Last result column); the burst of one pass is read once.
|
||||
if (e.type === "jobState" || e.type === "scheduleChanged") {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(load, 800);
|
||||
}
|
||||
@@ -498,17 +538,34 @@ function stepTip(step) {
|
||||
return has(key) ? t(key) : step;
|
||||
}
|
||||
|
||||
/** An instant in the server's time zone (schedule times are its wall-clock times); in the browser's when the zone is unknown here. */
|
||||
function zonedTime(iso, zone) {
|
||||
if (!iso) return "—";
|
||||
const options = { dateStyle: "short", timeStyle: "short" };
|
||||
/**
|
||||
* An instant in the server's time zone, as every time of this tab is ("Times in …": schedule times are its wall-clock
|
||||
* times); in the browser's when the zone is unknown here.
|
||||
*/
|
||||
function zoned(iso, zone, options) {
|
||||
const date = new Date(iso);
|
||||
try {
|
||||
return new Intl.DateTimeFormat(culture(), { ...options, timeZone: zone }).format(new Date(iso));
|
||||
return new Intl.DateTimeFormat(culture(), { ...options, timeZone: zone }).format(date);
|
||||
} catch {
|
||||
return new Intl.DateTimeFormat(culture(), options).format(new Date(iso));
|
||||
return new Intl.DateTimeFormat(culture(), options).format(date);
|
||||
}
|
||||
}
|
||||
|
||||
function zonedTime(iso, zone) {
|
||||
return iso ? zoned(iso, zone, { dateStyle: "short", timeStyle: "short" }) : "—";
|
||||
}
|
||||
|
||||
function zonedDateTime(iso, zone) {
|
||||
return iso ? zoned(iso, zone, { dateStyle: "short", timeStyle: "medium" }) : "—";
|
||||
}
|
||||
|
||||
/** Like formatTime in the server's time zone: the time when the instant falls on today there, else the date. */
|
||||
function zonedShort(iso, zone) {
|
||||
if (!iso) return "—";
|
||||
const day = (instant) => zoned(instant, zone, { dateStyle: "short" });
|
||||
return zoned(iso, zone, day(iso) === day(Date.now()) ? { timeStyle: "short" } : { dateStyle: "medium" });
|
||||
}
|
||||
|
||||
/** "03:00" as the panel's language writes a time of day. */
|
||||
function timeText(hhmm) {
|
||||
const [h, m] = String(hhmm ?? "").split(":").map(Number);
|
||||
|
||||
@@ -86,7 +86,7 @@ public static class ScheduleEndpoints
|
||||
if (schedule.LastJobId is { } last && await db.Jobs.GetAsync(last, ct) is { State: JobState.Queued or JobState.Running or JobState.Paused })
|
||||
return ApiErrors.Conflict(context, "schedule_pending", last.ToString());
|
||||
if (await host.Scheduler.RunNowAsync(id, caller.Id, ct) is not { } run) return ApiErrors.NotFound(context, "schedule");
|
||||
await audit.WriteAsync(caller, context, "schedule_run", "schedule", id.ToString(), $"{run.Schedule.Name} · {run.Outcome}");
|
||||
await audit.WriteAsync(caller, context, "schedule_run", "schedule", id.ToString(), $"{Named(run.Schedule)} · {run.Outcome}");
|
||||
return Results.Json(run, ProtocolJson.Default.ScheduleRunDto);
|
||||
}
|
||||
|
||||
@@ -109,10 +109,17 @@ public static class ScheduleEndpoints
|
||||
return Results.Json(new Page<JobDto>([.. page.Items.Select(Dto.Job)], page.NextCursor, null), ProtocolJson.Default.PageJobDto);
|
||||
}
|
||||
|
||||
/// <summary>The audit details of a schedule: its name, action and recurrence (<c>Archive scan · scan · every:10m</c>), and "off" while disabled.</summary>
|
||||
/// <summary>
|
||||
/// The audit details of a schedule: its name, action and recurrence code (<c>Weekly check · verify · weekly:mon@02:30</c>),
|
||||
/// and "off" while disabled. A system schedule's name follows its key (<c>system:archive-scan · Archive scan · scan ·
|
||||
/// every:10m</c>): the admin panel shows the name in its language, as its Scheduled tab does, and the recurrence in words.
|
||||
/// </summary>
|
||||
private static string Describe(ScheduleDto schedule)
|
||||
{
|
||||
var recurrence = ScheduleMapping.FromDto(schedule.Recurrence)?.Format() ?? schedule.Recurrence.Kind;
|
||||
return $"{schedule.Name} · {schedule.Action} · {recurrence}{(schedule.Enabled ? string.Empty : " · off")}";
|
||||
return $"{Named(schedule)} · {schedule.Action} · {recurrence}{(schedule.Enabled ? string.Empty : " · off")}";
|
||||
}
|
||||
|
||||
/// <summary>The schedule's name in audit details, after <c>system:<key> · </c> for a system schedule.</summary>
|
||||
private static string Named(ScheduleDto schedule) => schedule.SystemKey is { } key ? $"system:{key} · {schedule.Name}" : schedule.Name;
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using Chronos.Data;
|
||||
using Chronos.Data.Scheduling;
|
||||
using Chronos.Protocol;
|
||||
using Chronos.Server.Configuration;
|
||||
using Chronos.Server.Events;
|
||||
|
||||
namespace Chronos.Server.Jobs;
|
||||
|
||||
@@ -13,26 +14,35 @@ namespace Chronos.Server.Jobs;
|
||||
/// schedules, puts the age limits that environment variables fix into them (<see cref="ServerScheduleHost.LockedAgeOf"/>)
|
||||
/// before the first pass, so a run caught up at start already uses them, and settles missed runs; then it runs the archive
|
||||
/// scan once in the background, as the former polling did, so the photos of an adopted folder are indexed without waiting
|
||||
/// for its first interval (only while that schedule is enabled). The audit retention in force is
|
||||
/// the audit clean-up schedule's age limit, 0 while it is off: Settings › Logs shows it (<see cref="AuditRetentionAsync"/>)
|
||||
/// and sets it (<see cref="ApplyAuditRetentionAsync"/>); <see cref="ServerOptions.AuditRetentionDays"/> is only what the
|
||||
/// schedule is seeded and reset with, or what an environment variable fixes.
|
||||
/// for its first interval (only while that schedule is enabled). Every <see cref="ServerScheduleHost.ScanMinutes"/> it
|
||||
/// takes the settings another writer put into archive.json, as the polling did, whatever the scan schedule does. The audit
|
||||
/// retention in force is the audit clean-up schedule's age limit, 0 while it is off: Settings › Logs shows it
|
||||
/// (<see cref="AuditRetentionAsync"/>) and sets it (<see cref="ApplyAuditRetentionAsync"/>); <see cref="ServerOptions.AuditRetentionDays"/>
|
||||
/// is only what the schedule is seeded and reset with, or what an environment variable fixes. Every schedule change and
|
||||
/// run goes out as a <see cref="ScheduleChangedEvent"/>.
|
||||
/// </summary>
|
||||
public sealed class SchedulerHost : IHostedService
|
||||
{
|
||||
private readonly ChronosDatabase _db;
|
||||
private readonly TimeProvider _time;
|
||||
private readonly ILogger<SchedulerHost> _logger;
|
||||
private readonly CancellationTokenSource _stopping = new();
|
||||
private Task _startScan = Task.CompletedTask;
|
||||
private Task _refresh = Task.CompletedTask;
|
||||
private int _stopped;
|
||||
|
||||
public SchedulerHost(ChronosDatabase db, JobEngineHost jobs, ServerScheduleHost host, TimeProvider time, ILogger<SchedulerHost> logger)
|
||||
public SchedulerHost(ChronosDatabase db, JobEngineHost jobs, ServerScheduleHost host, EventHub events, TimeProvider time, ILogger<SchedulerHost> logger)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(jobs);
|
||||
ArgumentNullException.ThrowIfNull(events);
|
||||
_db = db;
|
||||
_time = time;
|
||||
_logger = logger;
|
||||
Host = host;
|
||||
Scheduler = new Scheduler(db, jobs.Engine, new SchedulerOptions { Time = time });
|
||||
// The Scheduled tab of the admin panel follows them, the runs that queue no job included.
|
||||
Scheduler.ScheduleChanged += schedule => events.Publish(new ScheduleChangedEvent(schedule.Id, Deleted: false));
|
||||
Scheduler.ScheduleDeleted += id => events.Publish(new ScheduleChangedEvent(id, Deleted: true));
|
||||
}
|
||||
|
||||
public Scheduler Scheduler { get; }
|
||||
@@ -45,6 +55,9 @@ public sealed class SchedulerHost : IHostedService
|
||||
await Scheduler.StartAsync(Host, ApplyLockedAgesAsync, cancellationToken);
|
||||
var stopping = _stopping.Token;
|
||||
_startScan = Task.Run(() => ScanAtStartAsync(started, stopping), CancellationToken.None);
|
||||
// Created here, so its first period counts from the start.
|
||||
var timer = new PeriodicTimer(TimeSpan.FromMinutes(ServerScheduleHost.ScanMinutes), _time);
|
||||
_refresh = Task.Run(() => RefreshArchiveAsync(timer, stopping), CancellationToken.None);
|
||||
}
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
@@ -52,6 +65,7 @@ public sealed class SchedulerHost : IHostedService
|
||||
if (Interlocked.Exchange(ref _stopped, 1) == 1) return;
|
||||
await _stopping.CancelAsync();
|
||||
await _startScan;
|
||||
await _refresh;
|
||||
await Scheduler.DisposeAsync();
|
||||
_stopping.Dispose();
|
||||
}
|
||||
@@ -113,4 +127,33 @@ public sealed class SchedulerHost : IHostedService
|
||||
_logger.LogWarning(ex, "The archive scan at start could not be queued; its schedule queues the next one");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Takes the settings a desktop sharing the archive put into archive.json at every tick of <paramref name="timer"/>
|
||||
/// (<see cref="ServerScheduleHost.GetArchiveAsync"/>), so uploads follow them although the scan schedule's runs, which
|
||||
/// read them too, are skipped while a long scan is pending, or are rare or off.
|
||||
/// </summary>
|
||||
private async Task RefreshArchiveAsync(PeriodicTimer timer, CancellationToken cancellationToken)
|
||||
{
|
||||
using (timer)
|
||||
{
|
||||
try
|
||||
{
|
||||
while (await timer.WaitForNextTickAsync(cancellationToken))
|
||||
{
|
||||
try
|
||||
{
|
||||
await Host.GetArchiveAsync(cancellationToken);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
_logger.LogWarning(ex, "The archive's archive.json could not be read; the next tick tries again");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,11 +14,12 @@ public sealed record LockedAge(string Setting, string Variable, int Days);
|
||||
|
||||
/// <summary>
|
||||
/// What the server tells its <see cref="Scheduler"/> (owner review 2, R10): the container's time zone (TZ), the archive it
|
||||
/// serves with the settings another writer put into archive.json (refreshed at every run, as the former polling did), the
|
||||
/// server's own clean-up steps, and its system schedules, which stand for every timer it ran out of sight before: the
|
||||
/// archive scan every 10 minutes (no catch-up), the uploads sweep every 6 hours, the nightly copy of an SQLite index,
|
||||
/// the job history and the audit log retentions. They are seeded from <see cref="ServerOptions"/> once (the user's edits
|
||||
/// stay) and reset to the options as they are then.
|
||||
/// serves with the settings another writer put into archive.json (read again at every run and, whatever the runs do, every
|
||||
/// <see cref="ScanMinutes"/> by <see cref="SchedulerHost"/>, as the former polling did), the server's own clean-up steps,
|
||||
/// and its system schedules, which stand for every timer it ran out of sight before: the archive scan every 10 minutes
|
||||
/// (no catch-up), the uploads sweep every 6 hours, the nightly copy of an SQLite index, the job history and the audit log
|
||||
/// retentions. They are seeded from <see cref="ServerOptions"/> once (the user's edits stay) and reset to the options as
|
||||
/// they are then.
|
||||
/// </summary>
|
||||
public sealed class ServerScheduleHost : IScheduleHost
|
||||
{
|
||||
@@ -91,7 +92,7 @@ public sealed class ServerScheduleHost : IScheduleHost
|
||||
|
||||
/// <summary>
|
||||
/// The served archive, after taking the settings another writer sharing it (a desktop) put into its archive.json, as
|
||||
/// the former polling did before each scan; while it is offline, or the file cannot be read, as the index has it.
|
||||
/// the former polling did; while it is offline, or the file cannot be read, as the index has it.
|
||||
/// </summary>
|
||||
public async ValueTask<Archive?> GetArchiveAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
@@ -27,7 +27,7 @@ public sealed class ServerArchive(Archive archive)
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="Current"/> once it and the index row hold the settings another writer sharing the archive (a desktop)
|
||||
/// put into its archive.json (<see cref="ArchiveService.RefreshSettingsAsync"/>): uploads, the polling and the admin
|
||||
/// put into its archive.json (<see cref="ArchiveService.RefreshSettingsAsync"/>): uploads, the schedules and the admin
|
||||
/// panel, whose form saves every field, never work from older ones. Unchanged while the file cannot be read.
|
||||
/// </summary>
|
||||
public async Task<Archive> RefreshAsync(ArchiveService archives, ChronosDatabase db, CancellationToken cancellationToken)
|
||||
|
||||
@@ -128,6 +128,89 @@ public sealed class ManualTimeProvider : TimeProvider
|
||||
public void Advance(TimeSpan by) => Interlocked.Add(ref _ticks, by.Ticks);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A clock tests move by hand whose timers (<see cref="PeriodicTimer"/>, <c>Task.Delay</c>, timed cancellation) fire only
|
||||
/// when <see cref="Advance"/> reaches them, never in real time: the loops paced by it run exactly when a test says.
|
||||
/// </summary>
|
||||
public sealed class SteppedTimeProvider : TimeProvider
|
||||
{
|
||||
private readonly Lock _gate = new();
|
||||
private readonly List<SteppedTimer> _timers = [];
|
||||
private DateTimeOffset _now = DateTimeOffset.UtcNow;
|
||||
|
||||
public override DateTimeOffset GetUtcNow()
|
||||
{
|
||||
lock (_gate) return _now;
|
||||
}
|
||||
|
||||
public override ITimer CreateTimer(TimerCallback callback, object? state, TimeSpan dueTime, TimeSpan period)
|
||||
{
|
||||
var timer = new SteppedTimer(this, callback, state);
|
||||
timer.Change(dueTime, period);
|
||||
return timer;
|
||||
}
|
||||
|
||||
/// <summary>Moves the clock on, then fires each timer due by then once (a periodic one moves to its next period after now).</summary>
|
||||
public void Advance(TimeSpan by)
|
||||
{
|
||||
List<SteppedTimer> due;
|
||||
lock (_gate)
|
||||
{
|
||||
_now += by;
|
||||
due = [.. _timers.Where(t => t.Due <= _now)];
|
||||
foreach (var timer in due)
|
||||
{
|
||||
if (timer.Period <= TimeSpan.Zero) _timers.Remove(timer);
|
||||
else
|
||||
{
|
||||
while (timer.Due <= _now) timer.Due += timer.Period;
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (var timer in due) timer.Fire();
|
||||
}
|
||||
|
||||
private void Schedule(SteppedTimer timer, TimeSpan dueTime, TimeSpan period)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_timers.Remove(timer);
|
||||
if (dueTime == Timeout.InfiniteTimeSpan) return;
|
||||
timer.Due = _now + dueTime;
|
||||
timer.Period = period == Timeout.InfiniteTimeSpan ? TimeSpan.Zero : period;
|
||||
_timers.Add(timer);
|
||||
}
|
||||
}
|
||||
|
||||
private void Remove(SteppedTimer timer)
|
||||
{
|
||||
lock (_gate) _timers.Remove(timer);
|
||||
}
|
||||
|
||||
private sealed class SteppedTimer(SteppedTimeProvider clock, TimerCallback callback, object? state) : ITimer
|
||||
{
|
||||
public DateTimeOffset Due { get; set; }
|
||||
|
||||
public TimeSpan Period { get; set; }
|
||||
|
||||
public bool Change(TimeSpan dueTime, TimeSpan period)
|
||||
{
|
||||
clock.Schedule(this, dueTime, period);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Fire() => callback(state);
|
||||
|
||||
public void Dispose() => clock.Remove(this);
|
||||
|
||||
public ValueTask DisposeAsync()
|
||||
{
|
||||
Dispose();
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A job handler that reports progress and then waits until <see cref="Release"/> (or cancellation), so tests can
|
||||
/// observe running, paused and cancelled states.
|
||||
|
||||
@@ -4,12 +4,14 @@ using System.Text.Json;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data;
|
||||
using Chronos.Data.Jobs;
|
||||
using Chronos.Data.Scheduling;
|
||||
using Chronos.Protocol;
|
||||
using Chronos.Server.Configuration;
|
||||
using Chronos.Server.Events;
|
||||
using Chronos.Server.Jobs;
|
||||
using Chronos.Server.Storage;
|
||||
using Chronos.Testing;
|
||||
@@ -191,9 +193,83 @@ public sealed class ScheduleEndpointTests(TestContext context)
|
||||
using (var notSystem = await admin.PostAsync($"/api/v1/schedules/{custom.Id}/reset", null))
|
||||
await notSystem.ExpectErrorAsync(HttpStatusCode.Conflict, "schedule_no_defaults");
|
||||
using (var unknown = await admin.PostAsync($"/api/v1/schedules/{Guid.CreateVersion7()}/reset", null)) await unknown.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
|
||||
Assert.Equal("queued", (await RunAsync(admin, housekeeping.Id)).Outcome);
|
||||
|
||||
using var audit = await admin.GetAsync("/api/v1/audit?action=schedule_reset");
|
||||
Assert.Equal(housekeeping.Id.ToString(), (await audit.ReadAsync(ProtocolJson.Default.PageAuditEntryDto)).Items.Single().TargetId);
|
||||
// A system schedule is audited after its key, so the admin panel names it in its language, as its Scheduled tab does.
|
||||
using var audit = await admin.GetAsync("/api/v1/audit?action=schedule_updated,schedule_reset,schedule_run");
|
||||
var entries = (await audit.ReadAsync(ProtocolJson.Default.PageAuditEntryDto)).Items;
|
||||
Assert.SequenceEqual(["schedule_run", "schedule_reset", "schedule_updated"], entries.Select(e => e.Action));
|
||||
Assert.True(entries.All(e => e.TargetId == housekeeping.Id.ToString()), "the target is the schedule");
|
||||
Assert.SequenceEqual(["system:housekeeping · Housekeeping · queued", "system:housekeeping · Housekeeping · cleanup · daily:04:00",
|
||||
"system:housekeeping · Housekeeping · cleanup · weekly:sun@05:15 · off"], entries.Select(e => e.Details));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Every change and run of a schedule goes out on the event socket, also a run that queues no job, so the admin panel's
|
||||
/// Scheduled tab follows what the scheduler does.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public async Task ScheduleChangesAndRunsAreEvents()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var admin = await server.AdminAsync();
|
||||
using var events = server.Services.GetRequiredService<EventHub>().Subscribe();
|
||||
|
||||
// Off, so the scheduler's own passes leave it alone; without a duplicates handler its run queues nothing.
|
||||
var created = await CreateAsync(admin, new CreateScheduleRequest("Duplicates", "duplicates", new RecurrenceDto("daily", Time: "01:00"), Enabled: false));
|
||||
var run = await RunAsync(admin, created.Id);
|
||||
Assert.Equal(("unavailable", (Guid?)null), (run.Outcome, run.JobId));
|
||||
using (var delete = await admin.DeleteAsync($"/api/v1/schedules/{created.Id}")) await delete.ExpectAsync(HttpStatusCode.NoContent);
|
||||
|
||||
var changes = new List<bool>();
|
||||
while (events.Reader.TryRead(out var e))
|
||||
{
|
||||
if (e is ScheduleChangedEvent changed && changed.ScheduleId == created.Id) changes.Add(changed.Deleted);
|
||||
}
|
||||
Assert.SequenceEqual([false, false, true], changes);
|
||||
Assert.Equal($$"""{"type":"scheduleChanged","scheduleId":"{{created.Id}}","deleted":true}""",
|
||||
JsonSerializer.Serialize<ServerEvent>(new ScheduleChangedEvent(created.Id, true), ProtocolJson.Default.ServerEvent));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Settings a desktop put into archive.json reach the index every 10 minutes whatever the archive scan does, as with the
|
||||
/// former polling: the scan's runs read them too, but they are skipped while a long scan is pending, and the schedule may
|
||||
/// be rare or off.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public async Task ArchiveJsonIsReadEveryTenMinutesWhateverTheScanScheduleDoes()
|
||||
{
|
||||
var clock = new SteppedTimeProvider();
|
||||
await using var server = await TestServer.StartAsync(context, services: s =>
|
||||
{
|
||||
ArchiveModule.Register(s);
|
||||
s.AddSingleton<TimeProvider>(clock);
|
||||
});
|
||||
var host = server.Services.GetRequiredService<SchedulerHost>();
|
||||
var archive = server.Services.GetRequiredService<ServerArchive>();
|
||||
// The scan at start is over, then the scan schedule is turned off.
|
||||
await ScansAsync(server, 1);
|
||||
var scanId = Assert.NotNull(await server.Database.Schedules.GetBySystemKeyAsync(ServerScheduleHost.ArchiveScan, Ct)).Id;
|
||||
Assert.Null((await host.Scheduler.UpdateAsync(scanId, new UpdateScheduleRequest(Enabled: false), Ct)).Problem);
|
||||
|
||||
// A desktop sharing the archive changes its folder template; nothing has read it yet.
|
||||
const string template = "{camera}/{date}";
|
||||
var path = new ArchivePaths(archive.Root).ManifestPath;
|
||||
var manifest = ManifestStore.ReadArchive(path);
|
||||
Assert.NotEqual(template, manifest.LayoutTemplates.Folder);
|
||||
ManifestStore.WriteArchive(path, manifest with { LayoutTemplates = manifest.LayoutTemplates with { Folder = template } });
|
||||
Assert.Equal(manifest.LayoutTemplates.Folder, Assert.NotNull(await server.Database.Archives.GetAsync(archive.Id, Ct)).FolderTemplate);
|
||||
|
||||
// Ten minutes on, uploads (the index row) and the admin panel (the served archive) follow it, with no scan.
|
||||
clock.Advance(TimeSpan.FromMinutes(ServerScheduleHost.ScanMinutes));
|
||||
var until = DateTime.UtcNow + TimeSpan.FromSeconds(TestTiming.Budget(30));
|
||||
while (Assert.NotNull(await server.Database.Archives.GetAsync(archive.Id, Ct)).FolderTemplate != template)
|
||||
{
|
||||
Assert.True(DateTime.UtcNow < until, "archive.json was not read again");
|
||||
await Task.Delay(20);
|
||||
}
|
||||
Assert.Equal(template, archive.Current.FolderTemplate);
|
||||
Assert.Count(1, (await server.Database.Jobs.ListAsync(null, JobKind.Scan, 50, null, Ct)).Items);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
Reference in new issue
Block a user