From 8eed44e48ecee3f19e57665a67698cb1df8ae8f0 Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Sun, 17 Oct 2021 18:27:27 +0200 Subject: [PATCH] feat: retry get gps coordinate when rate limited second (#5615) --- app/Exceptions/RateLimitedSecondException.php | 13 ++++++++ app/Jobs/GetGPSCoordinate.php | 13 +++++--- .../Geolocalization/GetGPSCoordinate.php | 17 +++++++---- .../Geolocalization/GetGPSCoordinateTest.php | 30 +++++++++++++++++-- 4 files changed, 61 insertions(+), 12 deletions(-) create mode 100644 app/Exceptions/RateLimitedSecondException.php diff --git a/app/Exceptions/RateLimitedSecondException.php b/app/Exceptions/RateLimitedSecondException.php new file mode 100644 index 00000000000..6f49bbae092 --- /dev/null +++ b/app/Exceptions/RateLimitedSecondException.php @@ -0,0 +1,13 @@ +execute([ - 'account_id' => $this->place->account_id, - 'place_id' => $this->place->id, - ]); + try { + app(GetGPSCoordinateService::class)->execute([ + 'account_id' => $this->place->account_id, + 'place_id' => $this->place->id, + ]); + } catch (RateLimitedSecondException $e) { + $this->release(15); + } } } diff --git a/app/Services/Instance/Geolocalization/GetGPSCoordinate.php b/app/Services/Instance/Geolocalization/GetGPSCoordinate.php index a829ae0db83..7845838ffb8 100644 --- a/app/Services/Instance/Geolocalization/GetGPSCoordinate.php +++ b/app/Services/Instance/Geolocalization/GetGPSCoordinate.php @@ -7,7 +7,8 @@ use App\Services\BaseService; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Http; -use Illuminate\Http\Client\HttpClientException; +use Illuminate\Http\Client\RequestException; +use App\Exceptions\RateLimitedSecondException; class GetGPSCoordinate extends BaseService { @@ -85,11 +86,15 @@ private function query(Place $place): ?Place $place->save(); return $place; - } catch (HttpClientException $e) { - Log::error(__CLASS__.' '.__FUNCTION__.': Error making the call: '.$e->getMessage(), [ - 'query' => Str::of($query)->replace(config('monica.location_iq_api_key'), '******'), - $e, - ]); + } catch (RequestException $e) { + if ($e->response->status() === 429 && ($error = $e->response->json('error')) && $error === 'Rate Limited Second') { + throw new RateLimitedSecondException($e); + } else { + Log::error(__CLASS__.' '.__FUNCTION__.': Error making the call: '.$e->getMessage(), [ + 'query' => Str::of($query)->replace(config('monica.location_iq_api_key'), '******'), + $e, + ]); + } } return null; diff --git a/tests/Unit/Services/Instance/Geolocalization/GetGPSCoordinateTest.php b/tests/Unit/Services/Instance/Geolocalization/GetGPSCoordinateTest.php index c8724d009b8..a6b4f9204cd 100644 --- a/tests/Unit/Services/Instance/Geolocalization/GetGPSCoordinateTest.php +++ b/tests/Unit/Services/Instance/Geolocalization/GetGPSCoordinateTest.php @@ -5,6 +5,7 @@ use Tests\TestCase; use App\Models\Account\Place; use Illuminate\Support\Facades\Http; +use App\Exceptions\RateLimitedSecondException; use Illuminate\Validation\ValidationException; use Illuminate\Foundation\Testing\DatabaseTransactions; use App\Services\Instance\Geolocalization\GetGPSCoordinate; @@ -97,7 +98,32 @@ public function it_fails_if_wrong_parameters_are_given() $this->expectException(ValidationException::class); - $geocodingService = new GetGPSCoordinate; - $place = app(GetGPSCoordinate::class)->execute($request); + app(GetGPSCoordinate::class)->execute($request); + } + + /** @test */ + public function it_release_the_job_if_rate_limited_second() + { + config(['monica.enable_geolocation' => true]); + config(['monica.location_iq_api_key' => 'test']); + + Http::fake([ + 'us1.locationiq.com/v1/*' => Http::response('{"error":"Rate Limited Second"}', 429), + ]); + + $place = factory(Place::class)->create([ + 'country' => 'ewqr', + 'street' => '', + 'city' => 'sieklopekznqqq', + 'postal_code' => '', + ]); + + $request = [ + 'account_id' => $place->account_id, + 'place_id' => $place->id, + ]; + + $this->expectException(RateLimitedSecondException::class); + app(GetGPSCoordinate::class)->execute($request); } }