diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index e22411ff..b5b07bc1 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -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 | diff --git a/src/Chronos.Protocol/Events.cs b/src/Chronos.Protocol/Events.cs index faa87132..d196f576 100644 --- a/src/Chronos.Protocol/Events.cs +++ b/src/Chronos.Protocol/Events.cs @@ -4,7 +4,7 @@ namespace Chronos.Protocol; /// /// Messages pushed on WS /api/v1/events. Each frame is one JSON object with a type discriminator: -/// jobProgress, jobState, sequenceChanged, cardEvent, nodeState. +/// jobProgress, jobState, sequenceChanged, cardEvent, nodeState, scheduleChanged. /// [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; + +/// +/// A schedule of the server was created, edited, reset, 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. +/// +public sealed record ScheduleChangedEvent(Guid ScheduleId, bool Deleted) : ServerEvent; diff --git a/src/Chronos.Server/Admin/web/audit.js b/src/Chronos.Server/Admin/web/audit.js index 097d86bd..47287fa2 100644 --- a/src/Chronos.Server/Admin/web/audit.js +++ b/src/Chronos.Server/Admin/web/audit.js @@ -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: · ]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: · ]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:" 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)]; diff --git a/src/Chronos.Server/Admin/web/schedules.js b/src/Chronos.Server/Admin/web/schedules.js index 9baf494f..228149d9 100644 --- a/src/Chronos.Server/Admin/web/schedules.js +++ b/src/Chronos.Server/Admin/web/schedules.js @@ -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 = "") => ``; @@ -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 ``; - const when = ` · ${escapeHtml(formatTime(s.lastRun))}`; - const tip = `${outcomeTip(s.lastOutcome)} · ${formatDateTime(s.lastRun)}`; + const when = ` · ${escapeHtml(zonedShort(s.lastRun, zone))}`; + const tip = `${outcomeTip(s.lastOutcome)} · ${zonedDateTime(s.lastRun, zone)}`; const job = s.lastOutcome === "queued" ? lastJobs.get(s.lastJobId) : null; if (job) return `${stateCell(job.state)}${when}`; const failed = String(s.lastOutcome).startsWith("failed:"); @@ -203,7 +241,7 @@ export async function renderSchedules(root, { api, setMeta, actions, onEvent }, } return `
${page.items.map((j) => `
${escapeHtml(kindLabel(j.kind))}${stateCell(j.state)} - ${escapeHtml(formatDateTime(j.created))} + ${escapeHtml(zonedDateTime(j.created, zone))}
`).join("")} ${page.cursor ? `` : ""}
`; } @@ -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); diff --git a/src/Chronos.Server/Api/ScheduleEndpoints.cs b/src/Chronos.Server/Api/ScheduleEndpoints.cs index a244625b..d7991416 100644 --- a/src/Chronos.Server/Api/ScheduleEndpoints.cs +++ b/src/Chronos.Server/Api/ScheduleEndpoints.cs @@ -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([.. page.Items.Select(Dto.Job)], page.NextCursor, null), ProtocolJson.Default.PageJobDto); } - /// The audit details of a schedule: its name, action and recurrence (Archive scan · scan · every:10m), and "off" while disabled. + /// + /// The audit details of a schedule: its name, action and recurrence code (Weekly check · verify · weekly:mon@02:30), + /// and "off" while disabled. A system schedule's name follows its key (system:archive-scan · Archive scan · scan · + /// every:10m): the admin panel shows the name in its language, as its Scheduled tab does, and the recurrence in words. + /// 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")}"; } + + /// The schedule's name in audit details, after system:<key> · for a system schedule. + private static string Named(ScheduleDto schedule) => schedule.SystemKey is { } key ? $"system:{key} · {schedule.Name}" : schedule.Name; } diff --git a/src/Chronos.Server/Jobs/SchedulerHost.cs b/src/Chronos.Server/Jobs/SchedulerHost.cs index 377d434c..8d670895 100644 --- a/src/Chronos.Server/Jobs/SchedulerHost.cs +++ b/src/Chronos.Server/Jobs/SchedulerHost.cs @@ -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 () /// 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 () -/// and sets it (); 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 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 +/// () and sets it (); +/// 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 . /// public sealed class SchedulerHost : IHostedService { private readonly ChronosDatabase _db; + private readonly TimeProvider _time; private readonly ILogger _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 logger) + public SchedulerHost(ChronosDatabase db, JobEngineHost jobs, ServerScheduleHost host, EventHub events, TimeProvider time, ILogger 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"); } } + + /// + /// Takes the settings a desktop sharing the archive put into archive.json at every tick of + /// (), 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. + /// + 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) + { + } + } + } } diff --git a/src/Chronos.Server/Jobs/ServerScheduleHost.cs b/src/Chronos.Server/Jobs/ServerScheduleHost.cs index 8481defa..7782ded1 100644 --- a/src/Chronos.Server/Jobs/ServerScheduleHost.cs +++ b/src/Chronos.Server/Jobs/ServerScheduleHost.cs @@ -14,11 +14,12 @@ public sealed record LockedAge(string Setting, string Variable, int Days); /// /// What the server tells its (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 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 +/// by , 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 once (the user's edits stay) and reset to the options as +/// they are then. /// public sealed class ServerScheduleHost : IScheduleHost { @@ -91,7 +92,7 @@ public sealed class ServerScheduleHost : IScheduleHost /// /// 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. /// public async ValueTask GetArchiveAsync(CancellationToken cancellationToken) { diff --git a/src/Chronos.Server/Storage/ServerArchive.cs b/src/Chronos.Server/Storage/ServerArchive.cs index f260f28e..d33fdf7e 100644 --- a/src/Chronos.Server/Storage/ServerArchive.cs +++ b/src/Chronos.Server/Storage/ServerArchive.cs @@ -27,7 +27,7 @@ public sealed class ServerArchive(Archive archive) /// /// once it and the index row hold the settings another writer sharing the archive (a desktop) - /// put into its archive.json (): uploads, the polling and the admin + /// put into its archive.json (): uploads, the schedules and the admin /// panel, whose form saves every field, never work from older ones. Unchanged while the file cannot be read. /// public async Task RefreshAsync(ArchiveService archives, ChronosDatabase db, CancellationToken cancellationToken) diff --git a/tests/Chronos.Server.Tests/Fakes.cs b/tests/Chronos.Server.Tests/Fakes.cs index 2d1a0adc..8d8e56b4 100644 --- a/tests/Chronos.Server.Tests/Fakes.cs +++ b/tests/Chronos.Server.Tests/Fakes.cs @@ -128,6 +128,89 @@ public sealed class ManualTimeProvider : TimeProvider public void Advance(TimeSpan by) => Interlocked.Add(ref _ticks, by.Ticks); } +/// +/// A clock tests move by hand whose timers (, Task.Delay, timed cancellation) fire only +/// when reaches them, never in real time: the loops paced by it run exactly when a test says. +/// +public sealed class SteppedTimeProvider : TimeProvider +{ + private readonly Lock _gate = new(); + private readonly List _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; + } + + /// Moves the clock on, then fires each timer due by then once (a periodic one moves to its next period after now). + public void Advance(TimeSpan by) + { + List 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; + } + } +} + /// /// A job handler that reports progress and then waits until (or cancellation), so tests can /// observe running, paused and cancelled states. diff --git a/tests/Chronos.Server.Tests/Jobs/ScheduleEndpointTests.cs b/tests/Chronos.Server.Tests/Jobs/ScheduleEndpointTests.cs index 5ad00a04..fa2b880f 100644 --- a/tests/Chronos.Server.Tests/Jobs/ScheduleEndpointTests.cs +++ b/tests/Chronos.Server.Tests/Jobs/ScheduleEndpointTests.cs @@ -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)); + } + + /// + /// 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. + /// + [Test] + public async Task ScheduleChangesAndRunsAreEvents() + { + await using var server = await TestServer.StartAsync(context); + var admin = await server.AdminAsync(); + using var events = server.Services.GetRequiredService().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(); + 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(new ScheduleChangedEvent(created.Id, true), ProtocolJson.Default.ServerEvent)); + } + + /// + /// 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. + /// + [Test] + public async Task ArchiveJsonIsReadEveryTenMinutesWhateverTheScanScheduleDoes() + { + var clock = new SteppedTimeProvider(); + await using var server = await TestServer.StartAsync(context, services: s => + { + ArchiveModule.Register(s); + s.AddSingleton(clock); + }); + var host = server.Services.GetRequiredService(); + var archive = server.Services.GetRequiredService(); + // 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]