[12.x] Add withHeartbeat
method to LazyCollection
#56477
+100
−0
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.
Adds a new
withHeartbeat
method toLazyCollection
, which allows you to run a callback at regular time intervals while the collection is being lazily enumerated:Main use case
In long-running tasks such as batch processing reports, you may need to hold a lock to prevent concurrent execution. However, if the code unexpectedly fails to release the lock, you don't want it to persist indefinitely. A common strategy is to acquire a short-lived lock, and then periodically extend it while the task is still running.
Here's an example where we acquire a lock for 5 minutes, then reacquire it every 4 minutes (assuming a report never takes a full minute to generate):
Note: the locking logic above is simplified for clarity. It's essentially pseudo-code.