简体   繁体   中英

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 )

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM