Files
Chronos/tests/Chronos.App.Tests/Jobs/ScheduleFixture.cs
T
Alby96 bc786c12c4 feat(app): Jobs › Scheduled and the desktop scheduler
Owner review 2, R10 on the desktop: every timer that queued work out of
sight is now a visible, editable schedule of this PC.

- DesktopScheduleHost implements IScheduleHost: the PC's time zone, the
  archive at Settings › Archive's folder (refreshed from archive.json,
  none while it is offline), and three system schedules: archive-scan
  (every:<ScanIntervalMinutes>m, seeded once from the old setting, no
  catch-up), index-backup (every day with catch-up, replacing
  DatabaseBackups.RunDailyAsync) and housekeeping (daily 04:00 with
  catch-up: job history 90 days and the new cache-trim step).
- AppServices starts the Scheduler after the engine, once the archive was
  reopened; an unplugged archive is looked for every minute and the
  scheduler woken when it is back. The rescan loop and the daily backup
  loop are gone; ApplyJobSettings wakes the scheduler.
- CacheStore.Trim brings the cache to Settings › Storage › Cache limit,
  oldest files first, skipping files in use and the proxies, render-node
  and server-import folders; CacheTrimStep runs it as a clean-up step.
- Jobs gets a Queue | Scheduled segmented control. Scheduled lists the
  schedules (switch, name, action, when, next run, last result) with Run
  now, Edit, Reset (system) or Delete (user's, confirmed), the selected
  schedule's last runs with Show in queue, and an in-window editor that
  checks every field with the scheduler's limits. Jobs a schedule queued
  carry a "Scheduled" pill; the history line follows the housekeeping's
  job-history days.
- JobNotices: a scheduled background run raises a toast only when it
  fails (upkeep stays silent).
- Settings › Archive › Rescan every is removed; ScanIntervalMinutes stays
  only as the seed. Strings in en and it; the others are retranslated.
- The capture shows jobs-scheduled and jobs-schedule-editor on a stopped
  scheduler clock with sample schedules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit e25d812feb3f417cb9251c748c29e8035fd9b03c)
2026-10-27 02:26:07 +01:00

98 lines
4.2 KiB
C#
Raw 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();
}
}