Skip to content

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 envision a possible future JobContext object that would be passed to the callback providing more job specific detail, making this potentially more useful.

💁🏼‍♂️ The author is available for hire -- inquire at [email protected].

@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 5 times, most recently 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?

@yitzwillroth
Copy link
Contributor Author

It's like catch(), except that it continues, and also progress(), except that it fires on failure rather than success.

@yitzwillroth
Copy link
Contributor Author

@taylorotwell would love to add tests to this if there's a general inclination towards approval.

@yitzwillroth yitzwillroth changed the title [12.x] :feat: Add Job Failure Callbacks to Batch [12.x] Batch Job Failure Callbacks Support Jul 7, 2025
@taylorotwell
Copy link
Member

@yitzwillroth yeah I think fine to add tests here.

@taylorotwell taylorotwell marked this pull request as draft August 8, 2025 14:39
@shaedrich
Copy link
Contributor

Would it make sense to also have a mechanism to only invoke on errors when the batch is completed?

@yitzwillroth yitzwillroth force-pushed the add-job-batch-enhancements branch from 0791d2e to 0fa6fb6 Compare August 25, 2025 16:55
@yitzwillroth yitzwillroth marked this pull request as ready for review August 25, 2025 16:58
@yitzwillroth
Copy link
Contributor Author

@taylorotwell Tests added as requested.

@yitzwillroth yitzwillroth force-pushed the add-job-batch-enhancements branch from cbeba75 to d2c6d4d Compare August 27, 2025 13:42
@crynobone crynobone closed this Sep 11, 2025
@crynobone crynobone reopened this Sep 11, 2025
@taylorotwell taylorotwell merged commit 9f2baf5 into laravel:12.x Sep 24, 2025
63 checks passed
@taylorotwell
Copy link
Member

Thanks!

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.

5 participants