Queue::connection('yourconnection')->push('Job', $data, 'tube');
Hi jminkler, I'm not quite sure that your reply answers the question, unless you're suggesting that I create a second 'sqs' connection called 'high' which is set to a different however that introduces its own problems when working with different queue drivers on live/local.
I've not used the Queue manager in laravel or SQS yet. But from a quite 1 minute look of the source code i can see that the queue url gets passed from the push()
method to pushRaw()
where it is used like so:
$this->sqs->sendMessage(array('QueueUrl' => $this->getQueue($queue)........
Assumingly the SqsClient requires a full URL, not an alias (which is what you want by looks of it). Now we don't really want to be going any deeper down into the call heirarchy as it will probably just get messy so the easiest method (but still quite involved) is to try and create our own "getQueue" method on that connector.
This can get as complex and fancy as you like. You COULD look at creating a queue url resolver or some other tye of url management that all connectors could rely on. But that's quite an involved thing to do, and far beyond the scope of a forum reply.
If you look at the service provider, you will see that the connectors are registered like so:
public function registerConnectors($manager)
{
foreach (array('Sync', 'Beanstalkd', 'Redis', 'Sqs', 'Iron') as $connector)
{
$this->{"register{$connector}Connector"}($manager);
}
}
Now you could create your own service provider that extends the default and register that instead. But i think the best thing to do is look at manually registering a connector outside of the service provider. Further down the service provider code you will find:
protected function registerSqsConnector($manager)
{
$manager->addConnector('sqs', function()
{
return new SqsConnector;
});
}
Here seems to be the magic we want. We need to call addConnector
on the queue manager and pass in an instance of a custom connector.
So first things first, create a custom connector somewhere like below, and make sure the class is being loaded by composer. This is really just a wrapper script that inits the actual queue
use Illuminate\Queue\Connectors\SqsConnector;
use Aws\Sqs\SqsClient;
class CustomSQSConnector extends SqsConnector {
public function connect(array $config)
{
$sqs = SqsClient::factory($config);
return new CustomSqsQueue($sqs, $config['queue']);
}
}
Now we need to create teh custom queue class, this is where we can redefine the getQueue method
use Illuminate\Queue\SqsQueue;
class CustomSqsQueue extends SqsQueue {
public function getQueue($queue)
{
/** You could do anything here, use an external resolver, use a switch, utilise a configuration object
Its up to you, but for the sake of keeping the example simple, i'll use an if statement, but you should do something better!
***/
if($queue == 'high') return 'http://somefullurl/path/to/somewhere/high';
//fallback
return parent::getQueue($queue);
}
}
So at this point, you have a custom connector and a custom queue. Since we inherit the default Sqs queue, it should just work (this is all untested!). So the last thing i think we need to do, is set it up.
So in your startup script app/start/global.php
put something like:
$app['queue']->addConnector('CustomSqs', function(){
return new CustomSqsConnector;
});
and don't forget the config in queue.php
duplicate your 'sqs' connection to 'customsqs' and set your default connection.
That should just about be most of what you need, to let you define a custom getqueue method which is the core thing you need to do, to do any fancy aliasing of the queue urls.
NOTICE: I've written all of this in the forum text box. None of it is tested, ive never done this before, and expect to have to bug fix and change things around. Use it as a guide and it should work, but expect to have to google a fair bit!
FINALLY: if all this seems to much. An easier approach would be to edit queue.php (in your config directory) create a new key of aliases and define some URL aliases. Such as:
'aliases' => [
'high' => 'http://somehost/somepath/high',
'low' => 'http://somehost/somepath/low'
]
Then you just push to your queue like so:
Queue::push('TestJob', $data', Config::get('queue.aliases.high'));
Wow, great answer, thank you! With this in mind (and for anyone who's looking for a similar solution) I'm planning to create a custom SQS queue with a switch, getting the queue URL from config variables. This will allow the URLs to be kept out of the class and overridden by local/staging/production config files as applicable.
use Illuminate\Queue\SqsQueue;
class CustomSqsQueue extends SqsQueue {
public function getQueue($queue)
{
switch ($queue) {
case 'high' : return Config.get('myconfig.sqs.url_high'); break;
case 'low' : return Config.get('myconfig.sqs.url_low'); break;
default: return Config.get('myconfig.sqs.url_default'); break;
}
}
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community