ayip
ayip

Reputation: 2543

"PhpAmqpLib\Exception\AMQPRuntimeException: Channel connection is closed" Error with Lumen Queues

I am using vladimir-yuldashev/laravel-queue-rabbitmq library to use RabbitMq queues in Lumen project.

The queue functionality is working fine, but I see tons of below errors in my log file.

lumen.ERROR: PhpAmqpLib\Exception\AMQPRuntimeException: Channel connection is closed. in /var/www/html/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Channel/AbstractChannel.php:227

From the error stack trace, it seems that the queue name is taken as "NULL". Here is my rabbitmq connection configuration from queue.php

'rabbitmq'  => [
        'driver' => 'rabbitmq',

        'host' => env('RABBITMQ_HOST', 'rabbitmq'),
        'port' => env('RABBITMQ_PORT', 5672),

        'vhost'    => env('RABBITMQ_VHOST', '/'),
        'login'    => env('RABBITMQ_LOGIN', 'guest'),
        'password' => env('RABBITMQ_PASSWORD', 'guest'),

        'queue' => env('RABBITMQ_QUEUE'),
        // name of the default queue,

        'exchange_declare' => env('RABBITMQ_EXCHANGE_DECLARE', true),
        // create the exchange if not exists
        'queue_declare_bind' => env('RABBITMQ_QUEUE_DECLARE_BIND', true),
        // create the queue if not exists and bind to the exchange

        'queue_params' => [
            'passive'     => env('RABBITMQ_QUEUE_PASSIVE', false),
            'durable'     => env('RABBITMQ_QUEUE_DURABLE', true),
            'exclusive'   => env('RABBITMQ_QUEUE_EXCLUSIVE', false),
            'auto_delete' => env('RABBITMQ_QUEUE_AUTODELETE', false),
        ],
        'exchange_params' => [
            'name' => env('RABBITMQ_EXCHANGE_NAME', null),
            'type' => env('RABBITMQ_EXCHANGE_TYPE', 'direct'),
            // more info at http://www.rabbitmq.com/tutorials/amqp-concepts.html
            'passive' => env('RABBITMQ_EXCHANGE_PASSIVE', false),
            'durable' => env('RABBITMQ_EXCHANGE_DURABLE', true),
            // the exchange will survive server restarts
            'auto_delete' => env('RABBITMQ_EXCHANGE_AUTODELETE', false),
        ],

        'sleep_on_error' => env('RABBITMQ_ERROR_SLEEP', 5), // the number of seconds to sleep if there's an error communicating with rabbitmq

    ]

I am not using the default queue. Instead, each of my event listeners declare a queue for itself. Here is how I am using the queue commands to start the worker and the listeners.

worker

php artisan queue:work rabbitmq

Listeners

php artisan queue:listen --queue=my-queue-1 --timeout=0
php artisan queue:listen --queue=my-queue-2 --timeout=0
php artisan queue:listen --queue=my-queue-3 --timeout=0

Each of these queue functionality is working fine.

My questions are:

  1. Is it ok to start just one worker for multiple listeners?
  2. Why are my logs filled up with these errors? And how can I fix this?

One more note: Not sure if it matters, my events are chained. i.e., I am firing event 2 from the event 1 listeners and so on so forth.

Upvotes: 4

Views: 3812

Answers (1)

ayip
ayip

Reputation: 2543

Ok, I finally have breakthrough on this one. Apparently, the error is happening because of this command php artisan queue:work rabbitmq as I was not passing the --queue option and I don't have a default queue declared in my .env file.

As per this question on SO, my understanding of how these queue commands work is incorrect.

As mentioned in the above url, I have totally removed the queue:listen and used multiple queue:work commands, passing the queue name to each of the work command. So, after the changes, this is how my commands look like:

php artisan queue:work --queue=my-queue-1 --timeout=0
php artisan queue:work --queue=my-queue-2 --timeout=0
php artisan queue:work --queue=my-queue-3 --timeout=0

Upvotes: 0

Related Questions