Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[12.x] :feat: Add Job Failure Callbacks to Batch #55916

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

Open
wants to merge 1 commit into
base: 12.x
Choose a base branch
Loading
from

Conversation

yitzwillroth
Copy link
Contributor

@yitzwillroth yitzwillroth commented Jun 3, 2025

Functional Enhancement

The functional change is in PendingBatch::allowFailures(), which now accepts:

  • A boolean value (original behavior)
  • A callable/Closure
  • An array of callables/Closures

This allows the registration of callbacks that will be executed when a job fails, providing more granular control over failure handling in batch processing. The Batch class has been updated with corresponding methods to handle these new failure callbacks.

// New usage example:
$batch->allowFailures(function ($batch, $exception) {
    // Handle individual job failure
    Log::error("Job failed in batch {$batch->id}: {$exception->getMessage()}");
});

Of course, $batch->allowFailures() and $batch->allowFailures($boolean) still work as before.

Additional Changes

  • Refactored callback invocation into a centralized invokeCallbacks method in the Batch class

Notes

  • I debated going with $batch->failure($callback) instead of dual-purposing $batch->allowFailures(), but went with the latter since the former would require either a call to allowFailures() anyway, setting this option implicitly in the failure() implementation, or reworking the logic of allowsFailures() to also return true when failure callbacks are present. I'm open to reworking this in any of those directions.
  • I'll add tests once the prior question is resolved.
  • I envision a possible future JobContext object that would be passed to the callback providing more job specific detail, making this potentially more useful.

@yitzwillroth yitzwillroth force-pushed the add-job-batch-enhancements branch from a7f2f49 to 2f3c496 Compare June 3, 2025 14:00
@yitzwillroth yitzwillroth marked this pull request as draft June 3, 2025 14:02
@yitzwillroth yitzwillroth marked this pull request as ready for review June 3, 2025 17:03
@taylorotwell
Copy link
Member

Hey @coderabbi - is it possible to remove the formatting changes... just makes the diff easier for me to review quickly.

@taylorotwell taylorotwell marked this pull request as draft June 4, 2025 13:50
@coderabbi
Copy link
Contributor

On it!

@yitzwillroth yitzwillroth force-pushed the add-job-batch-enhancements branch 4 times, most recently from 9c2e8a9 to b7ba1e1 Compare June 4, 2025 14:29
@yitzwillroth yitzwillroth force-pushed the add-job-batch-enhancements branch from b7ba1e1 to 0791d2e Compare June 4, 2025 14:30
@yitzwillroth yitzwillroth marked this pull request as ready for review June 4, 2025 14:36
@yitzwillroth
Copy link
Contributor Author

Done!

@taylorotwell
Copy link
Member

@coderabbi so is this similar to catch, but it is invoked for every job failure, not just the first one?

@coderabbi
Copy link
Contributor

coderabbi commented Jun 4, 2025

@taylorotwell

Exactly.

As I noted in the description, probably more useful when paired with a JobContext passed to the callback with additional job-specific details, but... babysteps (though if you're inclined to accept this, can certainly incorporate that here).

@coderabbi so is this similar to catch, but it is invoked for every job failure, not just the first one?

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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.