fix: guarantee every repeatable jobs are slotted #2966
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds a guarantee for repeatable jobs based on the "every" option so that only one delayed job per job scheduler can ever be produced by making sure the next iteration is "slotted". By "slotted" we mean that we only allow the repeatable jobs for a given every setting to be placed in evenly slots in the time space. So for example, if we have an every setting of 5000 (5 seconds), then the repetition will be slotted in time 0, 5, 10, 15, 20, 25, and so on. However we also compute an offset so that we can start the iterations at the same point in time the job scheduler is added to the queue. So for instance, if we add the job scheduler at second 10:23:03 (second 3), the iterations will be slotted at 3, 8, 13, 18, and so on.
Another improvement in this PR affects retried repeatable jobs. A repeatable job that has produced the next repeatable job iteration will be marked as such and will not create new iterations independently on how many times this job is retried.