ProcessCallLimitedAPI.php 3.18 KB
<?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');
    }
}