fix: Streamlines event scheduler API. Adds months to weekly recurrence (#2178)

This commit is contained in:
Kamron Batman 2025-05-07 22:42:44 -07:00 • committed by GitHub
parent 990e86b983
commit f95b3a8b3b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 347 additions and 137 deletions

View file

@ -2,59 +2,31 @@ using System;
namespace Server.Engines.Events;
public abstract class ScheduledEvent
public abstract class ScheduledEvent : BaseScheduledEvent
{
public IRecurrencePattern Recurrence { get; }
public TimeZoneInfo TimeZone { get; }
public TimeOnly Time { get; }
public DateTime EndDate { get; }
public DateTime NextOccurrence { get; protected set; }
public bool Cancelled { get; private set; }
public ScheduledEvent(DateTime startOn, TimeZoneInfo timeZone = null)
: this(startOn, startOn, TimeOnly.FromDateTime(startOn), null, timeZone)
{
}
public ScheduledEvent(DateTime startAfter, TimeOnly time, IRecurrencePattern recurrence, TimeZoneInfo timeZone = null)
: this(startAfter, DateTime.MaxValue, time, recurrence, timeZone)
public ScheduledEvent(TimeOnly time, IRecurrencePattern recurrence = null)
: this(time, DateTime.MaxValue, recurrence)
{
}
public ScheduledEvent(
DateTime startAfter,
DateTime endOn,
TimeOnly time,
IRecurrencePattern recurrence,
TimeZoneInfo timeZone = null
DateTime endOn,
IRecurrencePattern recurrence = null
)
{
Time = time;
Recurrence = recurrence;
TimeZone = timeZone ?? TimeZoneInfo.Utc;
EndDate = endOn == DateTime.MaxValue || endOn.Kind == DateTimeKind.Utc ? endOn : endOn.LocalToUtc(TimeZone);
var afterUtc = startAfter.Kind == DateTimeKind.Utc ? startAfter : startAfter.LocalToUtc(TimeZone);
var next = GetOccurrence(afterUtc);
// For the first occurrence, we should set it to the startAfter date if we have no recurrence.
NextOccurrence = next == DateTime.MaxValue ? afterUtc : next;
}
protected virtual DateTime GetOccurrence(DateTime after)
protected override DateTime GetNextOccurrence(DateTime after)
{
var next = Recurrence?.GetNextOccurrence(after, Time, TimeZone) ?? DateTime.MaxValue;
return next >= EndDate ? DateTime.MaxValue : next;
}
public void Cancel() => Cancelled = true;
public virtual DateTime Advance()
{
OnEvent();
return NextOccurrence = GetOccurrence(NextOccurrence);
}
public abstract void OnEvent();
}