Skip to content

Commit

Permalink
feat: retry get gps coordinate when rate limited second (#5615)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Oct 17, 2021
1 parent 596970b commit 8eed44e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
13 changes: 13 additions & 0 deletions app/Exceptions/RateLimitedSecondException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Exceptions;

use RuntimeException;

class RateLimitedSecondException extends RuntimeException
{
public function __construct($e)
{
parent::__construct('', 429, $e);
}
}
13 changes: 9 additions & 4 deletions app/Jobs/GetGPSCoordinate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\Middleware\RateLimited;
use App\Exceptions\RateLimitedSecondException;
use App\Services\Instance\Geolocalization\GetGPSCoordinate as GetGPSCoordinateService;

class GetGPSCoordinate implements ShouldQueue
Expand Down Expand Up @@ -63,9 +64,13 @@ public function middleware()
*/
public function handle()
{
app(GetGPSCoordinateService::class)->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);
}
}
}
17 changes: 11 additions & 6 deletions app/Services/Instance/Geolocalization/GetGPSCoordinate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 8eed44e

Please sign in to comment.