Gaganpreet Kaur
Gaganpreet Kaur

Reputation: 371

Multiple Queues in 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.

Upvotes: 7

Views: 32791

Answers (6)

Dillonsmart
Dillonsmart

Reputation: 1

In addition to the other answers here, you can also dispatch a job on a specified queue in Laravel like so:

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

Or, within the Job constructor:

public function __construct()
{
    $this->onQueue('myJobQueue');
}

Remember to start your queue from the terminal:

php artisan queue:listen --queue=myJobQueue

Upvotes: 0

Pushkraj Jori
Pushkraj Jori

Reputation: 359

If you are testing on a local machine you can create a .bat file inside your project and enter these lines in that bat file

start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low
start "" php artisan queue:work --queue=low

one line represents one queue at a time 10 means 10 queues will run at once.

also, I included --queue=low cause I have a low queue.

this is for local machines only for the online production checkout Supervisor.

Upvotes: 0

Antonio
Antonio

Reputation: 69

This might be old but just in case, all of the answers are on point, but you must set the .env variable QUEUE_CONNECTION to something else than sync,if your configuration is set to sync it will take every job in order of entry to the queue (thus finishing one and then starting the next one), if it's set to database or redis it will be taking jobs in parallel if needed (which is the idea of setting priorities) you should check this article (it helped me) https://medium.com/hexavara-tech/optimize-laravel-with-redis-caching-made-easy-bf486bf4c58 also you will need to configure your queues in config/queue.php like such for example in the 'connections' array:

    'database' => [
        'driver' => 'database',
        'table' => 'jobs',
        'queue' => ['default','another_queue'], //this is just 'default' by default
        'retry_after' => 90,
    ],

this applies for all the other configurations in this file.

Upvotes: 5

kregus
kregus

Reputation: 1108

Like Ben V said, it is highly recommended to use Supervisor to keep the workers active at all times, especially if you want to run one or more workers per queue, or if you want the queues to be processed simultaneously.

Here is an example Supervisor configuration file:

[program:laravel-worker-myJobQueue]
process_name=%(program_name)s_%(process_num)s
command=php artisan queue:work --queue=myJobQueue
numprocs=8
autostart=true
autorestart=true

[program:laravel-worker-myJobQueue1]
process_name=%(program_name)s_%(process_num)s
command=php artisan queue:work --queue=myJobQueue1
numprocs=1
autostart=true
autorestart=true

The above configuration creates 8 workers for myJobQueue, and one worker for myJobQueue1, since multiple workers can help speed things up, but can cause trouble for jobs that try to access the same row in the database, in which case you want to limit things to 1 worker only.

You then simply dispatch jobs to the correct queue using

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

or

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

Upvotes: 4

Jfizzle
Jfizzle

Reputation: 81

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.

Upvotes: 8

Ben V.
Ben V.

Reputation: 231

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!

Upvotes: 9

Related Questions