2021-08-23 19:38:16 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
|
|
use App\Models\Service;
|
|
|
|
|
use App\Models\ServiceStat;
|
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
2021-10-15 18:36:02 +02:00
|
|
|
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
2021-08-23 19:38:16 +02:00
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
|
|
|
|
class SetServiceStats implements ShouldQueue
|
|
|
|
|
{
|
|
|
|
|
use Dispatchable;
|
|
|
|
|
use InteractsWithQueue;
|
|
|
|
|
use Queueable;
|
|
|
|
|
use SerializesModels;
|
|
|
|
|
|
2021-11-05 16:22:36 +01:00
|
|
|
private const KEY = '2202020';
|
|
|
|
|
|
2021-08-23 19:38:16 +02:00
|
|
|
/** @var \App\Models\Service */
|
|
|
|
|
protected $service;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new job instance.
|
|
|
|
|
*
|
2023-06-23 17:18:59 +02:00
|
|
|
* @param \App\Models\Service $service
|
2021-08-23 19:38:16 +02:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(Service $service)
|
|
|
|
|
{
|
|
|
|
|
$this->service = $service;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the job.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2023-09-06 17:24:53 +02:00
|
|
|
public function handle(): void
|
2021-08-23 19:38:16 +02:00
|
|
|
{
|
2021-10-29 17:32:30 +02:00
|
|
|
$serviceStat = ServiceStat::firstOrNew([
|
|
|
|
|
'date' => date('Y-m-d'),
|
|
|
|
|
'service_id' => $this->service->id,
|
|
|
|
|
'brand_id' => $this->service->brand->id,
|
|
|
|
|
]);
|
2021-08-23 19:38:16 +02:00
|
|
|
if (!$serviceStat->exists) {
|
|
|
|
|
$serviceStat->service()->associate($this->service);
|
|
|
|
|
$serviceStat->brand()->associate($this->service->brand);
|
|
|
|
|
}
|
|
|
|
|
$serviceStat->nb_calls += 1;
|
|
|
|
|
$serviceStat->saveOrFail();
|
|
|
|
|
}
|
2021-10-15 18:36:02 +02:00
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Determine the time at which the job should timeout.
|
|
|
|
|
*
|
|
|
|
|
* @return \DateTime
|
|
|
|
|
*/
|
2023-09-06 17:24:53 +02:00
|
|
|
public function retryUntil(): \DateTime
|
2021-10-29 17:32:30 +02:00
|
|
|
{
|
|
|
|
|
return now()->addMinutes(rand(0, 360));
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Get the middleware the job should pass through.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2023-09-06 17:24:53 +02:00
|
|
|
public function middleware(): array
|
2021-10-15 18:36:02 +02:00
|
|
|
{
|
2021-10-29 17:32:30 +02:00
|
|
|
return [
|
2021-11-05 16:22:36 +01:00
|
|
|
(new WithoutOverlapping(self::KEY))
|
2021-10-29 17:32:30 +02:00
|
|
|
->releaseAfter(rand(90, 180))
|
|
|
|
|
];
|
2021-10-15 18:36:02 +02:00
|
|
|
}
|
2021-08-23 19:38:16 +02:00
|
|
|
}
|