80 lines
1.8 KiB
PHP
80 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\Api\ApiToken;
|
|
use App\Models\Api\ApiTokenStat;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
/**
|
|
* @codeCoverageIgnore
|
|
*/
|
|
class SetApiTokenStats implements ShouldQueue
|
|
{
|
|
use Dispatchable;
|
|
use InteractsWithQueue;
|
|
use Queueable;
|
|
use SerializesModels;
|
|
|
|
private const KEY = '2202020';
|
|
|
|
/** @var \App\Models\Api\ApiToken */
|
|
protected $apiToken;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @param \App\Models\Api\ApiToken $apiToken
|
|
* @return void
|
|
*/
|
|
public function __construct(ApiToken $apiToken)
|
|
{
|
|
$this->apiToken = $apiToken;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
$date = date('Y-m-d');
|
|
$apiTokenStat = ApiTokenStat::where('date', $date)
|
|
->whereHas('apiToken', function ($q) {
|
|
$q->where('id', $this->apiToken->id);
|
|
})->firstOrNew();
|
|
$apiTokenStat->date = $date;
|
|
$apiTokenStat->nb_calls += 1;
|
|
$apiTokenStat->apiToken()->associate($this->apiToken);
|
|
$apiTokenStat->saveOrFail();
|
|
}
|
|
|
|
/**
|
|
* Determine the time at which the job should timeout.
|
|
*
|
|
* @return \DateTime
|
|
*/
|
|
public function retryUntil()
|
|
{
|
|
return now()->addMinutes(rand(0, 360));
|
|
}
|
|
|
|
/**
|
|
* Get the middleware the job should pass through.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function middleware()
|
|
{
|
|
return [
|
|
(new WithoutOverlapping(self::KEY))
|
|
->releaseAfter(rand(90, 180))
|
|
];
|
|
}
|
|
}
|