24 using System.Collections.Generic;
34 private bool _sortingScheduledEventsRequired;
35 private List<ScheduledEvent> _scheduledEventsSortedByTime =
new List<ScheduledEvent>();
41 public override bool IsActive {
get;
protected set; }
50 base.Setup(algorithm, job, resultHandler, api, isolatorLimitProvider);
52 foreach (var scheduledEvent
in GetScheduledEventsSortedByTime())
55 scheduledEvent.SkipEventsUntil(algorithm.UtcTime);
60 _sortingScheduledEventsRequired =
true;
78 scheduledEvent.IsLoggingEnabled =
true;
81 _sortingScheduledEventsRequired =
true;
93 _sortingScheduledEventsRequired =
true;
102 var scheduledEvents = GetScheduledEventsSortedByTime();
105 while (scheduledEvents.Count > 0 && scheduledEvents[0].NextEventUtcTime <= time)
111 catch (Exception exception)
113 Algorithm.SetRuntimeError(exception, $
"Scheduled event: '{scheduledEvents[0].Name}' at {time}");
127 var scheduledEvents = GetScheduledEventsSortedByTime();
130 while (scheduledEvents.Count > 0 && scheduledEvents[0].NextEventUtcTime < time)
132 var scheduledEvent = scheduledEvents[0];
133 var nextEventUtcTime = scheduledEvent.NextEventUtcTime;
141 catch (Exception exception)
143 Algorithm.SetRuntimeError(exception, $
"Scheduled event: '{scheduledEvent.Name}' at {nextEventUtcTime}");
151 private List<ScheduledEvent> GetScheduledEventsSortedByTime()
153 if (_sortingScheduledEventsRequired)
155 _sortingScheduledEventsRequired =
false;
158 .OrderBy(x => x.Key.NextEventUtcTime)
161 .ThenBy(x => x.Value)
162 .Select(x => x.Key).ToList();
165 return _scheduledEventsSortedByTime;
174 var scheduledEvent = scheduledEvents[0];
175 var nextEventUtcTime = scheduledEvent.NextEventUtcTime;
177 if (scheduledEvents.Count > 1
179 && nextEventUtcTime > scheduledEvents[1].NextEventUtcTime)
182 scheduledEvents.RemoveAt(0);
184 var position = scheduledEvents.BinarySearch(nextEventUtcTime,
185 (time, orderEvent) => time.CompareTo(orderEvent.NextEventUtcTime));
189 var finalPosition = position + 1;
190 if (finalPosition == scheduledEvents.Count)
193 scheduledEvents.Add(scheduledEvent);
199 scheduledEvents.Insert(finalPosition, scheduledEvent);
204 var index = ~position;
205 if (index == scheduledEvents.Count)
208 scheduledEvents.Add(scheduledEvent);
213 scheduledEvents.Insert(index, scheduledEvent);