38

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.

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
rap-2-h
  • 30,204
  • 37
  • 167
  • 263

7 Answers7

36

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.

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
Torgheh
  • 776
  • 8
  • 18
35

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

Mouagip
  • 5,139
  • 3
  • 37
  • 52
16

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.

Hyder B.
  • 10,900
  • 5
  • 51
  • 60
14

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');.

Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
  • Why we need // List all keys with status (awaiting, reserved, delayed) Redis::keys('*'); On the production this can be heavy call depending on the usage? – Shivdhwaj Pandey Sep 26 '19 at 04:31
  • @ShivdhwajPandey Because I am not sure about it's performance (`Redis::keys('*');`), you are correct, thank you (https://stackoverflow.com/a/32604218/4728084). I just listed them up here for debugging in dev or stg environment. Do you have any recommend commands in Prod env for this case? Please share us! :bow: <3 – Bơ Loong A Nhứi Nov 04 '19 at 12:23
6

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

Alessandro
  • 409
  • 5
  • 12
1

If anybody is still looking approach for the older versions of the Laravel:

$connection = 'queue';
$queueName = 'default';

$totalQueuedLeads = Redis::connection($connection)
    ->zcount('queues:'.$queueName.':delayed' , '-inf', '+inf');
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
1

I have two queues, a default queue and a low_prio queue in my laravel 5.7 project.

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class JobsOnQueue extends Command
{

    // su -c "php artisan jobs:on:queue" -s /bin/sh www-data

    protected $signature = 'jobs:on:queue';

    protected $description = 'Print jobs in redis';

    protected $lines = [];

    public function handle()
    {

        $connection = null;

        $queuename       = 'default';
        $default_delayed = \Queue::getRedis()
            ->connection($connection)
            ->zrange('queues:' . $queuename . ':delayed', -9999, 9999);

        $default_reserved = \Queue::getRedis()
            ->connection($connection)
            ->zrange('queues:' . $queuename . ':reserved', -9999, 9999);

        $queuename        = 'low_prio';
        $low_prio_delayed = \Queue::getRedis()
            ->connection($connection)
            ->zrange('queues:' . $queuename . ':delayed', -9999, 9999);

        $low_prio_reserved = \Queue::getRedis()
            ->connection($connection)
            ->zrange('queues:' . $queuename . ':reserved', -9999, 9999);

        $this->getQueueData('default delayed', $default_delayed);
        $this->getQueueData('default reserved', $default_reserved);
        $this->getQueueData('low prio delayed', $low_prio_delayed);
        $this->getQueueData('low prio reserved', $low_prio_reserved);

        $this->info(join("\n", $this->lines));

    }

    private function getQueueData($title, $arr)
    {
        $this->lines[] = "*** $title ***";
        if (count($arr) == 0) {
            $this->lines[] = "Nothing on queue";
            $this->lines[] = "";
            return;
        }

        foreach ($arr as $json) {
            $queue = json_decode($json);
            $data  = $queue->data;

            if (isset($data) && isset($data->command)) {
                $this->getCommands($data->command);
            }
        }
        $this->lines[] = "";

    }

    private function getCommands($serialized)
    {

        $readable = str_replace(
            'O:43:"Illuminate\Foundation\Console\QueuedCommand',
            'O:33:"App\Support\ReadableQueuedCommand',
            $serialized);

        $readable = unserialize($readable);
        $command  = $readable->getData();

        $attribs = [];
        $options = $command[1];
        foreach ($options as $key => $value) {
            $attribs[] = $key . '=' . $value;
        }

        $this->lines[] = $command[0] . ' ' . join(" - ", $attribs);
    }

}

The ReadableQueuedCommand looks like this

<?php

namespace App\Support;

use Illuminate\Foundation\Console\QueuedCommand;

class ReadableQueuedCommand extends QueuedCommand
{
    public function getData()
    {
        return $this->data;
    }
}

The artisan command then lists all in queue

> php artisan jobs:on:queue

*** default delayed ***
Nothing on queue

*** default reserved ***
Nothing on queue

*** low prio delayed ***
Nothing on queue

*** low prio reserved ***
oppty:time:profile --by-dataset=2
oppty:on:reset:pages --by-dataset=2

Joeri
  • 2,214
  • 24
  • 24