Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Queues in Laravel

Tags:

php

queue

laravel

I am creating a web application in laravel in which bidding is being done by users in multiple games. Bidding is being performed by front end users and by cron job as well. Cron job do bid on each game after each second. Therefore some collision was happening between bids when same row was accessed at same time. To resolve concurrency issue I decided to use laravel queues for bidding. But I am having multiple games and therefore I want simultaneously bids of each game. I don't want bids of same game to be process at same time because then concurrency issue can be occur. I want to know about multiple queues system in laravel. After having search on internet I got to know about multiple queues like

php artisan queue:work --queue=myJobQueue, myJobQueue1, myJobQueue2,..myJobQueue7

But I am not sure how it works. Please someone explain me in detail that all 7 queues work simultaneously or one by one.

like image 258
Gaganpreet Kaur Avatar asked May 29 '18 04:05

Gaganpreet Kaur


2 Answers

Are looking for the queue:listen command?

queue:work will process all pending jobs that are stored by the queue driver, whereas queue:listen will be waiting for jobs to be thrown at it to execute them as they come.

If you do php artisan queue:listen --queue=myJobQueue, myJobQueue1, myJobQueue2,..myJobQueue7, 7 queues are being created and listening to new tasks on their own.

In your code, you can dispatch jobs like the following:

dispatch((new MyJob)->onQueue('myJobQueue'));

You might want to use a tool like Supervisor to make sure queue:listen is always running in the background.

Hope this helps!

like image 112
Ben V. Avatar answered Nov 02 '22 12:11

Ben V.


php artisan queue:work --queue=myJobQueue, myJobQueue1, myJobQueue2,..myJobQueue7 sets the priority in which queues will be executed. So with this all jobs on myJobQueue will be executed before moving to execute jobs on myJobQueue1 then to myJobQueue2 in that order.

However if you want jobs on these queues to be executed simultaneously, you could run each queue in the background.

php artisan queue:work --queue=myJobQueue & php artisan queue:work --queue=myJobQueue1 & php artisan queue:work --queue=myJobQueue2 &

This will run each queue as single processes in the background.

like image 35
Jfizzle Avatar answered Nov 02 '22 14:11

Jfizzle