Skip to content

[12.x] Add withHeartbeat method to LazyCollection #56477

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

Open
wants to merge 1 commit into
base: 12.x
Choose a base branch
from

Conversation

JosephSilber
Copy link
Contributor

@JosephSilber JosephSilber commented Jul 29, 2025

Adds a new withHeartbeat method to LazyCollection, which allows you to run a callback at regular time intervals while the collection is being lazily enumerated:

$collection
    ->withHeartbeat(CarbonInterval::minutes(5), function () {
        // ...
    })
    ->each(function ($item) {
        // ...
    });

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):

$lock = Cache::lock('generate-reports', CarbonInterval::minutes(5));

$lock->acquire();

Reports::where('status', 'pending')
    ->lazy()
    ->withHeartbeat(CarbonInterval::minutes(4), $lock->reacquire(...))
    ->each($this->generateReport(...));

$lock->release();

Note: the locking logic above is simplified for clarity. It's essentially pseudo-code.

@shaedrich
Copy link
Contributor

I actually wasn't able to guess from the method name what exactly it would do. Maybe something like withInterval() would be more intuitive, named after JavaScript's setInterval()

@JosephSilber
Copy link
Contributor Author

This doesn't run at an interval on its own. It only triggers as you pull values from the list, indicating that it's still running.

heartbeat is the standard term for this.

https://en.wikipedia.org/wiki/Heartbeat_(computing)

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