ProcessCallLimitedAPI.php
3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace App\Jobs;
use App\Service\Contract\APIRequest;
use App\Service\Limits;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class ProcessCallLimitedAPI implements ShouldQueue//, ShouldBeUnique
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* @var APIRequest
*/
private $api;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(APIRequest $api)
{
$this->api = $api;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$this->api->getToken()->refresh();
$limits = Limits::getInstance($this->api->getToken());
//получаем сколько объектов можем обработать
$objects = $limits->countObjectsLimit($this->api);
if (!$objects) {
//нет свободных баллов, замораживаем до следующего часа
Log::debug("Нет свободных баллов, замораживаем до следующего часа. token_id {$this->api->getToken()->getKey()}, объектов {$this->api->getObjectsCount()} {$this->api->getService()} {$this->api->getMethod()}");
$this->reRunHour();
return;
}
try{
//резервируем на это количетсво
$limit = $limits->getSpent($objects, $this->api);
Log::debug("Резервируем баллы {$limit}, token_id {$this->api->getToken()->getKey()}, на объекты {$this->api->getObjectsCount()} {$this->api->getService()} {$this->api->getMethod()}");
$limitId = $limits->doRezerv($this->api, $objects);
}catch(\Exception $e){
//нет свободных баллов, замораживаем до следующего часа
Log::debug('');
Log::debug('Нет свободных баллов Exception, замораживаем до следующего часа');
Log::debug($e->getMessage());
Log::debug('');
$this->reRunHour();
return;
}
//вызываем очередь получения данных от АПИ
//туда передаем сохраненный лимит
//там уже либо будет удален, если не удастся выполнить запрос
//либо обновятся данные
dispatch(new ProcessCallAPI($limitId, $this->api))->onQueue('api');
}
// /**
// * The unique ID of the job.
// *
// * @return string
// */
// public function uniqueId()
// {
// return $this->api->getToken()->id;
// }
private function reRunHour(){
$process = new ProcessCallLimitedAPI($this->api);
dispatch( $process )
->delay(now()->addMinutes(60-date("i")))
->onQueue('limits');
}
}