laravel / horizon

Dashboard and code-driven configuration for Laravel queues.
https://laravel.com/docs/horizon
MIT License
3.85k stars 651 forks source link

Metrics broken when using custom serialization/compression on PhpRedis database connection #1462

Closed dir closed 3 months ago

dir commented 3 months ago

Horizon Version

5.24.5

Laravel Version

11.7.0

PHP Version

8.3

Redis Driver

PhpRedis

Redis Version

6.0.2

Database Driver & Version

No response

Description

I recently integrated Laravel Horizon as a dedicated worker service for a horizontally scaled Laravel API.

It has been wonderful, but when I click on the metrics tab, it loads infinitely. After looking at the logs, I get the following error message on a variety of Horizon endpoints.

GET 500 http://example.com/
[msgpack] (php_msgpack_unserialize) Extra bytes

I suspect this is because I am using custom PHPRedis serializer and compression options in my config/database.php, like so:

'redis' => [
  'options' => array_merge(
    [
      'persistent' => true,
    ],
    env('REDIS_CLIENT', 'phpredis') === 'phpredis' ? [
      'serializer' => Redis::SERIALIZER_MSGPACK,
      'compression' => Redis::COMPRESSION_LZ4,
    ]: []
  ),

  'default' => [
    'url' => env('REDIS_URL'),
    'host' => env('REDIS_PRIMARY_HOST', '127.0.0.1'),
    'username' => env('REDIS_USERNAME'),
    'password' => env('REDIS_PASSWORD'),
    'port' => env('REDIS_PORT', '6379'),
    'database' => 0,
  ],
],

I am also ensured to schedule the horizon:snapshot command every five minutes in my routes/console.php file, and have confirmed it is running:

<?php

use Illuminate\Support\Facades\Schedule;

Schedule::command('horizon:snapshot')->everyFiveMinutes();

Another odd note is that Horizon seems to run the jobs fine, and the outputs for failed/completed/etc jobs is working perfectly. Mainly seems to be the metrics.

Steps To Reproduce

To reproduce, I have provided the bare minimum configuration to replicate:

config/database.php Redis configuration:

'redis' => [
        'client' => env('REDIS_CLIENT', 'phpredis'),

        'options' => array_merge(
            [
                'persistent' => true,
            ],
            env('REDIS_CLIENT', 'phpredis') === 'phpredis' ? [
                    'serializer' => Redis::SERIALIZER_MSGPACK,
                    'compression' => Redis::COMPRESSION_LZ4,
                ]: []
        ),

        'default' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_PRIMARY_HOST', '127.0.0.1'),
            'username' => env('REDIS_USERNAME'),
            'password' => env('REDIS_PASSWORD'),
            'port' => env('REDIS_PORT', '6379'),
            'database' => 0,
        ],
]

config/queue.php

    'connections' => [
        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'queue' => 'default',
            'retry_after' => env('REDIS_QUEUE_RETRY_AFTER', 3610),
            'block_for' => 1,
            'after_commit' => false,
        ],
    ],

config/cache.php

    'stores' => [
        'redis' => [
            'driver' => 'redis',
            'connection' => 'cache_write',
            'lock_connection' => 'default',
        ],
    ],

config/horizon.php

    'use' => 'default',
    'prefix' => 'horizon:',

routes/console.php

<?php

use Illuminate\Support\Facades\Schedule;

Schedule::command('horizon:snapshot')->everyFiveMinutes();

If anything else is needed on the replication-side, please let me know!

github-actions[bot] commented 3 months ago

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

dir commented 3 months ago

Apologies, upon doing further research and a bit of a code dive, I have discovered that this appears to be a larger Laravel framework issue.

See https://github.com/laravel/framework/issues/47578 and https://github.com/laravel/docs/pull/9000.

I would love to push this forward and get these working, or find out a way to have serialization + compression on my cache, and disable it on my queue connections. But this is not a Laravel Horizon-specific issue by any means. Sorry for unneeded issue.