Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 Queue

I've been using the Queue system in Laravel 4 and it works great! - I was wondering if there was a way to view what is actually in the Queue? I'm using redis for the back-end.

like image 830
Ben Kilah Avatar asked May 20 '14 21:05

Ben Kilah


1 Answers

I spent some time on this digging around the Queue driver and the API. I was able to find an answer for you.

Short TL;DR version:

There is no native Queue::getList() (or similar) function on the Queue interface.

But this will get you a list of all queued jobs in your default Redis queue waiting to be processed:

$list = (Queue::getRedis()->command('LRANGE',['queues:default', '0', '-1']));

change default to another name if you run multiple queue tubes.

Be warned that command might result in a very large dataset being returned (its like dumping part of your database) - so you might just want to get the number of jobs queued instead:

 $queue_length = (Queue::getRedis()->command('LLEN',['queues:default']));

Longer version:

There is no native Queue::getList() (or similar) function on the Queue interface. But I noticed that it is possible to get the Redis driver from the Queue interface:

$redis = Queue::getRedis();

Digging into the Redis driver - we can see there is a function called command(). Which is defined as

command(string $method, array $parameters = array()) 
Run a command against the Redis database.

So that means we can now run any native Redis command through Laravel onto the Redis instance.

A full list of Redis commands is here

By browsing that list - we have a number of helpful commands which we can use for Queues.

Firstly - you can view all KEYS available - which might be useful if you are not sure of the name of your queues:

$keys = Queue::getRedis()->command('KEYS',['*']);

You can also make sure a specific KEY exists before running another operation - like this:

if (Queue::getRedis()->command('EXISTS',['queues:default']))
{
    // Queues:default key exists!
}

Also - you can get the length of the queue - which is useful

 $queue_length = (Queue::getRedis()->command('LLEN',['queues:default']));

And finally you can get the entire list of queues with this

 $list = (Queue::getRedis()->command('LRANGE',['queues:default1', '0', '-1']));

If you dont want the full list (perhaps your queue is quite large) - you can get a subset of it. Read more a LRANGE at the Redis docs here.

like image 171
Laurence Avatar answered Oct 09 '22 19:10

Laurence