Files
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

98 lines
4.2 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.IO;
using Chronos.App.Infrastructure;
using Chronos.App.Jobs;
using Chronos.Data;
using Chronos.Data.Archiving;
using Chronos.Data.Jobs;
using Chronos.Data.Scheduling;
using Chronos.Testing;
namespace Chronos.App.Tests.Jobs;
/// <summary>A clock the test moves by hand; its timers are the system's.</summary>
internal sealed class ManualClock(DateTimeOffset start) : TimeProvider
{
private long _ticks = start.UtcTicks;
public DateTimeOffset Now => GetUtcNow();
public override DateTimeOffset GetUtcNow() => new(Interlocked.Read(ref _ticks), TimeSpan.Zero);
public void Advance(TimeSpan by) => Interlocked.Add(ref _ticks, by.Ticks);
}
/// <summary>
/// The desktop's schedules as <see cref="AppServices"/> composes them, without the shell: an SQLite index file, the archive
/// engine with the cache-trim step and a backups folder, a job engine that is not started (queued jobs stay queued), and a
/// <see cref="Scheduler"/> on <see cref="DesktopScheduleHost"/> with a clock the test moves.
/// </summary>
internal sealed class ScheduleFixture : IAsyncDisposable
{
public static readonly DateTimeOffset T0 = new(2026, 10, 2, 9, 0, 0, TimeSpan.Zero);
private ScheduleFixture(TestContext context, AppSettings settings, ChronosDatabase db, string folder)
{
Context = context;
Settings = settings;
Db = db;
Folder = folder;
Cache = Path.Combine(folder, "cache");
Backups = Path.Combine(folder, DatabaseBackups.DirectoryName);
Archives = new ArchiveService(db, new ArchiveServiceOptions
{
Owner = "test",
CleanupSteps = [new CacheTrimStep(() => Cache, () => CacheLimitBytes)],
BackupDirectory = Backups,
});
Engine = new JobEngine(db, Archives.CreateJobHandlers(), new JobEngineOptions { WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(100) });
Host = new DesktopScheduleHost(settings, () => Db, () => Archives) { TimeZone = TimeZoneInfo.Utc };
}
public TestContext Context { get; }
public AppSettings Settings { get; }
public ChronosDatabase Db { get; }
public string Folder { get; }
public string Cache { get; }
public string Backups { get; }
public long CacheLimitBytes { get; set; } = 1L << 30;
public ArchiveService Archives { get; }
public JobEngine Engine { get; }
public DesktopScheduleHost Host { get; }
public ManualClock Clock { get; } = new(T0);
public Scheduler? Scheduler { get; private set; }
public string DatabasePath => Path.Combine(Folder, "chronos.db");
public static async Task<ScheduleFixture> CreateAsync(TestContext context, AppSettings? settings = null)
{
var folder = context.CreateTempDirectory("schedules");
var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(folder, "chronos.db")), context.Cancellation);
return new ScheduleFixture(context, settings ?? new AppSettings { Language = "en" }, db, folder);
}
/// <summary>Starts a scheduler on the host (a previous one is stopped first, as at the next start of the app).</summary>
public async Task<Scheduler> StartAsync()
{
if (Scheduler is not null) await Scheduler.DisposeAsync();
Scheduler = new Scheduler(Db, Engine, new SchedulerOptions { Time = Clock, Tick = TimeSpan.FromHours(1) });
await Scheduler.StartAsync(Host, Context.Cancellation);
return Scheduler;
}
/// <summary>The view model of Jobs › Scheduled on this scheduler; deletes and resets are confirmed with <paramref name="confirm"/>.</summary>
public async Task<SchedulesViewModel> ViewModelAsync(Func<ConfirmRequest, bool>? confirm = null, List<string>? toasts = null)
{
var scheduler = Scheduler ?? await StartAsync();
var vm = new SchedulesViewModel(() => scheduler, () => Db, confirm ?? (_ => true), (title, _, _) => toasts?.Add(title), _ => Task.CompletedTask);
await vm.AttachAsync(Task.FromResult(true));
return vm;
}
public async ValueTask DisposeAsync()
{
if (Scheduler is not null) await Scheduler.DisposeAsync();
await Engine.DisposeAsync();
await Db.DisposeAsync();
}
}