Skip to content

Commit

Permalink
IM THE STUPDEST BITCH ALIVE
Browse files Browse the repository at this point in the history
  • Loading branch information
LolaLollipop committed Dec 3, 2023
1 parent 4e09e1c commit 9fedb0e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
11 changes: 7 additions & 4 deletions RueI/RueI/Displays/Scheduling/Scheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public Scheduler(DisplayCore coordinator)
/// </summary>
internal bool RateLimitActive => rateLimiter.Active;

private static DateTimeOffset Now => DateTimeOffset.UtcNow;

/// <summary>
/// Calculates the weighted time for a list of jobs to be performed.
/// </summary>
Expand Down Expand Up @@ -116,7 +118,7 @@ public void Schedule(ScheduledJob job)
/// <param name="token">An optional token to assign to the <see cref="ScheduledJob"/>.</param>
public void Schedule(TimeSpan time, Action action, int priority, JobToken? token = null)
{
Schedule(new ScheduledJob(DateTimeOffset.UtcNow + time, action, priority, token));
Schedule(new ScheduledJob(Now + time, action, priority, token));
}

/// <summary>
Expand All @@ -128,7 +130,7 @@ public void Schedule(TimeSpan time, Action action, int priority, JobToken? token
/// <param name="token">An optional token to assign to the <see cref="ScheduledJob"/>.</param>
public void Schedule(Action action, TimeSpan time, int priority, JobToken? token = null)
{
Schedule(new ScheduledJob(DateTimeOffset.UtcNow + time, action, priority, token));
Schedule(new ScheduledJob(Now + time, action, priority, token));
}

/// <summary>
Expand Down Expand Up @@ -181,7 +183,7 @@ private void UpdateBatches()
currentBatches.Clear();

List<ScheduledJob> currentBatch = ListPool<ScheduledJob>.Shared.Rent(10);
DateTimeOffset currentBatchTime = DateTimeOffset.UtcNow + MinimumBatch;
DateTimeOffset currentBatchTime = jobs.First().FinishAt + MinimumBatch;

foreach (ScheduledJob job in jobs)
{
Expand All @@ -202,10 +204,11 @@ private void UpdateBatches()
if (currentBatch.Count != 0)
{
BatchJob finishedBatch = new(currentBatch, CalculateWeighted(currentBatch));

currentBatches.Add(finishedBatch);
}

TimeSpan performAt = (currentBatches.First().PerformAt - DateTimeOffset.UtcNow).MaxIf(rateLimiter.Active, rateLimiter.TimeLeft);
TimeSpan performAt = (currentBatches.First().PerformAt - Now).MaxIf(rateLimiter.Active, rateLimiter.TimeLeft);

performTask.Start(performAt, PerformFirstBatch);
}
Expand Down
7 changes: 5 additions & 2 deletions RueITests/TestDisplays.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,20 @@ public async Task TestScheduling()
{
Stopwatch stopwatchOne = new();
Stopwatch stopwatchTwo = new();
Stopwatch stopwatchThree = new();

MockDisplayCore core = new();

core.Scheduler.Schedule(TimeSpan.FromMilliseconds(200), stopwatchOne.Stop);
core.Scheduler.Schedule(TimeSpan.FromMilliseconds(400), stopwatchTwo.Stop);
core.Scheduler.Schedule(TimeSpan.FromMilliseconds(300), stopwatchTwo.Stop);
core.Scheduler.Schedule(TimeSpan.FromMilliseconds(400), stopwatchThree.Stop);

stopwatchOne.Start();
stopwatchTwo.Start();
stopwatchThree.Start();

await Task.Delay(600);

Assert.AreEqual(300, (stopwatchOne.ElapsedMilliseconds + stopwatchTwo.ElapsedMilliseconds) / 2, 50);
Assert.AreEqual(300, (stopwatchOne.ElapsedMilliseconds + stopwatchTwo.ElapsedMilliseconds + stopwatchThree.ElapsedMilliseconds) / 3, 50);
}
}

0 comments on commit 9fedb0e

Please sign in to comment.