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

[11.x] Ensure batched jobs are actually batchable #54442

Open
wants to merge 4 commits into
base: 11.x
Choose a base branch
from
Open
Show file tree
Hide file tree
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
25 changes: 19 additions & 6 deletions src/Illuminate/Bus/PendingBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ class PendingBatch
/**
* Create a new pending batch instance.
*
* @param \Illuminate\Contracts\Container\Container $container
* @param \Illuminate\Support\Collection $jobs
* @return void
*/
public function __construct(Container $container, Collection $jobs)
{
$this->container = $container;
$this->jobs = $jobs;
$this->jobs = $jobs->each(function (object|array $job) {
$this->checkJobIsBatchable($job);
});
}

/**
Expand All @@ -70,6 +70,7 @@ public function add($jobs)
$jobs = is_iterable($jobs) ? $jobs : Arr::wrap($jobs);

foreach ($jobs as $job) {
$this->checkJobIsBatchable($job);
$this->jobs->push($job);
}

Expand Down Expand Up @@ -227,7 +228,6 @@ public function allowsFailures()
/**
* Set the name for the batch.
*
* @param string $name
* @return $this
*/
public function name(string $name)
Expand All @@ -240,7 +240,6 @@ public function name(string $name)
/**
* Specify the queue connection that the batched jobs should run on.
*
* @param string $connection
* @return $this
*/
public function onConnection(string $connection)
Expand Down Expand Up @@ -286,7 +285,6 @@ public function queue()
/**
* Add additional data into the batch's options array.
*
* @param string $key
* @param mixed $value
* @return $this
*/
Expand Down Expand Up @@ -414,4 +412,19 @@ protected function store($repository)

return $batch;
}

private function checkJobIsBatchable(object|array $job): void
{
foreach (Arr::wrap($job) as $job) {
if ($job instanceof PendingBatch) {
$this->checkJobIsBatchable($job->jobs->all());

return;
}

if (! in_array(Batchable::class, class_uses_recursive($job))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it wouldn't be useful to cache the job's class so you don't have to call class_uses_recursive for multiple instances of the same class.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it's not possible with class_uses_recursive, this could be taken one step further, by implementing something similar to class_uses_recursive: Caching all classes in the recursive chain 🤔

throw new \RuntimeException(sprintf('Job %s must use Batchable trait', $job::class));
}
}
}
}
9 changes: 9 additions & 0 deletions tests/Bus/BusPendingBatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,13 @@ public function test_batch_before_event_is_called()

$this->assertTrue($beforeCalled);
}

public function test_it_throws_exception_if_batched_job_is_not_batchable(): void
{
$nonBatchableJob = new class {};

$this->expectException(RuntimeException::class);

new PendingBatch(new Container, new Collection([$nonBatchableJob]));
}
}