laravel-queue-rabbitmq

repository·master·Indexed 24 days ago

https://github.com/vyuldashev/laravel-queue-rabbitmq

A RabbitMQ Queue driver for Laravel and Lumen that allows developers to use RabbitMQ as a backend for Laravel's queue system. It supports standard Laravel Queue API, Laravel Horizon, Quorum Queues, and custom job/connection/worker classes. Includes specialized CLI commands such as rabbitmq:consume for high-performance message consumption and rabbitmq:exchange-declare for managing exchanges.

Tokens
8K
Snippets
17
Records
41
Agent score
85%

What's inside laravel-queue-rabbitmq

  1. Use a custom RabbitMQJob class

    master

    If you need to process messages from other applications that do not follow Laravel's job payload schema, you can extend RabbitMQJob::class and specify your custom class in the options.queue.job config key.

    Common use cases:

    • Customizing payload: Override payload() to change how the job data is decoded.
    • Handling raw/non-JSON messages: Override getName() to return an empty string if the message is not in a standard format.
    • Custom execution logic: Override fire() to manually resolve and execute classes.
    // Config example
    'rabbitmq' => [
        'options' => [
            'queue' => [
                'job' => \App\Queue\Jobs\RabbitMQJob::class,
            ],
        ],
    ],
    
    // Custom Job implementation example
    namespace App\Queue\Jobs;
    
    use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob as BaseJob;
    
    class RabbitMQJob extends BaseJob
    {
        public function fire()
        {
            $payload = $this->payload();
            $class = WhatheverClassNameToExecute::class;
            $method = 'handle';
    
            ($this->instance = $this->resolve($class))->{$method}($this, $payload);
    
            $this->delete();
        }
    }
  2. Consume messages using queue:work or rabbitmq:consume

    master

    There are two ways to consume messages from RabbitMQ:

    1. php artisan queue:work: Laravel's built-in command. It uses the basic_get protocol. Use this if you need to consume from multiple queues simultaneously.

    2. php artisan rabbitmq:consume: A command provided by this package. It uses the basic_consume protocol and is approximately 2x more performant than basic_get, but it does not support consuming multiple queues at once.

  3. Run the test suite

    master

    To run tests, first ensure RabbitMQ is running via Docker Compose:

    docker compose up -d

    Then use the following Composer commands:

    • composer test: Runs both style and unit tests.
    • composer test:style: Runs only style tests.
    • composer test:unit: Runs only unit tests.
    • composer fix:style: Automatically fixes most style issues found by the style tests.
    docker compose up -d
    
    # Test commands
    composer test
    composer test:style
    composer test:unit
    composer fix:style
  4. Enable Laravel Horizon support

    master

    To use Laravel Horizon, install Horizon and set the worker key in your RabbitMQ connection configuration to 'horizon'. This informs Laravel to use the QueueApi compatible with Horizon.

    'rabbitmq' => [
        // ...
    
        /* Set to "horizon" if you wish to use Laravel Horizon. */
       'worker' => env('RABBITMQ_WORKER', 'default'),
    ],
  5. Configure queue priority and delayed messages

    master

    You can prioritize messages when they are delayed by adding prioritize_delayed and queue_max_priority to the options.queue configuration. If queue_max_priority is omitted, it defaults to 2 when used.

    'rabbitmq' => [
        // ...
    
        'options' => [
            'queue' => [
                // ...
    
                'prioritize_delayed' =>  false,
                'queue_max_priority' => 10,
            ],
        ],
    ],
  6. Configure Network Protocol and Lazy Connections

    master

    Control how the connection is established using these keys:

    • network_protocol: The protocol used (e.g., tcp, ssl, tls). Default is tcp.
    • lazy: Boolean to enable/disable lazy connections. Default is true.
    • after_commit: Boolean to instruct workers to dispatch events only after database commits are completed.
    'rabbitmq' => [
        'network_protocol' => 'tcp',
        'lazy' => false,
        'after_commit' => true,
    ],
  7. Use your own RabbitMQJob class

    master

    The RabbitMQJob class implements Laravel's Illuminate\Contracts\Queue\Job contract. It wraps a PhpAmqpLib\Message\AMQPMessage and provides methods to manage the job lifecycle within RabbitMQ, such as delete() (which acknowledges the message), release() (which re-publishes the message with a delay), and markAsFailed() (which rejects the message).

    If you are extending the driver or implementing custom logic, you can interact with these methods to control how RabbitMQ handles the message lifecycle (e.g., triggering Dead Letter Exchanges via reject during markAsFailed).

  8. Configure Heartbeat and Network Timeouts

    master

    Adjust connection stability and performance using the following options keys:

    • heartbeat: Sets the heartbeat interval (default is 0).
    • connection_timeout: Connection timeout in seconds (float).
    • read_timeout: Read timeout in seconds (float).
    • write_timeout: Write timeout in seconds (float).
    • channel_rpc_timeout: Channel RPC timeout in seconds (float).
    'rabbitmq' => [
        'options' => [
            'heartbeat' => 10,
            'connection_timeout' => 3.0,
            'read_timeout' => 3.0,
            'write_timeout' => 3.0,
            'channel_rpc_timeout' => 0.0,
        ],
    ],