fix optimize syncronize

This commit is contained in:
arifal
2025-03-14 19:10:28 +07:00
parent e2c26e0eff
commit 2e385f80cd
5 changed files with 569 additions and 54 deletions

View File

@@ -0,0 +1,62 @@
<?php
namespace App\Services;
use App\Models\GlobalSetting;
use GuzzleHttp\Client;
use Exception;
use Illuminate\Support\Facades\Log;
class ServicePbgTask
{
private $client;
private $simbg_host;
private $fetch_per_page;
private $pbg_task_url;
private $service_token;
public function __construct(Client $client, ServiceTokenSIMBG $service_token)
{
$settings = GlobalSetting::whereIn('key', ['SIMBG_HOST', 'FETCH_PER_PAGE'])
->pluck('value', 'key');
$this->simbg_host = trim((string) ($settings['SIMBG_HOST'] ?? ""));
// $this->fetch_per_page = trim((string) ($settings['FETCH_PER_PAGE'] ?? "10"));
$this->fetch_per_page = 10;
$this->client = $client;
$this->service_token = $service_token;
$this->pbg_task_url = "{$this->simbg_host}/api/pbg/v1/list/?page=1&size={$this->fetch_per_page}&sort=ASC";
}
public function run_service()
{
$this->fetch_pbg_task();
}
private function fetch_pbg_task()
{
try {
$token = $this->service_token->get_token();
$options = [
'headers' => [
'Authorization' => "Bearer {$token['access']}",
'Accept' => 'application/json'
]
];
$fetch_data = $this->client->get($this->pbg_task_url, $options);
$response_status_code = $fetch_data->getStatusCode();
$response = json_decode($fetch_data->getBody()->getContents(), true);
$data = $response['data'];
$total_page = $response['total_page'];
Log::info("Successfully fetched PBG tasks", ['data' => $data]);
Log::info("Status code response", ['code' => $response_status_code]);
Log::info("Success", ['total_page' => $total_page]);
return $response;
} catch (Exception $e) {
Log::error("Failed to fetch PBG tasks", ['error' => $e->getMessage()]);
return null; // Return null if an error occurs
}
}
}