【发布时间】:2021-01-27 21:19:35
【问题描述】:
我有一个简单的 laravel 作业批处理,我的问题是当批处理中的一个队列失败并抛出异常时,即使我添加了取消方法,它也不会停止或取消批处理的执行,仍在处理下一个队列。
这是我的句柄和失败的方法
public function handle()
{
if ($this->batch()->cancelled()) {
return;
}
$csv_data = array_map('str_getcsv', file($this->chunk_directory));
foreach ($csv_data as $key => $row) {
if(count($this->header) != count($row)) {
$data = array_combine($this->header, $row);
} else {
$this->batch()->cancel();
throw new Exception("Your file doesn't match the number of headers like your product header");
}
}
}
public function failed(\Exception $e = null)
{
broadcast(new QueueProcessing("failed", BatchHelpers::getBatch($this->batch()->id)));
}
这是我的命令行结果
[2021-01-11 01:17:57][637] Processing: App\Jobs\ImportItemFile
[2021-01-11 01:17:57][637] Failed: App\Jobs\ImportItemFile
[2021-01-11 01:17:58][638] Processing: App\Jobs\ImportItemFile
[2021-01-11 01:17:58][638] Processed: App\Jobs\ImportItemFile
【问题讨论】:
-
我不熟悉批处理,但我建议您在取消批处理后尽量不要抛出异常,以防在作业终止时发生取消
-
或者也许我在取消批处理之前把抛出的异常,我这样做是为了记录队列异常,这样我就可以在仪表板上显示每个队列失败的描述,但还是一样