18 using System.Collections.Generic;
39 private bool _needsMoveNext;
40 private bool _endOfScheduledEvents;
41 private readonly Action<string, DateTime> _callback;
42 private readonly IEnumerator<DateTime> _orderedEventUtcTimes;
60 internal bool IsLoggingEnabled
72 if (_endOfScheduledEvents)
74 return DateTime.MaxValue;
79 _needsMoveNext =
false;
80 _endOfScheduledEvents = !_orderedEventUtcTimes.MoveNext();
83 return _orderedEventUtcTimes.Current;
90 public string Name {
get; }
98 public ScheduledEvent(
string name, DateTime eventUtcTime, Action<string, DateTime> callback =
null)
99 : this(name, new[] { eventUtcTime }.AsEnumerable().GetEnumerator(), callback)
109 public ScheduledEvent(
string name, IEnumerable<DateTime> orderedEventUtcTimes, Action<string, DateTime> callback =
null)
110 : this(name, orderedEventUtcTimes.GetEnumerator(), callback)
120 public ScheduledEvent(
string name, IEnumerator<DateTime> orderedEventUtcTimes, Action<string, DateTime> callback =
null)
124 _callback = callback;
126 _needsMoveNext =
true;
127 _orderedEventUtcTimes = orderedEventUtcTimes;
136 return Name.GetHashCode();
145 return !ReferenceEquals(
null, obj) && ReferenceEquals(
this, obj);
152 internal void Scan(DateTime utcTime)
154 if (_endOfScheduledEvents)
164 if (!_orderedEventUtcTimes.MoveNext())
166 if (IsLoggingEnabled)
168 Log.
Trace($
"ScheduledEvent.{Name}: Completed scheduled events.");
170 _endOfScheduledEvents =
true;
173 if (IsLoggingEnabled)
175 Log.
Trace($
"ScheduledEvent.{Name}: Next event: {_orderedEventUtcTimes.Current.ToStringInvariant(DateFormat.UI)} UTC");
180 if (utcTime >= _orderedEventUtcTimes.Current)
182 if (IsLoggingEnabled)
184 Log.
Trace($
"ScheduledEvent.{Name}: Firing at {utcTime.ToStringInvariant(DateFormat.UI)} UTC " +
185 $
"Scheduled at {_orderedEventUtcTimes.Current.ToStringInvariant(DateFormat.UI)} UTC"
190 _needsMoveNext =
true;
195 _needsMoveNext =
false;
201 while (_needsMoveNext);
208 internal void SkipEventsUntil(DateTime utcTime)
215 if (IsLoggingEnabled)
217 Log.
Trace($
"ScheduledEvent.{Name}: Skipped events before {utcTime.ToStringInvariant(DateFormat.UI)}. " +
218 $
"Next event: {_orderedEventUtcTimes.Current.ToStringInvariant(DateFormat.UI)}"
224 while (_orderedEventUtcTimes.MoveNext());
226 if (IsLoggingEnabled)
228 Log.
Trace($
"ScheduledEvent.{Name}: Exhausted event stream during skip until {utcTime.ToStringInvariant(DateFormat.UI)}");
230 _endOfScheduledEvents =
true;
245 void IDisposable.Dispose()
247 _orderedEventUtcTimes.Dispose();
259 _callback?.Invoke(
Name, _orderedEventUtcTimes.Current);