Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Laravel how to create a queue object and set their connection without Facade

In Lumen/Laravel I want to send a message to a given queue.

by default I have it set to Redis, what I would like is to send it to another queue server as another application will take care of it.

I know I can do $queue->pushRaw('payload'); However there is no subsequent way for me to pick the connection.

I am aware that I can use Facade to create my Queue as such:

$connection = Queue::connection('connection_name');
$connection->pushOn('queue_name', $job)

However I'm doing this in Lumen, and would like to avoid turning on the Facade Just for this aspect. Also, I would like to know how to do this as I would like to pass by IoC through a job event handler eventually.

Version of Lumen/Laravel 5.2.

like image 449
azngunit81 Avatar asked May 20 '16 21:05

azngunit81


People also ask

What is DB facade in Laravel?

In a Laravel application, a facade is a class that provides access to an object from the container. The machinery that makes this work is in the Facade class. Laravel's facades, and any custom facades you create, will extend the base Illuminate\Support\Facades\Facade class.

What is Laravel queue sync?

Sync, or synchronous, is the default queue driver which runs a queued job within your existing process. With this driver enabled, you effectively have no queue as the queued job runs immediately.

How do I know if Laravel queue is running?

$this->mailer->queue($view, $data, function ($message) use ($toEmail, $toName, $subject) { $message ->to($toEmail, $toName) ->subject($subject); }); This will successfully run, but if the queue is not 'listening', the job gets pushed on to the job table, forever.


1 Answers

As @Mois44 alluded to, you should be able to accomplish this with the QueueManager.

The QueueManager will allow you to call the connection() method, which will return a Queue object. And from here, you can call the normal queued functions (pushOn, laterOn, etc)

// Returns an Illuminate\Queue\QueueManager object
$queueManager = app('queue'); 

// Returns an Illuminate\Queue\Queue object
$queue = $queueManager->connection('my-connection'); 

$queue->pushOn('queue_name', $job);

or all chained together

app('queue')->connection('my-connection')->pushOn('queue_name', $job)

Admittedly, my Lumen specific knowledge is pretty limited. If the app() method doesn't work to get an instance of the QueueMananger, then I'm not sure what to do.

like image 199
Tommmm Avatar answered Sep 30 '22 06:09

Tommmm