Skip to content
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

[8.x] Document takeUntilTimeout #6623

Merged
merged 1 commit into from
Dec 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -2943,6 +2943,37 @@ Almost all methods available on the `Collection` class are also available on the

In addition to the methods defined in the `Enumerable` contract, the `LazyCollection` class contains the following methods:

<a name="method-takeUntilTimeout"></a>
#### `takeUntilTimeout()` {#collection-method}

The `takeUntilTimeout` method returns a new lazy collection that will enumerate values until the specified time, and will then stop enumerating:

$lazyCollection = LazyCollection::times(INF)
->takeUntilTimeout(now()->addMinute());

$lazyCollection->each(function ($number) {
dump($number);

sleep(1);
});

// 1
// 2
// ...
// 58
// 59

This is especially useful for [Laravel Vapor](https://vapor.laravel.com/) apps, since AWS will kill any long-running processes after 15 minutes.

Imagine a system that submits invoices from the DB, one by one. You could set up a schedule that runs every 15 minutes, and only processes invoices for a maximum of 14 minutes:

Invoice::pending()->cursor()
->takeUntilTimeout(
Carbon::createFromTimestamp(LARAVEL_START)->add(14, 'minutes')
)
->each(fn ($invoice) => $invoice->submit());


<a name="method-tapEach"></a>
#### `tapEach()` {#collection-method}

Expand Down