Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel queue rate limiting not executing work

I'm attempting to implement Laravel 5.7's queue job rate limiting which for use when queue jobs hit an external API that's rate limited.

Here's my job:

    public function handle() {
        echo 'about to check throttling'.PHP_EOL;
        Redis::throttle('throttle-test')->allow(10)->every(5)->then(function () {
            // this is never executed
            echo 'doing work'.PHP_EOL;
        }, function () {
            // also never executed
            echo 'released back onto queue'.PHP_EOL;
            return $this->release(10);
        });
    }

There's no mention of needing to use Redis for queues, cache or anything of that nature in the docs, aside from "...application can interact with a Redis server." Either way, here's my env vars:

DB_CONNECTION=mysql
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=redis

I've confirmed the RedisServiceProvider is receiving the correct configuration it expects:

array:3 [
  "client" => "predis"
  "default" => array:4 [
    "host" => "redis"
    "password" => null
    "port" => "6379"
    "database" => 0
  ]
  "horizon" => array:5 [
    "host" => "redis"
    "password" => null
    "port" => "6379"
    "database" => 0
    "options" => array:1 [
      "prefix" => "horizon:"
    ]
  ]
]

I'm struggling to understand why there's no runtime errors, it's just that nothings get executed. If I comment out the throttle stuff the job runs fine and does what it's supposed to do. What am I missing about the requirements to use this throttling?

like image 709
Webnet Avatar asked Mar 01 '19 01:03

Webnet


People also ask

How do I know if my Laravel queue is working?

Show activity on this post. This is lower level but in the same vein you could run a command such as ps -aux | grep queue to literally view the running queue processes on whatever server your application/queue workers are running on.

How does Laravel rate limit work?

Laravel's rate limiting middleware stores the client's IP address alongside with the amount of requests in a given time period and performs a check on every request.

How do I retry failed jobs in Laravel?

You can retry all failed Jobs by running: php artisan queue:retry all . The question is for Laravel 4, but yes. From Laravel 5 and forward this is the correct answer.


1 Answers

The documentation for Laravel in this case is very misleading. While it implies you only need a connection to Redis you actually have to be using Redis for your queues.

like image 79
Webnet Avatar answered Sep 22 '22 13:09

Webnet