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] Makes it easy to add additional options to PendingBatch #40333

Merged
merged 3 commits into from
Jan 10, 2022
Merged

[8.x] Makes it easy to add additional options to PendingBatch #40333

merged 3 commits into from
Jan 10, 2022

Conversation

orkhanahmadov
Copy link
Contributor

Useful when extra identifier or parameter needed on Batch.

Use-case:
When a batch is dispatched by a user we can set user_id as an extra option. When authorizing the batch model we can use the value of this option.

@orkhanahmadov orkhanahmadov changed the title [8.x] makes it possible to add additional options to PendingBatch [8.x] Makes possible to add additional options to PendingBatch Jan 10, 2022
@orkhanahmadov orkhanahmadov changed the title [8.x] Makes possible to add additional options to PendingBatch [8.x] Makes possible to add additional options to PendingBatch Jan 10, 2022
@orkhanahmadov orkhanahmadov changed the title [8.x] Makes possible to add additional options to PendingBatch [8.x] Makes it easy to add additional options to PendingBatch Jan 10, 2022
@taylorotwell
Copy link
Member

Can you show me how you would actually use this in your own application? And where you would read the option back out?

@orkhanahmadov
Copy link
Contributor Author

orkhanahmadov commented Jan 10, 2022

In our application when a batch is dispatched we assign it to the user, then broadcast batch progress from the broadcasting channel. When the user tries to join the broadcasting channel we authorize if the user "owns" the batch:

public function join(User $user, string $batchId): bool
{
    $batch = Bus::findBatch($batchId);

    return $batch?->options['user_id'] == $user->id;
}

Reading options is easy, since it is a public property on \Illuminate\Bus\Batch. But setting additional options isn't straightforward. Currently we do it like this:

$batch = new PendingBatch($this->container, $jobs);
$batch->options = ['user_id' => $user->id];

$batch->then(fn (Batch $batch) => ...)->dispatch();

Having addOption method will simplify the code to:

Bus::batch($jobs)
    ->then(fn (Batch $batch) => ...)
    ->addOption('user_id', $user->id)
    ->dispatch();

@taylorotwell taylorotwell merged commit 2ce3e56 into laravel:8.x Jan 10, 2022
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