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

后端 未结 7 933
抹茶落季
抹茶落季 2020-12-13 10:06

Queue listener was not started on a server, some jobs where pushed (using Redis driver).

How could I count (or get all) theses jobs ? I did not found any artisan co

相关标签:
7条回答
  • 2020-12-13 10:13

    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.

    0 讨论(0)
  • 2020-12-13 10:15

    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

    0 讨论(0)
  • 2020-12-13 10:22

    I am an PHP Laravel dev, 3 years, I have just known these command recently, so shame on me. ;(

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

    0 讨论(0)
  • 2020-12-13 10:23

    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');
    
    0 讨论(0)
  • 2020-12-13 10:29

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

    0 讨论(0)
  • 2020-12-13 10:30

    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
    
    
    0 讨论(0)
提交回复
热议问题