2017-07-18 25 views
2

我正在使用laravel v5.3.30,并且我有复杂的工作,正在执行请求并将数据导入系统。如果我多次尝试多次尝试,那么我的数据库就会增加两倍甚至三倍的插入次数。这是工作的样子概述:Laravel工作标记为失败,例外说太多的尝试,但工作成功?

<?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行。

所以我不明白工作如何失败,即使我正在处理例外。有人有什么主意吗?

回答

0

推送作业排队没有工人和处理这个工作manualy