Search code examples
laravel-5configuration

Use multiple connection in laravel queue


Using laravel 5.5, we need to use both Redis and SQS queues. Redis for our internal messaging and SQS for messages coming from a 3rd party.

config/queue.php has various connection information. The first key is the default connection. That default is the one used by queue:work artisan command.

'default' => 'redis',

'connections' => [
    'sqs' => [            
        'driver' => 'sqs',
        'key'    => env('ACCESS_KEY_ID', ''),
        'secret' => env('SECRET_ACCESS_KEY', ''),
        'prefix' => 'https://sqs.us-west-1.amazonaws.com/account-id/',
        'queue'  => 'my-sqs-que'),
        'region' => 'us-west-1',
    ],


    'redis' => [
        'driver' => 'redis',
        'connection' => 'default',
        'queue' => env('REDIS_QUE' , 'default'),
        'retry_after' => 90,
    ],

The question is how can we use different queue connection for queue:work.

If --queue=my-sqs-que is supplied, with default connection set to redis, laravel looks under redis and obviously does not find my-sqs-que

Setting default to sqs will disable processing our internal messages.


Solution

  • You can specify the connection when running queue:work, see Specifying the Connection and Queue:

    You may also specify which queue connection the worker should utilize. The connection name passed to the work command should correspond to one of the connections defined in your config/queue.php configuration file:

    php artisan queue:work redis
    

    You will need to setup the corresponding connections per queue, as well.

    However, any given queue connection may have multiple "queues" which may be thought of as different stacks or piles of queued jobs.