Refactor for unique jobs

This commit is contained in:
David Bomba 2022-12-19 16:31:23 +11:00
parent 9cb1e2b0b4
commit db89751ebf

View File

@ -21,6 +21,7 @@ use App\Models\Invoice;
use App\Services\Bank\BankService; use App\Services\Bank\BankService;
use App\Utils\Traits\GeneratesCounter; use App\Utils\Traits\GeneratesCounter;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
@ -29,7 +30,7 @@ use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
class BankMatchingService implements ShouldQueue class BankMatchingService implements ShouldQueue, ShouldBeUnique
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
@ -37,13 +38,10 @@ class BankMatchingService implements ShouldQueue
protected $db; protected $db;
protected $middleware_key;
public function __construct($company_id, $db) public function __construct($company_id, $db)
{ {
$this->company_id = $company_id; $this->company_id = $company_id;
$this->db = $db; $this->db = $db;
$this->middleware_key = "bank_match_rate:{$this->company_id}";
} }
public function handle() :void public function handle() :void
@ -62,8 +60,14 @@ class BankMatchingService implements ShouldQueue
} }
public function middleware() /**
* The unique ID of the job.
*
* @return string
*/
public function uniqueId()
{ {
return [new WithoutOverlapping($this->middleware_key)]; return (string)$this->company_id;
} }
} }