first commit
This commit is contained in:
69
laravel/app/Jobs/WbSyncJob.php
Normal file
69
laravel/app/Jobs/WbSyncJob.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Contracts\WbSyncFactoryInterface;
|
||||
use App\Contracts\WbSyncManagerInterface;
|
||||
use App\DTO\WbRequestDTO;
|
||||
use App\Enums\WbEndpoint;
|
||||
use Illuminate\Bus\Batchable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Queue\Queueable;
|
||||
use Log;
|
||||
|
||||
final class WbSyncJob implements ShouldQueue
|
||||
{
|
||||
use Queueable, Batchable;
|
||||
|
||||
public int $tries = 3;
|
||||
public array $backoff = [10, 30, 60];
|
||||
|
||||
public function __construct(
|
||||
private readonly WbEndpoint $endpoint,
|
||||
private readonly string $dateFrom,
|
||||
private readonly ?string $dateTo,
|
||||
private readonly int $page,
|
||||
) {
|
||||
}
|
||||
|
||||
public function handle(
|
||||
WbSyncFactoryInterface $factory,
|
||||
WbSyncManagerInterface $manager
|
||||
): void {
|
||||
$service = $factory->make($this->endpoint);
|
||||
|
||||
$dto = new WbRequestDTO(
|
||||
endpoint: $this->endpoint,
|
||||
dateFrom: $this->dateFrom,
|
||||
dateTo: $this->dateTo,
|
||||
page: $this->page,
|
||||
);
|
||||
|
||||
$response = $service->sync($dto);
|
||||
|
||||
$service->save($response['data']);
|
||||
|
||||
if ($this->page === 1) {
|
||||
$lastPage = (int)data_get($response, 'meta.last_page', 1);
|
||||
$manager->handleFirstPage(
|
||||
$this->endpoint,
|
||||
$this->dateFrom,
|
||||
$this->dateTo,
|
||||
$lastPage,
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
Log::info(
|
||||
"WB sync page $this->page for {$this->endpoint->name} date form: $this->dateFrom, to: $this->dateTo processed"
|
||||
);
|
||||
|
||||
$manager->incrementProgress(
|
||||
$this->endpoint,
|
||||
$this->dateFrom,
|
||||
$this->dateTo
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user