Skip to content

Conversation

@kamilkozak
Copy link
Contributor

@kamilkozak kamilkozak commented Sep 9, 2025

takeUntilTimeout() — optional callback support

This PR adds an optional $callback parameter to takeUntilTimeout().
The callback is executed when the timeout is reached and receives the last processed item and key (or null, null if the timeout occurs before iteration).

Benefits

  • Resumable processing: store the last processed key and continue from there on the next run.
  • Queue integration: throw an exception in the callback, catch it in a Job, and re-release the job back to the queue.

Examples

$users = User::cursor()->takeUntilTimeout(now()->addMinutes(14), function ($user, $key) {
    Cache::put('last_user_id', $user?->id);
});

foreach ($users as $user) {
    // ...
}
$users = User::cursor()->takeUntilTimeout(now()->addMinutes(14), function () {
    throw TimeboxExceededException::forReleaseJob();
});

Compatibility

  • Callback is optional (null by default).
  • Existing behavior is unchanged when no callback is passed.

The takeUntilTimeout method now accepts an optional callback that is invoked when the timeout is reached, providing the last yielded value and key. Tests and type assertions have been updated to cover the new callback functionality.
@taylorotwell taylorotwell merged commit 3be63bb into laravel:12.x Sep 10, 2025
63 checks passed
tegos pushed a commit to tegos/laravel-framework that referenced this pull request Sep 28, 2025
…6981)

* Add callback support to takeUntilTimeout in LazyCollection

The takeUntilTimeout method now accepts an optional callback that is invoked when the timeout is reached, providing the last yielded value and key. Tests and type assertions have been updated to cover the new callback functionality.

* Fix indentation

* Simplify initialization of `$timedOutWith` in SupportLazyCollectionTest
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