I am working with laravel v5.3.30 and I have complex job which is doing guzzle requests and importing data into the system. If I multiple workers with multiple tries I am getting double, even triple insertions into my database. This is what the job looks like overview:
<?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);
}
}
I am checking if the data already exists and if it does it should not import it again. I have even wrapped the whole code in a try catch statement so even if something fails, it would log it and the job would run fine.
When I tried with a tries=1
, 55 jobs were created, out of which 7 failed but my failed-jobs table shows me 30 rows in there.
So I don't understand how jobs are failing even though I am handling the exceptions. Does anyone have any idea?
push job to queue without workers and process this job manualy