egekhter
egekhter

Reputation: 2225

Laravel and Multiple SQS (queue) configs

Is there a method on the Queue class that can specify a specific connection as defined in the queue config? There's a similar option for MySql, where you can define 'mysql2', and then call:

DB::connection('mysql2')->table('etc')->get();

Is there a similar option for queues?

Something like:

Queue::connection('sqs2')->push('MyQueue', array('message' => $message));

Upvotes: 13

Views: 10226

Answers (2)

Lu32
Lu32

Reputation: 840

You only configure one connection to SQS, then unlike other drivers like redis or database, if you want to send a job in a different queue name, that queue should exist previously, create it in the amazon console or with an SDK/api, then pass it when dispatching the job like this:

dispatch(new MyJob)->onConnection('sqs')->onQueue('https://sqs.us-east-1.amazonaws.com/{account_id}/{queue_name}');

In this way you only configure one connection, and have multiple queues in that connection

For last for proccesing the Job like in egekhter response,

php artisan queue:work sqs --queue='https://sqs.us-east-1.amazonaws.com/{account_id}/{queue_name}'

you also can configure multiple connections but it seems unnecessary since all of them are in the same account and credentials, and it seems the perfect use case for multiple queues in the same conexion.

Upvotes: 5

egekhter
egekhter

Reputation: 2225

Apparently I answered my own question above without even realizing it. You can have multiple queues and specify which one you want to push the message to by using a connection method.

Here's what my partial config looks like for anybody that's interested:

    'default' => 'sqs',

   'connections' => array(

    'sync' => array(
        'driver' => 'sync',
    ),

    'beanstalkd' => array(
        'driver' => 'beanstalkd',
        'host'   => 'localhost',
        'queue'  => 'default',
    ),

    'sqs' => array(
        'driver' => 'sqs',
        'key'    => 'xxxxxxxxxxxx',
        'secret' => 'yyyyyyyyyyyyyy',
        'queue'  => 'https://sqs.us-west-2.amazonaws.com/zzzzzzzzz',
        'region' => 'us-west-2',
    ),

    'sqs2' => array(
        'driver' => 'sqs',
        'key'    => 'uuuuuuuuuuuuu',
        'secret' => 'vvvvvvvvvvvvvvvv',
        'queue'  => 'https://sqs.us-west-2.amazonaws.com/wwwwwwwwwww',
        'region' => 'us-west-2',
    ),

Upvotes: 15

Related Questions