How to get all pending jobs in laravel queue on redis?

46,202

Solution 1

If someone is still looking for an answer, here is the way I did it:

$connection = null;
$default = 'default';

// For the delayed jobs
var_dump(
    \Queue::getRedis()
        ->connection($connection)
        ->zrange('queues:'.$default.':delayed', 0, -1)
);
    
// For the reserved jobs
var_dump(
    \Queue::getRedis()
        ->connection($connection)
        ->zrange('queues:'.$default.':reserved', 0, -1)
);

$connection is the Redis' connection name which is null by default, and the $default is the name of the queue which is default by default.

Solution 2

Since Laravel 5.3 you can simply use Queue::size() (see PR).

Solution 3

You can also use the Redis Facade directly by doing this:

use Redis;

\Redis::lrange('queues:$queueName', 0, -1);

Tested in Laravel 5.6 but should work for all 5.X.

Solution 4

If you are using redis driver for your queue, you can count all remaining jobs by name:

use Redis;

// List all keys with status (awaiting, reserved, delayed)
Redis::keys('*');

// Count by name
$queueName = 'default';
echo Redis::llen('queues:' . $queueName);

// To count by status:
echo Redis::zcount('queues:' . $queueName . ':delayed', '-inf', '+inf');
echo Redis::zcount('queues:' . $queueName . ':reserved', '-inf', '+inf');

To see the result immediately, you can use php artisan tinker and hit Redis::llen('queues:default');.

Solution 5

You can install Horizon. Laravel Horizon provides a dashboard for monitoring your queues, and allows you to do more configuration to your queue.

composer require laravel/horizon

php artisan vendor:publish --provider="Laravel\Horizon\HorizonServiceProvider"

You have to set .env config file and config/horizon.php file.

Tested with Laravel 5.6

Share:
46,202
rap-2-h
Author by

rap-2-h

Laravel enthusiast & Rust artisan. Resume: https://raph.site/en

Updated on January 07, 2022

Comments

  • rap-2-h
    rap-2-h over 2 years

    The queue:listen was not run on a server, so some jobs were pushed (using Redis driver) but never run.

    How could I count (or get all) these jobs? I did not find any artisan command to get this information.