Owner review 2, R10: every timer that queues work becomes a visible schedule, so the server and the desktop need one shared engine to run them. - Migration 0007: schedules table (system rows keyed by system_key) and jobs.schedule_id; Job.ScheduleId, JobDto.ScheduleId, JobRepository HasPendingAsync / ListPendingTargetsAsync / ListByScheduleAsync. - ScheduleRepository: seeding that keeps user edits, reset, compare-and-set claim of a run (one winner across processes), outcomes. - Scheduling: in-house recurrence (every/daily/weekly/monthly, DST gap moves forward, ambiguous time runs once), ScheduleActions resolving scan, verify, thumbnails, proxies, analysis, maintenance, backup, cleanup, duplicates and orphans into archive jobs, IScheduleHost, and the Scheduler loop (catch-up, clock going back, run now, CRUD as API v1 DTOs). - New job kinds Backup (16) and Cleanup (17) with handlers, shared clean-up steps (job history, audit log, trash report that never deletes) and host steps through ArchiveServiceOptions; read-only Duplicates and Orphans report handlers, so the server can run those kinds too. - JobEngine no longer purges history at start: the housekeeping schedule's job-history step does; JobEngine.CanRun and an EnqueueAsync with scheduleId. - Protocol DTOs for schedules; job kind names in en and it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit eb0edd2483a493db7c6945d5b494461cabb2a4ad)
175 lines
9.4 KiB
C#
175 lines
9.4 KiB
C#
using Chronos.Data.Scheduling;
|
|
using Chronos.Protocol;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.Data.Tests.Scheduling;
|
|
|
|
/// <summary>The in-house recurrence of schedules (owner review 2, R10): text form, intervals on their anchor, wall-clock times across clock changes.</summary>
|
|
[Category("schedules")]
|
|
public sealed class ScheduleRecurrenceTests
|
|
{
|
|
/// <summary>Central European time, built here so the test does not depend on the machine's zone data: +1, +2 from the last Sunday of March 02:00 to the last Sunday of October 03:00.</summary>
|
|
internal static readonly TimeZoneInfo Cet = TimeZoneInfo.CreateCustomTimeZone("Test/CET", TimeSpan.FromHours(1), "Test CET", "Test CET", "Test CEST",
|
|
[
|
|
TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(DateTime.MinValue.Date, DateTime.MaxValue.Date, TimeSpan.FromHours(1),
|
|
TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0), 3, 5, DayOfWeek.Sunday),
|
|
TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 3, 0, 0), 10, 5, DayOfWeek.Sunday)),
|
|
]);
|
|
|
|
private static DateTimeOffset Utc(int month, int day, int hour, int minute = 0, int second = 0) => new(2026, month, day, hour, minute, second, TimeSpan.Zero);
|
|
|
|
[Theory]
|
|
[Case("every:5m", "every:5m")]
|
|
[Case("every:1440m", "every:1440m")]
|
|
[Case(" Every : 6H ", "every:6h")]
|
|
[Case("every:7d", "every:7d")]
|
|
[Case("daily:3:05", "daily:03:05")]
|
|
[Case("daily:23:59", "daily:23:59")]
|
|
[Case("weekly:thu,mon,thu@2:30", "weekly:mon,thu@02:30")]
|
|
[Case("WEEKLY:sun@00:00", "weekly:sun@00:00")]
|
|
[Case("monthly:28@04:00", "monthly:28@04:00")]
|
|
[Case("monthly: 1 @ 04:00", "monthly:1@04:00")]
|
|
public void TheTextFormRoundTrips(string text, string canonical)
|
|
{
|
|
var recurrence = ScheduleRecurrence.Parse(text);
|
|
Assert.Equal(canonical, recurrence.Format());
|
|
Assert.Equal(recurrence, ScheduleRecurrence.Parse(recurrence.Format()));
|
|
Assert.Equal(recurrence, ScheduleMapping.FromDto(ScheduleMapping.ToDto(recurrence)));
|
|
}
|
|
|
|
[Theory]
|
|
[Case("")]
|
|
[Case("every")]
|
|
[Case("every:")]
|
|
[Case("every:4m")]
|
|
[Case("every:1441m")]
|
|
[Case("every:0h")]
|
|
[Case("every:169h")]
|
|
[Case("every:367d")]
|
|
[Case("every:10x")]
|
|
[Case("every:-5m")]
|
|
[Case("every:1.5h")]
|
|
[Case("hourly:1")]
|
|
[Case("daily:24:00")]
|
|
[Case("daily:12:60")]
|
|
[Case("daily:3:5")]
|
|
[Case("daily:")]
|
|
[Case("weekly:@03:00")]
|
|
[Case("weekly:mon")]
|
|
[Case("weekly:mon,xyz@03:00")]
|
|
[Case("monthly:0@03:00")]
|
|
[Case("monthly:29@03:00")]
|
|
[Case("monthly:@03:00")]
|
|
public void InvalidTextIsRefused(string text)
|
|
{
|
|
Assert.False(ScheduleRecurrence.TryParse(text, out _), text);
|
|
Assert.Throws<FormatException>(() => ScheduleRecurrence.Parse(text));
|
|
}
|
|
|
|
[Test]
|
|
public void TheFactoriesCheckTheirRanges()
|
|
{
|
|
Assert.False(ScheduleRecurrence.TryParse(null, out _));
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => ScheduleRecurrence.Interval(4, RecurrenceUnit.Minutes));
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => ScheduleRecurrence.MonthlyAt(29, new TimeOnly(3, 0)));
|
|
Assert.Throws<ArgumentException>(() => ScheduleRecurrence.WeeklyAt([], new TimeOnly(3, 0)));
|
|
// Seconds are dropped: schedules work to the minute.
|
|
Assert.Equal(ScheduleRecurrence.DailyAt(new TimeOnly(3, 0)), ScheduleRecurrence.DailyAt(new TimeOnly(3, 0, 42)));
|
|
Assert.Equal(ScheduleRecurrence.Parse("weekly:thu,mon@02:30"), ScheduleRecurrence.WeeklyAt([DayOfWeek.Monday, DayOfWeek.Thursday], new TimeOnly(2, 30)));
|
|
Assert.NotEqual(ScheduleRecurrence.Parse("weekly:mon@02:30"), ScheduleRecurrence.Parse("weekly:tue@02:30"));
|
|
Assert.SequenceEqual([DayOfWeek.Monday, DayOfWeek.Sunday], ScheduleRecurrence.Parse("weekly:sun,mon@02:30").Days);
|
|
}
|
|
|
|
[Test]
|
|
public void PeriodsBoundTheGapBetweenRuns()
|
|
{
|
|
Assert.Equal(TimeSpan.FromMinutes(10), ScheduleRecurrence.Parse("every:10m").Period);
|
|
Assert.Equal(TimeSpan.FromHours(6), ScheduleRecurrence.Parse("every:6h").Period);
|
|
Assert.Equal(TimeSpan.FromDays(2), ScheduleRecurrence.Parse("every:2d").Period);
|
|
Assert.Equal(TimeSpan.FromDays(1), ScheduleRecurrence.Parse("daily:03:00").Period);
|
|
Assert.Equal(TimeSpan.FromDays(7), ScheduleRecurrence.Parse("weekly:mon@03:00").Period);
|
|
Assert.Equal(TimeSpan.FromDays(31), ScheduleRecurrence.Parse("monthly:1@03:00").Period);
|
|
}
|
|
|
|
[Test]
|
|
public void IntervalsStayOnTheGridOfTheirAnchor()
|
|
{
|
|
var every10 = ScheduleRecurrence.Parse("every:10m");
|
|
var anchor = Utc(10, 2, 12, 0);
|
|
Assert.Equal(Utc(10, 2, 12, 10), every10.Next(anchor, TimeZoneInfo.Utc, anchor));
|
|
// Late by 35 minutes: the missed runs collapse into the next one on the grid, no drift.
|
|
Assert.Equal(Utc(10, 2, 12, 40), every10.Next(Utc(10, 2, 12, 35, 10), TimeZoneInfo.Utc, anchor));
|
|
Assert.Equal(Utc(10, 2, 12, 40), every10.Next(Utc(10, 2, 12, 30), TimeZoneInfo.Utc, anchor));
|
|
// An anchor ahead of the time asked about is itself on the grid.
|
|
Assert.Equal(Utc(10, 2, 11, 50), every10.Next(Utc(10, 2, 11, 45), TimeZoneInfo.Utc, anchor));
|
|
Assert.Equal(Utc(10, 2, 12, 0), every10.Next(Utc(10, 2, 11, 59, 59), TimeZoneInfo.Utc, anchor));
|
|
// Without an anchor: one interval after the minute.
|
|
Assert.Equal(Utc(10, 2, 12, 13), every10.Next(Utc(10, 2, 12, 3, 27), TimeZoneInfo.Utc));
|
|
// Days of an interval are 24 hours whatever the clocks do.
|
|
Assert.Equal(Utc(3, 29, 1, 30), ScheduleRecurrence.Parse("every:1d").Next(Utc(3, 28, 1, 30), Cet, Utc(3, 28, 1, 30)));
|
|
}
|
|
|
|
[Test]
|
|
public void DailyWeeklyAndMonthlyTimesAreWallClockTimes()
|
|
{
|
|
var daily = ScheduleRecurrence.Parse("daily:03:00");
|
|
Assert.Equal(Utc(10, 2, 3, 0), daily.Next(Utc(10, 2, 2, 59), TimeZoneInfo.Utc));
|
|
Assert.Equal(Utc(10, 3, 3, 0), daily.Next(Utc(10, 2, 3, 0), TimeZoneInfo.Utc));
|
|
// 03:00 in Rome on 2 October (summer time) is 01:00 UTC; in January (winter time) 02:00 UTC.
|
|
Assert.Equal(Utc(10, 2, 1, 0), daily.Next(Utc(10, 1, 23, 0), Cet));
|
|
Assert.Equal(new DateTimeOffset(2027, 1, 15, 2, 0, 0, TimeSpan.Zero), daily.Next(new DateTimeOffset(2027, 1, 15, 1, 0, 0, TimeSpan.Zero), Cet));
|
|
|
|
// 2 October 2026 is a Friday.
|
|
var weekly = ScheduleRecurrence.Parse("weekly:mon,thu@08:00");
|
|
Assert.Equal(Utc(10, 5, 6, 0), weekly.Next(Utc(10, 2, 12, 0), Cet));
|
|
Assert.Equal(Utc(10, 8, 6, 0), weekly.Next(Utc(10, 5, 6, 0), Cet));
|
|
Assert.Equal(Utc(10, 12, 6, 0), weekly.Next(Utc(10, 8, 7, 0), Cet));
|
|
|
|
var monthly = ScheduleRecurrence.Parse("monthly:28@04:00");
|
|
Assert.Equal(Utc(3, 28, 4, 0), monthly.Next(Utc(2, 28, 5, 0), TimeZoneInfo.Utc));
|
|
Assert.Equal(Utc(2, 28, 4, 0), monthly.Next(Utc(1, 28, 4, 0), TimeZoneInfo.Utc));
|
|
}
|
|
|
|
[Test]
|
|
public void ATimeTheClocksSkipMovesForwardByTheGap()
|
|
{
|
|
// 29 March 2026: 02:00 CET becomes 03:00 CEST, so 02:30 does not exist that night and runs at 03:30 CEST.
|
|
var daily = ScheduleRecurrence.Parse("daily:02:30");
|
|
var next = daily.Next(Utc(3, 28, 23, 0), Cet);
|
|
Assert.Equal(Utc(3, 29, 1, 30), next);
|
|
Assert.Equal(new DateTime(2026, 3, 29, 3, 30, 0), TimeZoneInfo.ConvertTime(next, Cet).DateTime);
|
|
// The next night is a normal one again.
|
|
Assert.Equal(Utc(3, 30, 0, 30), daily.Next(next, Cet));
|
|
Assert.Equal(Utc(3, 29, 1, 30), ScheduleRecurrence.ToUtc(new DateTime(2026, 3, 29, 2, 30, 0), Cet));
|
|
}
|
|
|
|
[Test]
|
|
public void ATimeTheClocksGoBackOverRunsOnceAtItsFirstOccurrence()
|
|
{
|
|
// 25 October 2026: 03:00 CEST becomes 02:00 CET, so 02:30 happens twice; it runs at the first (00:30 UTC).
|
|
var daily = ScheduleRecurrence.Parse("daily:02:30");
|
|
var first = daily.Next(Utc(10, 24, 23, 0), Cet);
|
|
Assert.Equal(Utc(10, 25, 0, 30), first);
|
|
// Not again an hour later at the second 02:30: the next run is the next day's.
|
|
Assert.Equal(Utc(10, 26, 1, 30), daily.Next(first, Cet));
|
|
Assert.Equal(Utc(10, 26, 1, 30), daily.Next(Utc(10, 25, 1, 0), Cet));
|
|
Assert.Equal(Utc(10, 25, 0, 30), ScheduleRecurrence.ToUtc(new DateTime(2026, 10, 25, 2, 30, 0), Cet));
|
|
}
|
|
|
|
[Test]
|
|
public void TheStructuredFormIsCheckedLikeTheText()
|
|
{
|
|
Assert.Equal("every:15m", ScheduleMapping.FromDto(new RecurrenceDto("every", Every: 15, Unit: "minutes"))?.Format());
|
|
Assert.Equal("weekly:mon,fri@06:00", ScheduleMapping.FromDto(new RecurrenceDto("Weekly", Time: "06:00", Days: ["fri", "mon"]))?.Format());
|
|
Assert.Equal("monthly:3@01:15", ScheduleMapping.FromDto(new RecurrenceDto("monthly", Time: "01:15", DayOfMonth: 3))?.Format());
|
|
Assert.Equal(new RecurrenceDto("weekly", Time: "06:00", Days: null), ScheduleMapping.ToDto(ScheduleRecurrence.Parse("weekly:mon@06:00")) with { Days = null });
|
|
Assert.Null(ScheduleMapping.FromDto(null));
|
|
Assert.Null(ScheduleMapping.FromDto(new RecurrenceDto("every", Every: 2, Unit: "minutes")));
|
|
Assert.Null(ScheduleMapping.FromDto(new RecurrenceDto("every", Every: 2, Unit: "weeks")));
|
|
Assert.Null(ScheduleMapping.FromDto(new RecurrenceDto("daily")));
|
|
Assert.Null(ScheduleMapping.FromDto(new RecurrenceDto("weekly", Time: "06:00", Days: [])));
|
|
Assert.Null(ScheduleMapping.FromDto(new RecurrenceDto("monthly", Time: "06:00", DayOfMonth: 31)));
|
|
Assert.Null(ScheduleMapping.FromDto(new RecurrenceDto("yearly", Time: "06:00")));
|
|
}
|
|
}
|