Service Stat with redis
composer require spatie/laravel-rate-limited-job-middleware
This commit is contained in:
parent
41943a6db8
commit
bccc1fb052
6 changed files with 106 additions and 9 deletions
|
|
@ -10,6 +10,7 @@ use Illuminate\Foundation\Bus\Dispatchable;
|
|||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Spatie\RateLimitedMiddleware\RateLimited;
|
||||
|
||||
class SetServiceStats implements ShouldQueue
|
||||
{
|
||||
|
|
@ -18,8 +19,10 @@ class SetServiceStats implements ShouldQueue
|
|||
use Queueable;
|
||||
use SerializesModels;
|
||||
|
||||
/** Used to prevent overlaping */
|
||||
private const JOBKEY = 95563222;
|
||||
// Set high max tries as using prevent overlap
|
||||
public $tries = 40;
|
||||
|
||||
public $maxExceptions = 40;
|
||||
|
||||
/** @var \App\Models\Service */
|
||||
protected $service;
|
||||
|
|
@ -41,7 +44,11 @@ class SetServiceStats implements ShouldQueue
|
|||
*/
|
||||
public function handle()
|
||||
{
|
||||
$serviceStat = ServiceStat::firstOrNew(['date' => date('Y-m-d')]);
|
||||
$serviceStat = ServiceStat::firstOrNew([
|
||||
'date' => date('Y-m-d'),
|
||||
'service_id' => $this->service->id,
|
||||
'brand_id' => $this->service->brand->id,
|
||||
]);
|
||||
if (!$serviceStat->exists) {
|
||||
$serviceStat->service()->associate($this->service);
|
||||
$serviceStat->brand()->associate($this->service->brand);
|
||||
|
|
@ -50,8 +57,21 @@ class SetServiceStats implements ShouldQueue
|
|||
$serviceStat->saveOrFail();
|
||||
}
|
||||
|
||||
public function retryUntil()
|
||||
{
|
||||
return now()->addMinutes(rand(0, 360));
|
||||
}
|
||||
|
||||
public function middleware()
|
||||
{
|
||||
return [(new WithoutOverlapping(self::JOBKEY))->releaseAfter(30)];
|
||||
$rateLimitedMiddleware = (new RateLimited())
|
||||
->allow(4)
|
||||
->everySeconds(1)
|
||||
->releaseAfterSeconds(rand(90, 180));
|
||||
return [
|
||||
$rateLimitedMiddleware,
|
||||
(new WithoutOverlapping($this->service->id))
|
||||
->releaseAfter(rand(90, 180))
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,15 +3,24 @@
|
|||
namespace App\Jobs;
|
||||
|
||||
use App\Models\Brand;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
|
||||
class StartStatJobs
|
||||
class StartStatJobs implements ShouldQueue
|
||||
{
|
||||
use Dispatchable;
|
||||
use InteractsWithQueue;
|
||||
use Queueable;
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$brands = Brand::all();
|
||||
foreach ($brands as $brand) {
|
||||
\dispatch(new StatJob($brand))->onQueue('stats')->delay(rand(0, 90));
|
||||
\dispatch(new StatJob($brand))
|
||||
->onConnection('redis')
|
||||
->onQueue('stats')->delay(rand(0, 90));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -150,7 +150,7 @@ class Service extends Model
|
|||
public function incrementStat(): void
|
||||
{
|
||||
SetServiceStats::dispatch($this)->afterResponse()
|
||||
->onConnection('database')->onQueue('stats');
|
||||
->onConnection('redis')->onQueue('stats');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -2,12 +2,14 @@
|
|||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Cache\RateLimiting\Limit;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Pagination\Paginator;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Illuminate\Support\Facades\View;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
|
@ -31,6 +33,11 @@ class AppServiceProvider extends ServiceProvider
|
|||
*/
|
||||
public function boot()
|
||||
{
|
||||
// STATS jobs Rate Limiting
|
||||
RateLimiter::for('stats', function () {
|
||||
return Limit::perMinute(60)->by(2);
|
||||
});
|
||||
|
||||
//! Important pourra etre retiré quand la V1 sera supprimé
|
||||
// empeche l'encapsulation dans data
|
||||
JsonResource::withoutWrapping();
|
||||
|
|
|
|||
|
|
@ -42,7 +42,8 @@
|
|||
"league/csv": "^9.7",
|
||||
"propaganistas/laravel-phone": "^4.3",
|
||||
"rennokki/laravel-eloquent-query-cache": "^3.0",
|
||||
"spatie/laravel-mix-preload": "^1.2"
|
||||
"spatie/laravel-mix-preload": "^1.2",
|
||||
"spatie/laravel-rate-limited-job-middleware": "^1.5"
|
||||
},
|
||||
"require-dev": {
|
||||
"facade/ignition": "^2.5",
|
||||
|
|
|
|||
62
composer.lock
generated
62
composer.lock
generated
|
|
@ -4,7 +4,7 @@
|
|||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "b8758dfa7a98fe359d4f35d1caf11de0",
|
||||
"content-hash": "cac9cff057e02f69a5e32aa8ca5174d4",
|
||||
"packages": [
|
||||
{
|
||||
"name": "arondeparon/laravel-request-sanitizer",
|
||||
|
|
@ -4058,6 +4058,66 @@
|
|||
],
|
||||
"time": "2021-01-04T08:01:50+00:00"
|
||||
},
|
||||
{
|
||||
"name": "spatie/laravel-rate-limited-job-middleware",
|
||||
"version": "1.5.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/spatie/laravel-rate-limited-job-middleware.git",
|
||||
"reference": "7b72592e0d823e2948c413f5e661de0fd3431db5"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/spatie/laravel-rate-limited-job-middleware/zipball/7b72592e0d823e2948c413f5e661de0fd3431db5",
|
||||
"reference": "7b72592e0d823e2948c413f5e661de0fd3431db5",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"illuminate/redis": "^6.0|^7.0|^8.0",
|
||||
"php": "^7.3|^8.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"mockery/mockery": "^1.4",
|
||||
"orchestra/testbench": "^4.0|^5.0|^6.0",
|
||||
"phpunit/phpunit": "^8.2|^9.0",
|
||||
"symfony/var-dumper": "^4.3|^5.0"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Spatie\\RateLimitedMiddleware\\": "src"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Freek Van der Herten",
|
||||
"email": "freek@spatie.be",
|
||||
"homepage": "https://spatie.be",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"description": "A middleware that can rate limit jobs",
|
||||
"homepage": "https://github.com/spatie/laravel-rate-limited-job-middleware",
|
||||
"keywords": [
|
||||
"laravel-rate-limited-job-middleware",
|
||||
"spatie"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/spatie/laravel-rate-limited-job-middleware/issues",
|
||||
"source": "https://github.com/spatie/laravel-rate-limited-job-middleware/tree/1.5.0"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://www.patreon.com/spatie",
|
||||
"type": "patreon"
|
||||
}
|
||||
],
|
||||
"time": "2020-11-27T09:36:55+00:00"
|
||||
},
|
||||
{
|
||||
"name": "swiftmailer/swiftmailer",
|
||||
"version": "v6.2.7",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue