Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MichMich's avatar

Measuring Jobs of a Specific Type on Laravel Horizon Queue

Hello everyone,

I'm working on a laravel 11 project where I need to measure how many jobs of a specific type are currently on the queue managed by Laravel Horizon. However, I've encountered some difficulties in inspecting the jobs on the queue. Here’s what I’ve done so far and the issues I'm facing:

Objective:

My main goal is to measure the number of jobs of a specific type currently on the queue.

What I Have Tried:

  1. Fetching Job Keys:

    • I'm able to retrieve the job keys from Redis.
    $redis = Redis::connection('horizon');
    $keys = $redis->keys('*');
    print_r($keys);
    
  2. Inspecting Job Data:

    • I can see the job keys using the above method.
    • However, I encounter issues when trying to read the job data based on these keys.
    $key = 'my_app_horizon:08ae02cd-91d4-4e6f-97e0-33630f53bf83';
    $value = $redis->hgetall($key);
    print_r($value);
    
    • The above code returns an empty array.
  3. Verification with Redis CLI:

    • Using the Redis CLI, I can fetch the job data without any issues.
    redis-cli HGETALL my_app_horizon:08ae02cd-91d4-4e6f-97e0-33630f53bf83
    
    • The CLI returns the expected data, indicating that the key and data exist.

What Works:

  • The Redis connection and database selection seem correct since I can fetch keys.
  • Horizon is able to process jobs, which indicates that it can read job data correctly.

Issues:

  • Checking the type of a key using $redis->type($key) returns 0
  • Fetching job data using $redis->hgetall($key) returns an empty array despite the key existing and containing data.

Questions:

  1. How can I accurately read the job data from Redis within Laravel, similar to how Horizon does it?
  2. Is there a specific method or approach recommended for counting the number of jobs of a specific type currently on the queue?
  3. Why can't I read the redis data based on a key?

Any help or guidance would be greatly appreciated. Thank you!

0 likes
4 replies
LaryAI's avatar
Level 58

To measure the number of jobs of a specific type currently on the queue managed by Laravel Horizon, you can leverage Laravel's built-in queue and Horizon functionalities. Here’s a step-by-step solution to achieve this:

Step 1: Use Horizon's Metrics

Laravel Horizon provides metrics that can be used to monitor the jobs. You can use the Horizon::stats() method to get an overview of the queue status.

Step 2: Fetch Specific Job Type Count

To count the number of jobs of a specific type, you can use the Queue facade to inspect the jobs in the queue. However, Horizon does not provide a direct way to filter jobs by type. Instead, you can use Redis commands to filter and count the jobs.

Step 3: Custom Command to Count Jobs

You can create a custom Artisan command to count the jobs of a specific type. Here’s an example of how you can achieve this:

Create a Custom Artisan Command

  1. Generate a new Artisan command:

    php artisan make:command CountSpecificJobs
    
  2. Implement the command logic in app/Console/Commands/CountSpecificJobs.php:

    <?php
    
    namespace App\Console\Commands;
    
    use Illuminate\Console\Command;
    use Illuminate\Support\Facades\Redis;
    
    class CountSpecificJobs extends Command
    {
        protected $signature = 'jobs:count-specific {type}';
        protected $description = 'Count the number of jobs of a specific type in the queue';
    
        public function __construct()
        {
            parent::__construct();
        }
    
        public function handle()
        {
            $type = $this->argument('type');
            $redis = Redis::connection('horizon');
            $keys = $redis->keys('*');
    
            $count = 0;
    
            foreach ($keys as $key) {
                $jobData = $redis->hgetall($key);
                if (isset($jobData['data'])) {
                    $jobPayload = json_decode($jobData['data'], true);
                    if (isset($jobPayload['displayName']) && $jobPayload['displayName'] === $type) {
                        $count++;
                    }
                }
            }
    
            $this->info("Number of jobs of type {$type}: {$count}");
        }
    }
    

Step 4: Run the Command

You can now run the command to count the jobs of a specific type:

php artisan jobs:count-specific 'App\Jobs\YourSpecificJob'

Explanation

  1. Fetching Job Keys: The command fetches all keys from Redis.
  2. Inspecting Job Data: For each key, it retrieves the job data and checks if the job type matches the specified type.
  3. Counting Jobs: It increments the count for each matching job type.

Troubleshooting

  • Redis Connection: Ensure that the Redis connection is correctly configured in your config/database.php.
  • Job Data Structure: The job data structure may vary, so adjust the key names (data, displayName) as needed based on your job payload.

This approach should help you accurately measure the number of jobs of a specific type currently on the queue managed by Laravel Horizon.

MichMich's avatar

The approach that our AI friend Gary gives is exactly what I'm trying to do, but the following line returns an empty array:

$jobData = $redis->hgetall($key);
martinbean's avatar

@michmich Queues aren’t databases. If you want to see the counts of different types of jobs you have, push them to separate queues.

MichMich's avatar

@martinbean I understand what you're saying, but I respectfully don't fully agree with you. Regardless of the fact that Redis might or might not be considered a DB, I should be able to see which jobs there are. Just like Horizon does. I'm ably to see the keys, I'm able to read the data using the CLI, the only thing is I'm somehow not able to read the data using PHP.

Please or to participate in this conversation.