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

Conversation

josepostiga
Copy link

@josepostiga josepostiga commented Feb 2, 2025

Although the documentation for batch jobs clearly states that batched jobs should use the Batchable trait, there's not much that alerts or prevents the dev from wrongly trying to batch jobs that don't implement it, resulting in runtime problems. Even if we use the assertBatched test, tests pass even with invalid jobs supplied. I was a victim of this. (I know, it might also be a skill issue from my side, for forgetting to use the trait...)

This PR adds a check to batched jobs to ensure that the jobs use the required Batchable trait, throwing a RuntimeException if a job is found in a given list when calling Bus::batch or if supplied to the add method of a PendingBatch instance.

@josepostiga josepostiga marked this pull request as draft February 2, 2025 18:14
@josepostiga
Copy link
Author

josepostiga commented Feb 2, 2025

Looks like I missed a few things in this implementation. I'm looking into it... Converted the PR to draft, in the meantime.

Edit: I think we're good for review, now.

@josepostiga josepostiga marked this pull request as ready for review February 2, 2025 18:34
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 🤔

@shaedrich
Copy link
Contributor

There might be cases, even though, it might be rare, where the trait isn't even used for some reason, but the functionality is implemented. This wouldn't work anymore. This could possibly be avoided by using an interface 🤔

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.

3 participants