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] Optimize PendingBatch@ensureJobIsBatchable #54485

Merged
merged 3 commits into from
Feb 6, 2025

Conversation

cosmastech
Copy link
Contributor

Building on top of #54442

We can memoize the classes which use Batchable. I imagine there are more than a few cases where a developer is chaining/batching hundreds of the same job (just for different users or something), so we don't need to recursively check the traits of the same class repeatedly.

What about a long-running process like Octane? Don't you need to clear the cache on application tear down? No, because if the classes change (and now do use Batchable where they previously hadn't) this would require a server restart, which would be clearing them anyways. As an added bonus, we've memoized the failures as well for this use case.

Copy link

github-actions bot commented Feb 5, 2025

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

return;
}

if (! in_array(Batchable::class, class_uses_recursive($job))) {
if (! (static::$batchableClasses[$job::class] ?? false) && ! in_array(Batchable::class, class_uses_recursive($job))) {
Copy link
Contributor

@shaedrich shaedrich Feb 5, 2025

Choose a reason for hiding this comment

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

While it might 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 🤔

Copy link
Contributor

@shaedrich shaedrich Feb 8, 2025

Choose a reason for hiding this comment

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

Something along the lines of this: 454eb0d...0000530

Copy link
Contributor

@shaedrich shaedrich Feb 9, 2025

Choose a reason for hiding this comment

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

@@ -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());
Copy link
Contributor Author

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 in PendingBatch@store().

Copy link
Contributor

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.

@cosmastech cosmastech force-pushed the pending-batch-improvements branch from 0633f2a to 8851991 Compare February 6, 2025 02:19
@cosmastech cosmastech marked this pull request as ready for review February 6, 2025 22:35
@taylorotwell taylorotwell merged commit 917a3fe into laravel:11.x Feb 6, 2025
40 checks passed
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.

4 participants