Files
T
Alby96andClaude Opus 5.5 af9eeaab9b
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
feat: owner review 2 phase 2: scheduled jobs and log settings (#10)
- 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>
2026-10-27 11:19:26 +01:00

250 lines
10 KiB
C#

using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
using Chronos.Core.Model;
using Chronos.Data;
using Chronos.Data.Archiving;
using Chronos.Data.Jobs;
using Chronos.Server.Configuration;
using Chronos.Server.Storage;
namespace Chronos.Server.Tests;
/// <summary>
/// Stand-in for the archive module's ingest: files staged uploads as <c>&lt;folder&gt;/originals/&lt;name&gt;</c>, one
/// sequence per session key, and records every request it received.
/// </summary>
public sealed class FakeArchiveIngest(ChronosDatabase db) : IArchiveIngest
{
private readonly ConcurrentDictionary<string, Guid> _sessions = new(StringComparer.Ordinal);
private readonly SemaphoreSlim _gate = new(1, 1);
public ConcurrentQueue<IngestRequest> Requests { get; } = new();
public async Task<IngestResult> CommitAsync(IngestRequest request, CancellationToken cancellationToken = default)
{
Requests.Enqueue(request);
await _gate.WaitAsync(cancellationToken);
try
{
var archive = (await db.Archives.GetAsync(request.ArchiveId, cancellationToken))!;
var sequence = await SequenceForAsync(request, archive, cancellationToken);
var existing = (await db.Frames.FindBySha256Async(request.Sha256, cancellationToken)).FirstOrDefault(f => f.SequenceId == sequence.Id);
if (existing is not null)
{
File.Delete(request.StagedPath);
return new IngestResult(sequence.Id, existing.Id, existing.Index, existing.RelativePath, Duplicate: true);
}
var index = sequence.FrameCount;
var relative = $"{sequence.Folder}/originals/{index:D4}_{request.OriginalFileName}";
var target = Path.Combine(archive.RootPath, relative);
Directory.CreateDirectory(Path.GetDirectoryName(target)!);
File.Move(request.StagedPath, target);
var frame = new Frame
{
Id = Guid.CreateVersion7(),
SequenceId = sequence.Id,
Index = index,
RelativePath = relative,
FileName = Path.GetFileName(relative),
OriginalFileName = request.OriginalFileName,
Size = request.Size,
Sha256 = request.Sha256,
CapturedAt = request.ClientCapturedAt,
};
await db.Frames.InsertAsync(frame, cancellationToken);
await db.Sequences.RefreshStatsAsync(sequence.Id, cancellationToken);
return new IngestResult(sequence.Id, frame.Id, index, relative, Duplicate: false);
}
finally
{
_gate.Release();
}
}
private async Task<Sequence> SequenceForAsync(IngestRequest request, Archive archive, CancellationToken cancellationToken)
{
Sequence? sequence = null;
if (request.SequenceId is { } id) sequence = await db.Sequences.GetAsync(id, cancellationToken);
else if (request.SessionKey is { } key && _sessions.TryGetValue(key, out var known)) sequence = await db.Sequences.GetAsync(known, cancellationToken);
if (sequence is null)
{
sequence = new Sequence { Id = Guid.CreateVersion7(), ArchiveId = archive.Id, Folder = string.Empty, Title = request.NewSequenceTitle, OwnerUserId = request.UserId };
if (request.SessionKey is { } key) _sessions[key] = sequence.Id;
}
if (string.IsNullOrEmpty(sequence.Folder))
{
sequence = sequence with { Folder = $"2026/{sequence.Id:N}", UpdatedAt = DateTimeOffset.UtcNow };
await db.Sequences.UpsertAsync(sequence, cancellationToken);
}
return sequence;
}
}
/// <summary>Proxies at <c>&lt;folder&gt;/proxies/NNNN.jpg</c>; thumbnails as 64-byte slots of <c>&lt;folder&gt;/.chronos/thumbs.pack</c>.</summary>
public sealed class FakeAssetLocator : IFrameAssetLocator
{
public const int ThumbnailSize = 64;
/// <summary>When set, every lookup answers this location (path traversal tests).</summary>
public AssetLocation? Override { get; set; }
public ValueTask<AssetLocation?> LocateAsync(Archive archive, Sequence sequence, Frame frame, FrameAsset asset, CancellationToken cancellationToken)
{
if (Override is not null) return ValueTask.FromResult<AssetLocation?>(Override);
var folder = Path.Combine(archive.RootPath, sequence.Folder);
AssetLocation? location = asset == FrameAsset.Proxy
? new AssetLocation(Path.Combine(folder, "proxies", $"{frame.Index:D4}.jpg"), 0, -1, "image/jpeg")
: new AssetLocation(Path.Combine(folder, ".chronos", "thumbs.pack"), (long)frame.Index * ThumbnailSize, ThumbnailSize, "image/jpeg");
if (!File.Exists(location.Path)) return ValueTask.FromResult<AssetLocation?>(null);
if (location.Length < 0) location = location with { Length = new FileInfo(location.Path).Length };
return ValueTask.FromResult<AssetLocation?>(location);
}
}
/// <summary>Measures volumes for real, except the folders given figures of their own (a full archive, a separate array).</summary>
public sealed class FakeVolumeSpace(ServerOptions options, ILogger<VolumeSpace> logger) : VolumeSpace(options, logger)
{
private readonly ConcurrentDictionary<string, VolumeFigures> _figures = new(StringComparer.Ordinal);
/// <summary>Gives <paramref name="path"/> these figures; null measures it for real again.</summary>
public void Set(string path, VolumeFigures? figures)
{
if (figures is null) _figures.TryRemove(Key(path), out _);
else _figures[Key(path)] = figures;
}
public override VolumeFigures? Measure(string path) => _figures.TryGetValue(Key(path), out var figures) ? figures : base.Measure(path);
private static string Key(string path) => Path.TrimEndingDirectorySeparator(Path.GetFullPath(path));
}
/// <summary>A clock tests move by hand (token expiry, heartbeat timeouts).</summary>
public sealed class ManualTimeProvider : TimeProvider
{
private long _ticks = DateTimeOffset.UtcNow.UtcTicks;
public override DateTimeOffset GetUtcNow() => new(Interlocked.Read(ref _ticks), TimeSpan.Zero);
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.
/// </summary>
public sealed class BlockingJobHandler(JobKind kind) : IJobHandler
{
private TaskCompletionSource _release = new(TaskCreationOptions.RunContinuationsAsynchronously);
public JobKind Kind => kind;
public TaskCompletionSource Started { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
/// <summary>When set, the next run fails with this message.</summary>
public string? FailWith { get; set; }
public void Release() => _release.TrySetResult();
public async Task RunAsync(JobContext context)
{
await context.ReportAsync(1, 4, "started");
Started.TrySetResult();
if (FailWith is { } message)
{
FailWith = null;
throw new InvalidOperationException(message);
}
for (var i = 2; i <= 3; i++)
{
await Task.Delay(50, context.Cancellation);
await context.ReportAsync(i, 4);
}
await _release.Task.WaitAsync(context.Cancellation);
_release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
await context.ReportAsync(4, 4);
}
}