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.
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.
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.
lrange
command instead of zrange
. –
Twofaced Since Laravel 5.3 you can simply use Queue::size()
(see PR).
Queue::size('queue-name');
for specific queue. –
Liquescent 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.
lrange
command instead of zrange
. –
Twofaced 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.
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');
.
Redis::keys('*');
), you are correct, thank you (https://mcmap.net/q/175304/-scan-vs-keys-performance-in-redis). 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 –
Icebreaker 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
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');
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
© 2022 - 2024 — McMap. All rights reserved.