Adding 100ms minimum delay for recurring tasks #615
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.
Hey @rosa
While I was trying to reduce the flakiness of
test/integration/instrumentation_test.rb
, I uncovered a real issue:On rare occasions the scheduler would recurse with a near-zero delay and enqueue 2-3 jobs in the same second, which 100% shouldn't happen. The output was something like:
Explanation:
So what happens is:
delay_from_now
function returns something very close to 0, because the timestamp is2025-08-01T02:41:38.995992Z
delay_from_now
≈ 0 executes the block right away#schedule_task
, which invokes#schedule
again (still zero delay), and so on, nesting 2-3 immediate runs before any enqueue happens.The fix
By clamping the delay to at least 100 ms - when the raw delay is just a few milliseconds before the next whole-second mark - that extra 0.1 s pushes the scheduled timestamp into the following second, guaranteeing Step 3 (the enqueue) has time to complete and prevents Step 2 from going into recursion.
It's kinda confusing 😅, but the risk should be very low, and I have confirmed the test no longer fails: