Yerke
Yerke

Reputation: 2215

How to call `catch` closure for every failed job in job batches Laravel 8

Laravel 8 introduced Job Batching, which allows to execute jobs in batches and perform actions on batch completion and failure. When a job within batch fails, the catch callback is executed and the whole batch is marked as "canceled" according to the documentation. In case you don't want the batch to cancel (on the first failure) you can append the method allowFailures() while dispatching the batch:

$batch = Bus::batch([
    // ...
])->then(function (Batch $batch) {
    // All jobs completed successfully...
})->catch(function (Batch $batch, Throwable $e) {
    // First batch job failure detected...
    
    // TODO: call this for every failed job

})->allowFailures()->dispatch();

It works as expected, however I wonder whether it's possible to call catch closure for every failed job? (at this case with allowFailures)

Upvotes: 2

Views: 1174

Answers (1)

Sergey Pavlov
Sergey Pavlov

Reputation: 39

You can use number of attemtps in Jobs for finding failed job

  if ($this->attempts() > 1) {
  // your code
  }

Upvotes: 0

Related Questions