【发布时间】:2017-12-23 01:13:27
【问题描述】:
我正在使用 laravel v5.3.30 并且我的工作很复杂,即处理大量请求并将数据导入系统。如果我多次尝试多个工作人员,我会在我的数据库中获得两倍甚至三倍的插入。这是这份工作的概述:
<?php
namespace Uppdragshuset\AO\Tenant\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Uppdragshuset\AO\Tenant\Import\ImportHandler;
class ImportPatentCreateCaseJob implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
protected $number;
protected $import;
protected $workflow_id;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($number, $import, $workflow_id)
{
$this->number = $number;
$this->import = $import;
$this->workflow_id = $workflow_id;
$this->onQueue('import');
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$importHandler = new ImportHandler();
try {
DB::beginTransaction();
$patent = $importHandler->checkIfPatentExists($this->number['number']);
if( ! $patent){
$patent = $importHandler->handlePatentData($this->number);
}
if( ! $importHandler->checkIfCaseExists($patent->id, $this->workflow_id)){
$task_id = $importHandler->prepareWorkflowAndGetTaskId($this->workflow_id);
$importHandler->createCase($this->workflow_id, $task_id, $patent->id);
}
$this->import->update([
'status' => 'COMPLETED'
]);
DB::commit();
} catch (\Exception $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
DB::rollBack();
$this->import->update([
'status' => 'FAILED'
]);
}
$importHandler->markBatchAsCompleted($this->import);
}
}
我正在检查数据是否已经存在,如果存在,则不应再次导入。我什至将整个代码包装在一个 try catch 语句中,所以即使出现故障,它也会记录它并且作业会运行良好。
当我尝试使用 tries=1 时,创建了 55 个工作,其中 7 个失败,但我的失败工作表显示其中有 30 行。
因此,即使我正在处理异常,我也不明白作业是如何失败的。有人知道吗?
【问题讨论】: