feat: owner review 2 phase 2: scheduled jobs and log settings (#10)
CI / Build and test (push) Successful in 7m0s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 13m28s
CI / PostgreSQL 16 integration (push) Successful in 5m52s
CI / PostgreSQL 17 integration (push) Successful in 4m50s
CI / Docker image (push) Successful in 1m4s
Release / Server image (push) Successful in 1m35s

- Jobs > Scheduled on the desktop and the admin panel. Every timer that queued work is now a visible system schedule: archive scan, upload clean-up, index backup, job-history and audit clean-ups, desktop housekeeping. New actions: verify, thumbnails, proxies, analysis, maintenance, backup, clean-ups, reports.
- The scheduler: migration 0007, in-house recurrence, compare-and-set claim, catch-up, scan at start.
- Log settings: levels, retention, size parts, total limit, daily or hourly rotation, audit retention, diagnostics share; the server writes Debug/Trace to its files again; logging never fails a job.

Upgrade: CHRONOS_LOG_LEVEL set in an existing container locks both levels; migration 0007 is one-way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
This commit was merged in pull request #10.
This commit is contained in:
Alby96andClaude Opus 5.5 authored and Alby96 committed 2026-10-27 11:19:26 +01:00
1 parent 1f089888e8
commit af9eeaab9b
349 files changed
+28341 -1304

No files matched your search

@@ -2,7 +2,7 @@ using Chronos.Core.Model;
namespace Chronos.Data.Repositories;
/// <summary>Append-only record of who did what (BRIEF §5 AuditLog, §14.2).</summary>
/// <summary>Append-only record of who did what (BRIEF §5 AuditLog, §14.2); old entries leave only through a retention (<see cref="PurgeAsync"/>).</summary>
public sealed class AuditRepository
{
private const string Columns = "id, at, user_id, username, action, target_kind, target_id, details, remote_address";
@@ -16,6 +16,10 @@ public sealed class AuditRepository
[entry.Id, entry.At, entry.UserId, entry.Username, entry.Action, entry.TargetKind, entry.TargetId, entry.Details, entry.RemoteAddress],
cancellationToken).ConfigureAwait(false);
/// <summary>Deletes the entries written before <paramref name="before"/> (the audit retention's clean-up step); returns how many.</summary>
public Task<int> PurgeAsync(DateTimeOffset before, CancellationToken cancellationToken = default) =>
_db.ExecuteAsync("DELETE FROM audit_log WHERE at < $1", [before], cancellationToken);
/// <summary>Entries newest first, optionally of one user.</summary>
public Task<Page<AuditEntry>> ListAsync(Guid? userId = null, int limit = 100, string? cursor = null, CancellationToken cancellationToken = default) =>
ListAsync(userId, action: null, limit, cursor, cancellationToken);
+75 -3
View File
@@ -15,7 +15,11 @@ public sealed class JobRepository
{
private const string Columns =
"id, kind, target_ids, target_label, parameters_json, state, priority, progress_done, progress_total, items_per_second, " +
"eta_seconds, checkpoint, log_tail, created_by, node_id, attempts, error, created, started, finished";
"eta_seconds, checkpoint, log_tail, created_by, node_id, attempts, error, created, started, finished, schedule_id";
/// <summary>The states of a job that has not ended: queued, running, paused.</summary>
private static readonly string PendingStates =
string.Join(", ", new[] { JobState.Queued, JobState.Running, JobState.Paused }.Select(s => ((int)s).ToString(CultureInfo.InvariantCulture)));
private readonly DbAccess _db;
@@ -23,11 +27,11 @@ public sealed class JobRepository
public async Task EnqueueAsync(Job job, CancellationToken cancellationToken = default) =>
await _db.ExecuteAsync(
$"INSERT INTO jobs ({Columns}) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20)",
$"INSERT INTO jobs ({Columns}) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21)",
[
job.Id, (int)job.Kind, string.Join(',', job.TargetIds), job.TargetLabel, DbParam.Json(job.ParametersJson), (int)job.State,
(int)job.Priority, job.ProgressDone, job.ProgressTotal, job.ItemsPerSecond, job.Eta?.TotalSeconds, job.Checkpoint, job.LogTail,
job.CreatedBy, job.NodeId, job.Attempts, job.Error, job.Created, job.Started, job.Finished,
job.CreatedBy, job.NodeId, job.Attempts, job.Error, job.Created, job.Started, job.Finished, job.ScheduleId,
], cancellationToken).ConfigureAwait(false);
public Task<Job?> GetAsync(Guid id, CancellationToken cancellationToken = default) =>
@@ -173,6 +177,73 @@ public sealed class JobRepository
$"SELECT count(*) FROM jobs WHERE state = {(int)JobState.Queued} AND kind = $1 AND target_ids = $2",
[(int)kind, targetId.ToString()], cancellationToken).ConfigureAwait(false) > 0;
/// <summary>
/// True when a job of one of <paramref name="kinds"/> is queued, running or paused; with <paramref name="targetIds"/>,
/// only such a job that targets at least one of them (an archive, sequences) counts. A schedule does not queue its
/// action again while one waits (owner review 2, R10).
/// </summary>
public async Task<bool> HasPendingAsync(IReadOnlyCollection<JobKind> kinds, IReadOnlyCollection<Guid>? targetIds = null,
CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(kinds);
if (kinds.Count == 0 || targetIds is { Count: 0 }) return false;
var p = new SqlParameters();
var sql = $"SELECT count(*) FROM jobs WHERE state IN ({PendingStates}) AND kind IN ({KindList(kinds)})";
// Target ids are stored comma-joined as lower-case text, which never holds a LIKE wildcard.
if (targetIds is not null)
sql += $" AND ({string.Join(" OR ", targetIds.Distinct().Select(id => $"target_ids LIKE {p.Add(SqlDialect.ContainsPattern(id.ToString()))} ESCAPE '\\'"))})";
return await _db.ScalarAsync<long>(sql, p.ToArray(), cancellationToken).ConfigureAwait(false) > 0;
}
/// <summary>The targets of the queued, running and paused jobs of <paramref name="kind"/> (the newest 5000 such jobs).</summary>
public Task<HashSet<Guid>> ListPendingTargetsAsync(JobKind kind, CancellationToken cancellationToken = default) =>
_db.RunAsync(async session =>
{
var rows = await session.QueryListAsync(
$"SELECT target_ids FROM jobs WHERE kind = $1 AND state IN ({PendingStates}) ORDER BY created DESC, id DESC LIMIT 5000",
[(int)kind], r => r.GetString(0), cancellationToken).ConfigureAwait(false);
var ids = new HashSet<Guid>();
foreach (var text in rows)
{
if (text.Length == 0) continue;
foreach (var id in text.Split(',')) ids.Add(Guid.Parse(id));
}
return ids;
}, cancellationToken);
/// <summary>The jobs the schedule <paramref name="scheduleId"/> queued, newest first.</summary>
public Task<IReadOnlyList<Job>> ListByScheduleAsync(Guid scheduleId, int limit = 20, CancellationToken cancellationToken = default) =>
_db.ListAsync($"SELECT {Columns} FROM jobs WHERE schedule_id = $1 ORDER BY created DESC, id DESC LIMIT $2",
[scheduleId, Cursor.ClampLimit(limit)], Map, cancellationToken);
/// <summary>
/// The jobs the schedule <paramref name="scheduleId"/> queued, newest first, a page at a time (the runs a schedule's
/// row lists in the admin panel); <paramref name="cursor"/> continues after the previous page.
/// </summary>
public Task<Page<Job>> PageByScheduleAsync(Guid scheduleId, int limit = 20, string? cursor = null, CancellationToken cancellationToken = default)
{
var p = new SqlParameters();
var where = $"schedule_id = {p.Add(scheduleId)}";
if (cursor is not null)
{
var (value, id) = Cursor.Decode("schedule-runs", cursor);
where += " AND " + Cursor.After("created", "id", descending: true, value, id, p.Add);
}
var take = Cursor.ClampLimit(limit);
var sql = $"SELECT {Columns} FROM jobs WHERE {where} ORDER BY created DESC, id DESC LIMIT {p.Add(take + 1)}";
return _db.RunAsync(async session =>
{
var jobs = await session.QueryListAsync(sql, p.ToArray(), Map, cancellationToken).ConfigureAwait(false);
string? next = null;
if (jobs.Count > take)
{
jobs.RemoveAt(take);
next = Cursor.Encode("schedule-runs", jobs[^1].Created, jobs[^1].Id);
}
return new Page<Job>(jobs, next);
}, cancellationToken);
}
/// <summary>
/// Claims one specific queued job for <paramref name="nodeId"/> (render nodes pick by capability); null when another
/// claimer was faster or the job is no longer queued.
@@ -245,5 +316,6 @@ public sealed class JobRepository
Created = r.GetDateTimeOffset(17),
Started = r.GetDateTimeOffsetOrNull(18),
Finished = r.GetDateTimeOffsetOrNull(19),
ScheduleId = r.GetGuidOrNull(20),
};
}
@@ -0,0 +1,171 @@
using Chronos.Core.Model;
using Chronos.Data.Scheduling;
namespace Chronos.Data.Repositories;
/// <summary>
/// Scheduled jobs (owner review 2, R10). A run is claimed by moving <c>next_run</c> with a compare-and-set
/// (<see cref="TryAdvanceAsync"/>), so when several processes share one database (server instances on one PostgreSQL)
/// exactly one fires it. System schedules are seeded without touching rows that exist (<see cref="EnsureSystemAsync"/>), so
/// what the user changed stays, and they are never deleted (<see cref="DeleteAsync"/>).
/// </summary>
public sealed class ScheduleRepository
{
private const string Columns =
"id, system_key, name, action, target, parameters_json, recurrence, catch_up, priority, enabled, next_run, last_run, last_job_id, " +
"last_outcome, created_by, created, updated";
private const string InsertSql =
$"INSERT INTO schedules ({Columns}) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)";
/// <summary>System schedules first, then the others, oldest first.</summary>
private const string Order = "CASE WHEN system_key IS NULL THEN 1 ELSE 0 END, created, id";
private readonly DbAccess _db;
internal ScheduleRepository(DbAccess db) => _db = db;
/// <summary>Every schedule: the system ones first, then the others, oldest first.</summary>
public Task<IReadOnlyList<Schedule>> ListAsync(CancellationToken cancellationToken = default) =>
_db.ListAsync($"SELECT {Columns} FROM schedules ORDER BY {Order}", [], Map, cancellationToken);
public Task<Schedule?> GetAsync(Guid id, CancellationToken cancellationToken = default) =>
_db.SingleAsync($"SELECT {Columns} FROM schedules WHERE id = $1", [id], Map, cancellationToken);
public Task<Schedule?> GetBySystemKeyAsync(string systemKey, CancellationToken cancellationToken = default) =>
_db.SingleAsync($"SELECT {Columns} FROM schedules WHERE system_key = $1", [systemKey], Map, cancellationToken);
/// <summary>Adds a schedule; a <see cref="Schedule.SystemKey"/> another row has already fails (unique).</summary>
public async Task InsertAsync(Schedule schedule, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(schedule);
await _db.ExecuteAsync(InsertSql, Values(schedule), cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Stores what a user edits — name, action, target, parameters, recurrence, catch-up, priority, enabled — and the update
/// time; with <paramref name="setNextRun"/> the next run too, otherwise it stays as stored (a run the scheduler claims
/// meanwhile is not undone). The key, the creation and the last run stay as stored. False when the schedule is gone.
/// </summary>
public async Task<bool> UpdateAsync(Schedule schedule, bool setNextRun = true, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(schedule);
return await _db.ExecuteAsync(
"UPDATE schedules SET name = $2, action = $3, target = $4, parameters_json = $5, recurrence = $6, catch_up = $7, priority = $8, " +
$"enabled = $9, updated = $10{(setNextRun ? ", next_run = $11" : string.Empty)} WHERE id = $1",
[
schedule.Id, schedule.Name, schedule.Action, schedule.Target, DbParam.Json(schedule.ParametersJson), schedule.Recurrence,
schedule.CatchUp, (int)schedule.Priority, schedule.Enabled, schedule.Updated, .. setNextRun ? new DbParam[] { schedule.NextRun } : [],
], cancellationToken).ConfigureAwait(false) > 0;
}
/// <summary>Deletes a schedule a user created; false when it is unknown or a system schedule (those are disabled, never deleted).</summary>
public async Task<bool> DeleteAsync(Guid id, CancellationToken cancellationToken = default) =>
await _db.ExecuteAsync("DELETE FROM schedules WHERE id = $1 AND system_key IS NULL", [id], cancellationToken).ConfigureAwait(false) > 0;
/// <summary>
/// Inserts each system schedule whose key no row has yet (<c>INSERT … ON CONFLICT (system_key) DO NOTHING</c>), with
/// no next run (the scheduler computes it), or due at <paramref name="now"/> when it is enabled and its work was last
/// done a period or more before (<see cref="SystemSchedule.LastDone"/>); rows that exist keep whatever the user changed.
/// Rows added together are stamped a microsecond apart, so listings keep the host's order. Returns how many were added.
/// </summary>
public Task<int> EnsureSystemAsync(IEnumerable<SystemSchedule> schedules, DateTimeOffset now, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(schedules);
var rows = schedules.Select((s, i) => FromSystem(s, now.AddTicks(i * TimeSpan.TicksPerMicrosecond)) with
{
NextRun = s.Enabled && s.LastDone is { } done && now - done >= s.Recurrence.Period ? now : null,
}).ToList();
return rows.Count == 0 ? Task.FromResult(0) : _db.InTransactionAsync(async session =>
{
var added = 0;
foreach (var row in rows)
added += await session.ExecuteAsync(InsertSql + " ON CONFLICT (system_key) DO NOTHING", Values(row), cancellationToken).ConfigureAwait(false);
return added;
}, cancellationToken);
}
/// <summary>
/// Puts the system schedule <paramref name="systemKey"/> back to <paramref name="defaults"/> (name, action, parameters,
/// recurrence, catch-up, priority, enabled) with no next run, so the scheduler computes it again; its history stays.
/// False when no row has that key.
/// </summary>
public async Task<bool> ResetSystemAsync(string systemKey, SystemSchedule defaults, DateTimeOffset now, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(defaults);
var row = FromSystem(defaults, now);
return await _db.ExecuteAsync(
"UPDATE schedules SET name = $2, action = $3, target = $4, parameters_json = $5, recurrence = $6, catch_up = $7, priority = $8, " +
"enabled = $9, next_run = NULL, updated = $10 WHERE system_key = $1",
[
systemKey, row.Name, row.Action, row.Target, DbParam.Json(row.ParametersJson), row.Recurrence, row.CatchUp, (int)row.Priority, row.Enabled,
now,
], cancellationToken).ConfigureAwait(false) > 0;
}
/// <summary>Enabled schedules whose next run is at or before <paramref name="nowUtc"/>, the longest due first.</summary>
public Task<IReadOnlyList<Schedule>> ListDueAsync(DateTimeOffset nowUtc, CancellationToken cancellationToken = default) =>
_db.ListAsync($"SELECT {Columns} FROM schedules WHERE enabled = TRUE AND next_run IS NOT NULL AND next_run <= $1 ORDER BY next_run, id",
[nowUtc], Map, cancellationToken);
/// <summary>
/// Moves an enabled schedule's next run from <paramref name="expectedNextRun"/> (null: none yet) to
/// <paramref name="nextRun"/>, only if it still is <paramref name="expectedNextRun"/>: true for the one caller that won.
/// Claiming a due run this way is what keeps two processes from both firing it.
/// </summary>
public async Task<bool> TryAdvanceAsync(Guid id, DateTimeOffset? expectedNextRun, DateTimeOffset? nextRun, CancellationToken cancellationToken = default) =>
await (expectedNextRun is { } expected
? _db.ExecuteAsync("UPDATE schedules SET next_run = $2 WHERE id = $1 AND enabled = TRUE AND next_run = $3", [id, nextRun, expected], cancellationToken)
: _db.ExecuteAsync("UPDATE schedules SET next_run = $2 WHERE id = $1 AND enabled = TRUE AND next_run IS NULL", [id, nextRun], cancellationToken))
.ConfigureAwait(false) > 0;
/// <summary>
/// Records a run: when (<paramref name="atUtc"/>), what it did (<see cref="ScheduleOutcome"/>) and the first job it
/// queued; a run that queued nothing keeps the previous job, so the next run still sees whether it is pending.
/// </summary>
public async Task<bool> RecordOutcomeAsync(Guid id, Guid? jobId, string outcome, DateTimeOffset atUtc, CancellationToken cancellationToken = default) =>
await _db.ExecuteAsync("UPDATE schedules SET last_run = $2, last_job_id = COALESCE($3, last_job_id), last_outcome = $4 WHERE id = $1",
[id, atUtc, jobId, outcome], cancellationToken).ConfigureAwait(false) > 0;
private static Schedule FromSystem(SystemSchedule system, DateTimeOffset now) => new()
{
Id = Guid.CreateVersion7(),
SystemKey = system.SystemKey,
Name = system.Name,
Action = system.Action,
ParametersJson = ScheduleMapping.ParametersJson(system.Action, system.Parameters),
Recurrence = system.Recurrence.Format(),
CatchUp = system.CatchUp,
Priority = system.Priority,
Enabled = system.Enabled,
Created = now,
Updated = now,
};
private static DbParam[] Values(Schedule s) =>
[
s.Id, s.SystemKey, s.Name, s.Action, s.Target, DbParam.Json(s.ParametersJson), s.Recurrence, s.CatchUp, (int)s.Priority, s.Enabled, s.NextRun,
s.LastRun, s.LastJobId, s.LastOutcome, s.CreatedBy, s.Created, s.Updated,
];
private static Schedule Map(DbRow r) => new()
{
Id = r.GetGuid(0),
SystemKey = r.GetStringOrNull(1),
Name = r.GetString(2),
Action = r.GetString(3),
Target = r.GetString(4),
ParametersJson = r.GetStringOrNull(5),
Recurrence = r.GetString(6),
CatchUp = r.GetBool(7),
Priority = (JobPriority)r.GetInt32(8),
Enabled = r.GetBool(9),
NextRun = r.GetDateTimeOffsetOrNull(10),
LastRun = r.GetDateTimeOffsetOrNull(11),
LastJobId = r.GetGuidOrNull(12),
LastOutcome = r.GetStringOrNull(13),
CreatedBy = r.GetGuidOrNull(14),
Created = r.GetDateTimeOffset(15),
Updated = r.GetDateTimeOffset(16),
};
}
@@ -389,6 +389,21 @@ public sealed class SequenceRepository
}, cancellationToken);
}
/// <summary>
/// The sequences of <paramref name="archiveId"/> a rolling verification checks next (owner review 2, R10): those never
/// verified first, then the longest unverified, at most <paramref name="limit"/>. With <paramref name="verifiedBefore"/>
/// only those never verified or last verified before it are listed. Sequences without frames are left out.
/// </summary>
public Task<IReadOnlyList<Guid>> ListVerifyCandidatesAsync(Guid archiveId, DateTimeOffset? verifiedBefore, int limit,
CancellationToken cancellationToken = default)
{
var p = new SqlParameters();
var where = $"archive_id = {p.Add(archiveId)} AND frame_count > 0";
if (verifiedBefore is { } before) where += $" AND (verified_at IS NULL OR verified_at < {p.Add(before)})";
return _db.ListAsync($"SELECT id FROM sequences WHERE {where} ORDER BY verified_at ASC NULLS FIRST, id LIMIT {p.Add(Math.Max(1, limit))}",
p.ToArray(), r => r.GetGuid(0), cancellationToken);
}
/// <summary>Folder and title of every sequence of an archive, by folder.</summary>
public Task<IReadOnlyList<SequenceLocation>> ListLocationsAsync(Guid archiveId, CancellationToken cancellationToken = default) =>
_db.ListAsync("SELECT id, folder, title FROM sequences WHERE archive_id = $1 ORDER BY folder, id", [archiveId],