Skip to content

Commit

Permalink
[Fix](Job)Fix the Calculation of the First Trigger Time and Add a Sin…
Browse files Browse the repository at this point in the history
…gle-Time Scheduling Compensation Logic

### Background:
The previous scheduling logic for calculating the first trigger time had the following issues:

If the current time (currentTimeMs) exceeds the start of the time window (windowStartTimeMs), the first trigger time could be skipped, causing some tasks to be missed.
Minor delays during code execution (e.g., between window initialization and scheduling logic execution) could result in tasks being missed within the active time window.
### Solution:

- Adjustment of First Trigger Time Logic:

 When the initially calculated firstTriggerTime is less than or equal to the currentTimeMs, compute the number of missed intervals and directly adjust to the largest trigger time that is less than the currentTimeMs.
- Single-Time Compensation Logic:

 Introduced a compensation mechanism to handle the slight delay caused by code execution. This ensures that tasks missed during the initialization phase are correctly scheduled.
The compensation is explicitly single-time to avoid impacting the normal scheduling logic.
  • Loading branch information
CalvinKirs committed Nov 19, 2024
1 parent a6926b8 commit acec12a
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private List<Long> getExecutionDelaySeconds(long windowStartTimeMs, long windowE

long firstTriggerTime = windowStartTimeMs + (intervalMs - ((windowStartTimeMs - startTimeMs)
% intervalMs)) % intervalMs;
if (firstTriggerTime <= currentTimeMs) {
if (firstTriggerTime < currentTimeMs) {
// Calculate how many intervals to add to get the largest trigger time < currentTimeMs
long intervalsToAdd = (currentTimeMs - firstTriggerTime) / intervalMs;
firstTriggerTime += intervalsToAdd * intervalMs;
Expand Down

0 comments on commit acec12a

Please sign in to comment.