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:
Alby96 committed 2026-10-27 09:57:23 +01:00
1 parent e384564784
commit 3f0e6357b1
10 files changed
+339 -43

No files matched your search

+29 -7
View File
@@ -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)];
+73 -16
View File
@@ -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);
+10 -3
View File
@@ -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:&lt;key&gt; · </c> for a system schedule.</summary>
private static string Named(ScheduleDto schedule) => schedule.SystemKey is { } key ? $"system:{key} · {schedule.Name}" : schedule.Name;
}
+48 -5
View File
@@ -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)
{
+1 -1
View File
@@ -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)