Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(scheduler): change to single task based scheduler #235

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

abdullah-cognite
Copy link
Collaborator

@abdullah-cognite abdullah-cognite commented Feb 12, 2025

Current scheduler has a dotnet Task for each routine revision. This leads to problems when a large number of RoutineRevisions are scheduled at the exact same time every hour. This PR changes the approach so we dont create Tasks at all, rather have just one Task that keeps waking up every minute and goes through all the routines in the state to decide which needs to be scheduled based on their cron expression.

@abdullah-cognite abdullah-cognite requested a review from a team as a code owner February 12, 2025 22:09
Copy link

codecov bot commented Feb 12, 2025

Codecov Report

Attention: Patch coverage is 65.00000% with 21 lines in your changes missing coverage. Please review.

Project coverage is 71.35%. Comparing base (4c00348) to head (721cb34).

Files with missing lines Patch % Lines
Cognite.Simulator.Utils/SimulationScheduler.cs 65.00% 18 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #235      +/-   ##
==========================================
- Coverage   71.57%   71.35%   -0.23%     
==========================================
  Files          33       33              
  Lines        3536     3501      -35     
  Branches      420      413       -7     
==========================================
- Hits         2531     2498      -33     
- Misses        848      849       +1     
+ Partials      157      154       -3     
Files with missing lines Coverage Δ
Cognite.Simulator.Utils/SimulationScheduler.cs 71.66% <65.00%> (-5.11%) ⬇️


// If the next occurrence is less or equal than the current UTC time (plus tolerance)
// and is greater than the last run time, trigger the simulation run.
if (nextOccurrence <= DateTime.UtcNow.Add(tolerance) && nextOccurrence > lastRun)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (nextOccurrence <= DateTime.UtcNow.Add(tolerance) && nextOccurrence > lastRun)
if (nextOccurrence >= DateTime.UtcNow && nextOccurrence <= DateTime.UtcNow.Add(tolerance) && nextOccurrence > lastRun)

otherwise it might fire before the actual schedule (we can't allow that)

@@ -43,50 +42,25 @@ public async Task Delay(TimeSpan delay, CancellationToken token)

/// <summary>
/// Represents a scheduled job for simulation.
/// (No longer used since scheduling is now done in a single loop.)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you keeping it then?

// to a new configuration on the API
if (scheduledJobs.TryGetValue(routineRev.RoutineExternalId, out var job))
// Parse the cron schedule.
CrontabSchedule schedule;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe put this into the map as well, so we don't recompute it each time?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could probably repurpose ScheduledJob for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants