Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zuzex's avatar

Multiple queue listeners

Is it possible to run few queue listeners on the same server? Sometimes, when running 2 listeners, they appear to take the same task and produce an error in task logic. Using amazon queue service

0 likes
8 replies
sitesense's avatar

@zuzex Yes you can run several queue listeners like this:

// from command line 1
queue:listen --queue=my_first_queue --timeout=7200

// from command line 2
queue:listen --queue=my_second_queue --timeout=7200

When you push to the queue, use the queue name like this:

$queueName = 'my_first_queue';
$queue->pushOn($queueName, $command);
1 like
zuzex's avatar

Thanks! And what if I need few workers processing tasks from 1 queue? Is it possible?

MThomas's avatar

@zurex yes that is possible too, just start multiple listeners that pick from the same queue.

queue:listen --queue=my_first_queue --timeout=7200
queue:listen --queue=my_first_queue --timeout=7200

Make sure you check that the queue timeout is set correctly, if it takes say 5 seconds to complete a job, you need to make sure that the queue job won't be released to an other worker too.

BTW best to make use of supervisor to keep your queues running: https://laracasts.com/lessons/supervise-this

1 like
zuzex's avatar

Yes, that's it! Is there a way to prevent job to be taken by another worker, if it is already processing?

MThomas's avatar

@zuzex for that you need to keep track of the jobs you are processing. If you push a job to the db, add a record to a table in the db, once your worker picks up the job the first thing you do is marking that job as currently in process in the table (a in_progress column?), once the job is finished you will mark the job as processed.

Each worker who picks op a job firsts checks if the job is not locked by an other worker.

1 like
zuzex's avatar

@MThomas, thank you! I just hoped laravel and/or amazon have out-of-the-box support for this...

MThomas's avatar
MThomas
Best Answer
Level 35

@zuzex laravel does have some support for this, if you configure your listener correct than a job will be locked for some time.

The biggest problem with SQS is that it has the tendency to deliver message duplicates once in a while: https://www.google.nl/search?q=amazon%20sqs%20duplicate

So if you use SQS you will need to keep track of your pushed and pulled jobs :)

1 like

Please or to participate in this conversation.