pluck('value', 'key'); $this->simbg_host = trim((string) ($settings['SIMBG_HOST'] ?? "")); $this->fetch_per_page = trim((string) ($settings['FETCH_PER_PAGE'] ?? "10")); $this->client = $client; $this->service_token = $service_token; $auth_data = $this->service_token->get_token(); $this->user_token = $auth_data['access']; $this->user_refresh_token = $auth_data['refresh']; } public function run_service($retry_uuid = null) { try { $pbg_tasks = PbgTask::orderBy('id')->get(); $start = false; foreach ($pbg_tasks as $pbg_task) { if($retry_uuid){ if($pbg_task->uuid === $retry_uuid){ $start = true; } if(!$start){ continue; } } try{ $this->current_uuid = $pbg_task->uuid; $this->scraping_task_assignments($pbg_task->uuid); $this->scraping_task_retributions($pbg_task->uuid); $this->scraping_task_integrations($pbg_task->uuid); }catch(\Exception $e){ Log::error("Failed on UUID: {$this->current_uuid}, Error: " . $e->getMessage()); throw $e; } } } catch (\Exception $e) { Log::error("Failed to syncronize: " . $e->getMessage()); throw $e; } } public function getFailedUUID(){ return $this->current_uuid; } private function scraping_task_assignments($uuid) { $url = "{$this->simbg_host}/api/pbg/v1/list-tim-penilai/{$uuid}/?page=1&size=10"; $options = [ 'headers' => [ 'Authorization' => "Bearer {$this->user_token}", 'Content-Type' => 'application/json' ] ]; $maxRetries = 3; $initialDelay = 1; $retriedAfter401 = false; for ($retryCount = 0; $retryCount < $maxRetries; $retryCount++) { try { $response = $this->client->get($url, $options); $responseData = json_decode($response->getBody()->getContents(), true); if (empty($responseData['data']) || !is_array($responseData['data'])) { return true; } $task_assignments = []; foreach ($responseData['data'] as $data) { $task_assignments[] = [ 'pbg_task_uid' => $uuid, 'user_id' => $data['user_id'] ?? null, 'name' => $data['name'] ?? null, 'username' => $data['username'] ?? null, 'email' => $data['email'] ?? null, 'phone_number' => $data['phone_number'] ?? null, 'role' => $data['role'] ?? null, 'role_name' => $data['role_name'] ?? null, 'is_active' => $data['is_active'] ?? false, 'file' => !empty($data['file']) ? json_encode($data['file']) : null, 'expertise' => !empty($data['expertise']) ? json_encode($data['expertise']) : null, 'experience' => !empty($data['experience']) ? json_encode($data['experience']) : null, 'is_verif' => $data['is_verif'] ?? false, 'uid' => $data['uid'] ?? null, 'status' => $data['status'] ?? null, 'status_name' => $data['status_name'] ?? null, 'note' => $data['note'] ?? null, 'ta_id' => $data['id'] ?? null, 'created_at' => now(), 'updated_at' => now(), ]; } if (!empty($task_assignments)) { TaskAssignment::upsert( $task_assignments, ['uid'], ['ta_id', 'name', 'username', 'email', 'phone_number', 'role', 'role_name', 'is_active', 'file', 'expertise', 'experience', 'is_verif', 'status', 'status_name', 'note', 'updated_at'] ); } return $responseData; } catch (\GuzzleHttp\Exception\ClientException $e) { if ($e->getCode() === 401 && !$retriedAfter401) { Log::warning("401 Unauthorized - Refreshing token and retrying..."); try{ $this->refreshToken(); $options['headers']['Authorization'] = "Bearer {$this->user_token}"; $retriedAfter401 = true; continue; }catch(\Exception $refreshError){ Log::error("Token refresh and login failed: " . $refreshError->getMessage()); return false; } } throw $e; } catch (\GuzzleHttp\Exception\ServerException | \GuzzleHttp\Exception\ConnectException $e) { if ($e->getCode() === 502) { Log::warning("502 Bad Gateway - Retrying in {$initialDelay} seconds..."); } else { Log::error("Network error ({$e->getCode()}) - Retrying in {$initialDelay} seconds..."); } sleep($initialDelay); $initialDelay *= 2; } catch (\Exception $e) { Log::error("Unexpected error: " . $e->getMessage()); throw $e; } } Log::error("Failed to fetch task assignments for UUID {$uuid} after {$maxRetries} retries."); throw new \Exception("Failed to fetch task assignments for UUID {$uuid} after retries."); } private function scraping_task_retributions($uuid) { $url = "{$this->simbg_host}/api/pbg/v1/detail/" . $uuid . "/retribution/submit/"; $options = [ 'headers' => [ 'Authorization' => "Bearer {$this->user_token}", 'Content-Type' => 'application/json' ] ]; $maxRetries = 3; $initialDelay = 1; $retriedAfter401 = false; for ($retryCount = 0; $retryCount < $maxRetries; $retryCount++) { try { $response = $this->client->get($url, $options); $responseData = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); if (empty($responseData['data']) || !is_array($responseData['data'])) { return true; } $data = $responseData['data']; $detailCreatedAt = isset($data['created_at']) ? Carbon::parse($data['created_at'])->format('Y-m-d H:i:s') : null; $detailUpdatedAt = isset($data['updated_at']) ? Carbon::parse($data['updated_at'])->format('Y-m-d H:i:s') : null; $pbg_task_retributions = PbgTaskRetributions::updateOrCreate( ['detail_id' => $data['id']], [ 'detail_uid' => $data['uid'] ?? null, 'detail_created_at' => $detailCreatedAt ?? null, 'detail_updated_at' => $detailUpdatedAt ?? null, 'luas_bangunan' => $data['luas_bangunan'] ?? null, 'indeks_lokalitas' => $data['indeks_lokalitas'] ?? null, 'wilayah_shst' => $data['wilayah_shst'] ?? null, 'kegiatan_id' => $data['kegiatan']['id'] ?? null, 'kegiatan_name' => $data['kegiatan']['name'] ?? null, 'nilai_shst' => $data['nilai_shst'] ?? null, 'indeks_terintegrasi' => $data['indeks_terintegrasi'] ?? null, 'indeks_bg_terbangun' => $data['indeks_bg_terbangun'] ?? null, 'nilai_retribusi_bangunan' => $data['nilai_retribusi_bangunan'] ?? null, 'nilai_prasarana' => $data['nilai_prasarana'] ?? null, 'created_by' => $data['created_by'] ?? null, 'pbg_document' => $data['pbg_document'] ?? null, 'underpayment' => $data['underpayment'] ?? null, 'skrd_amount' => $data['skrd_amount'] ?? null, 'pbg_task_uid' => $uuid, ] ); $pbg_task_retribution_id = $pbg_task_retributions->id; $prasaranaData = $data['prasarana'] ?? []; if (!empty($prasaranaData)) { $insertData = array_map(fn($item) => [ 'pbg_task_uid' => $uuid, 'pbg_task_retribution_id' => $pbg_task_retribution_id, 'prasarana_id' => $item['id'] ?? null, 'prasarana_type' => $item['prasarana_type'] ?? null, 'building_type' => $item['building_type'] ?? null, 'total' => $item['total'] ?? null, 'quantity' => $item['quantity'] ?? null, 'unit' => $item['unit'] ?? null, 'index_prasarana' => $item['index_prasarana'] ?? null, ], $prasaranaData); PbgTaskPrasarana::upsert($insertData, ['prasarana_id']); } return $responseData; } catch (\GuzzleHttp\Exception\ClientException $e) { if ($e->getCode() === 401 && !$retriedAfter401) { Log::warning("401 Unauthorized - Refreshing token and retrying..."); try{ $this->refreshToken(); $options['headers']['Authorization'] = "Bearer {$this->user_token}"; $retriedAfter401 = true; continue; }catch(\Exception $refreshError){ Log::error("Token refresh and login failed: " . $refreshError->getMessage()); return false; } } return false; } catch (\GuzzleHttp\Exception\ServerException | \GuzzleHttp\Exception\ConnectException $e) { if ($e->getCode() === 502) { Log::warning("502 Bad Gateway - Retrying in {$initialDelay} seconds..."); } else { Log::error("Network error ({$e->getCode()}) - Retrying in {$initialDelay} seconds..."); } sleep($initialDelay); $initialDelay *= 2; } catch (\GuzzleHttp\Exception\RequestException $e) { Log::error("Request error ({$e->getCode()}): " . $e->getMessage()); return false; } catch (\JsonException $e) { Log::error("JSON decoding error: " . $e->getMessage()); return false; } catch (\Throwable $e) { Log::critical("Unhandled error: " . $e->getMessage(), ['trace' => $e->getTraceAsString()]); return false; } } Log::error("Failed to fetch task retributions for UUID {$uuid} after retries."); throw new \Exception("Failed to fetch task retributions for UUID {$uuid} after retries."); } private function scraping_task_integrations($uuid){ $url = "{$this->simbg_host}/api/pbg/v1/detail/" . $uuid . "/retribution/indeks-terintegrasi/"; $options = [ 'headers' => [ 'Authorization' => "Bearer {$this->user_token}", 'Content-Type' => 'application/json' ] ]; $maxRetries = 3; $initialDelay = 1; $retriedAfter401 = false; for ($retryCount = 0; $retryCount < $maxRetries; $retryCount++) { try { $response = $this->client->get($url, $options); $responseData = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); if (empty($responseData['data']) || !is_array($responseData['data'])) { return true; } $data = $responseData['data']; $integrations[] = [ 'pbg_task_uid' => $uuid, 'indeks_fungsi_bangunan' => $data['indeks_fungsi_bangunan'] ?? null, 'indeks_parameter_kompleksitas' => $data['indeks_parameter_kompleksitas'] ?? null, 'indeks_parameter_permanensi' => $data['indeks_parameter_permanensi'] ?? null, 'indeks_parameter_ketinggian' => $data['indeks_parameter_ketinggian'] ?? null, 'faktor_kepemilikan' => $data['faktor_kepemilikan'] ?? null, 'indeks_terintegrasi' => $data['indeks_terintegrasi'] ?? null, 'total' => $data['total'] ?? null, ]; if (!empty($integrations)) { PbgTaskIndexIntegrations::upsert($integrations, ['pbg_task_uid'], ['indeks_fungsi_bangunan', 'indeks_parameter_kompleksitas', 'indeks_parameter_permanensi', 'indeks_parameter_ketinggian', 'faktor_kepemilikan', 'indeks_terintegrasi', 'total']); } return $responseData; } catch (\GuzzleHttp\Exception\ClientException $e) { if ($e->getCode() === 401 && !$retriedAfter401) { Log::warning("401 Unauthorized - Refreshing token and retrying..."); try{ $this->refreshToken(); $options['headers']['Authorization'] = "Bearer {$this->user_token}"; $retriedAfter401 = true; continue; }catch(\Exception $refreshError){ Log::error("Token refresh and login failed: " . $refreshError->getMessage()); return false; } } return false; } catch (\GuzzleHttp\Exception\ServerException | \GuzzleHttp\Exception\ConnectException $e) { if ($e->getCode() === 502) { Log::warning("502 Bad Gateway - Retrying in {$initialDelay} seconds..."); } else { Log::error("Network error ({$e->getCode()}) - Retrying in {$initialDelay} seconds..."); } sleep($initialDelay); $initialDelay *= 2; } catch (\GuzzleHttp\Exception\RequestException $e) { Log::error("Request error ({$e->getCode()}): " . $e->getMessage()); return false; } catch (\JsonException $e) { Log::error("JSON decoding error: " . $e->getMessage()); return false; } catch (\Throwable $e) { Log::critical("Unhandled error: " . $e->getMessage(), ['trace' => $e->getTraceAsString()]); return false; } } Log::error("Failed to fetch task index integration for UUID {$uuid} after retries."); throw new \Exception("Failed to fetch task index integration for UUID {$uuid} after retries."); } private function refreshToken() { $maxRetries = 3; // Maximum retry attempts $attempt = 0; while ($attempt < $maxRetries) { try { $attempt++; Log::info("Attempt $attempt: Refreshing token..."); $newAuthToken = $this->service_token->refresh_token($this->user_refresh_token); if (!isset($newAuthToken['access']) || !isset($newAuthToken['refresh'])) { throw new \Exception("Invalid refresh token response."); } $this->user_token = $newAuthToken['access']; $this->user_refresh_token = $newAuthToken['refresh']; Log::info("Token refreshed successfully on attempt $attempt."); return; // Exit function on success } catch (\Exception $e) { Log::error("Token refresh failed on attempt $attempt: " . $e->getMessage()); if ($attempt >= $maxRetries) { Log::info("Max retries reached. Attempting to log in again..."); break; } sleep(30); // Wait for 30 seconds before retrying } } // If refresh fails after retries, attempt re-login $attempt = 0; while ($attempt < $maxRetries) { try { $attempt++; Log::info("Attempt $attempt: Re-logging in..."); $loginAgain = $this->service_token->get_token(); // Login again if (!isset($loginAgain['access']) || !isset($loginAgain['refresh'])) { throw new \Exception("Invalid login response."); } $this->user_token = $loginAgain['access']; $this->user_refresh_token = $loginAgain['refresh']; Log::info("Re-login successful on attempt $attempt."); return; // Exit function on success } catch (\Exception $e) { Log::error("Re-login failed on attempt $attempt: " . $e->getMessage()); if ($attempt >= $maxRetries) { throw new \Exception("Both token refresh and login failed after $maxRetries attempts. " . $e->getMessage()); } sleep(30); // Wait for 30 seconds before retrying } } } }