-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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] Optimize PendingBatch@ensureJobIsBatchable
#54485
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,13 @@ class PendingBatch | |
*/ | ||
public $options = []; | ||
|
||
/** | ||
* Batches that have been checked to contain the Batchable trait. | ||
* | ||
* @var array<class-string, bool> | ||
*/ | ||
protected static $batchableClasses = []; | ||
|
||
/** | ||
* Create a new pending batch instance. | ||
* | ||
|
@@ -92,14 +99,16 @@ protected function ensureJobIsBatchable(object|array $job): void | |
{ | ||
foreach (Arr::wrap($job) as $job) { | ||
if ($job instanceof PendingBatch) { | ||
$this->ensureJobIsBatchable($job->jobs->all()); | ||
|
||
return; | ||
} | ||
|
||
if (! in_array(Batchable::class, class_uses_recursive($job))) { | ||
if (! (static::$batchableClasses[$job::class] ?? false) && ! in_array(Batchable::class, class_uses_recursive($job))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While it might not possible with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something along the lines of this: 454eb0d...0000530 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or 454eb0d...1990987 (1990987) |
||
static::$batchableClasses[$job::class] = false; | ||
|
||
throw new RuntimeException(sprintf('Attempted to batch job [%s], but it does not use the Batchable trait.', $job::class)); | ||
} | ||
|
||
static::$batchableClasses[$job::class] = true; | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@josepostiga Do you think it make sense to remove this check here? If it's a PendingBatch, that all of the jobs added to it should've had this method called for each job.
There is the case where a user creates a PendingBatch and then modifies the
$jobs
Collection manually ($pendingBatch->jobs->push(new SomeJobThatIsNotDispatchable)
), but if we want to guard against this, we would have to ensure this check happens before we write to the repository, probably inPendingBatch@store()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh, that actually makes sense, so I agree we could drop the check here and assume jobs have been sanitized before.
That case you refer, though, worries me a little. It leaves a "known" inconsistency in the framework. Do you know how hard would it be to re-architect it so that we can also cover for that? I can look into it later, though.