简体   繁体   English

如何为作业批次中的每个失败作业调用“catch”闭包 Laravel 8

[英]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. Laravel 8 引入了作业批处理,它允许批量执行作业,并对批处理完成和失败执行操作。 When a job within batch fails, the catch callback is executed and the whole batch is marked as "canceled" according to the documentation .当批处理中的作业失败时,将执行catch回调并根据文档将整个批处理标记为“已取消”。 In case you don't want the batch to cancel (on the first failure) you can append the method allowFailures() while dispatching the batch:如果您不希望批处理取消(在第一次失败时),您可以在调度批处理时使用方法allowFailures()

$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?它按预期工作,但是我想知道是否可以为每个失败的工作调用catch关闭? (at this case with allowFailures ) (在这种情况下使用allowFailures

You can use number of attemtps in Jobs for finding failed job您可以使用 Jobs 中的尝试次数来查找失败的作业

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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