John Davidson

php - Laravel 9 - RateLimiter unable to access protected property in Job

0 comments
Message:


I have been trying to queue a job with Laravel 9 and setting a rate limit, to the API I am going to hit. The limit is max 10 requests per minute.


My AppServiceProvider.php


namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
public function register() {}

public function boot()
{
RateLimiter::for('sendContentStackToEasyTranslate', function($job) {
return Limit::perMinute(10)->by($job->entry->id);
});
}
}

My middleware and constructor in SenCSToET.php job


namespace App\Jobs;

use App\Models\ContentStackEntry;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\RateLimited;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use IncrediblePony\Auditlog\Traits\AuditlogTrait;

class SendCSToET implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, AuditlogTrait;

/**
* @var \App\Models\ContentStackEntry
*/
protected $entry;

/**
* Create a new job instance.
*
* @param \App\Models\ContentStackEntry
* @return void
*/
public function __construct(ContentStackEntry $entry)
{
$this->entry = $entry;
}

/**
* Get the middleware the job should pass through.
*
* @return array
*/
public function middleware() {
return [new RateLimited('sendContentStackToEasyTranslate')];
}
}


https://laravel.com/docs/9.x/queues#rate-limiting is my source of this approach.


When the code is hit the error is presented to me:


{
"message": "Cannot access protected property App\\Jobs\\SendCSToET::$entry",
"exception": "Error",
"file": "/var/www/services/test-translation/releases/6/app/Providers/AppServiceProvider.php",
"line": 30,
}

BUT if I change the $entry variable in SendCSToET.php file from protected to public the code runs as expected. What am I missing?



My Answer:
> In Laravel 9, the RateLimiter class has been updated to use a new syntax for defining rate limits. Instead of directly accessing the protected property, you should use the `limit` method to set the rate limit.

Here's an updated version of your code in AppServiceProvider.php:

php
namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
public function register() {}

public function boot()
{
RateLimiter::for('sendContentStackToEasyTranslate', function ($job) {
return Limit::perMinute(10);
});
}
}


By using the `Limit::perMinute(10)` method, you are setting the rate limit to 10 requests per minute for the specified job. This should work correctly with Laravel 9's RateLimiter functionality.

Rate this post

3 of 5 based on 5910 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us