stratboy's avatar

How to add workers if I have different queues on the same connection?

Say I have Scout queues (and configured with 'connection' => 'redis', 'queue' => 'scout'), but also I use queues to upload images in background when I send certain forms. Both use redis. how should I start the workers? Is it correct like this:

sail artisan queue:work redis --queue=uploads sail artisan queue:work redis --queue=scout

Will it work?

0 likes
2 replies
LaryAI's avatar
Level 58

Yes, your approach is correct. You can start multiple workers for different queues on the same connection by specifying the queue name with the --queue option. This way, each worker will only process jobs from the specified queue.

Here is how you can start the workers for your uploads and scout queues:

sail artisan queue:work redis --queue=uploads
sail artisan queue:work redis --queue=scout

This will start two separate workers, one for the uploads queue and another for the scout queue, both using the redis connection.

Additionally, if you want to run these workers in the background, you can use a process manager like Supervisor to manage them. Here is an example Supervisor configuration for your workers:

[program:uploads_queue_worker]
process_name=%(program_name)s_%(process_num)02d
command=docker exec your_container_name sail artisan queue:work redis --queue=uploads --sleep=3 --tries=3
autostart=true
autorestart=true
numprocs=1
redirect_stderr=true
stdout_logfile=/path/to/your/logs/uploads_queue_worker.log

[program:scout_queue_worker]
process_name=%(program_name)s_%(process_num)02d
command=docker exec your_container_name sail artisan queue:work redis --queue=scout --sleep=3 --tries=3
autostart=true
autorestart=true
numprocs=1
redirect_stderr=true
stdout_logfile=/path/to/your/logs/scout_queue_worker.log

Replace your_container_name with the name of your Docker container running Laravel Sail, and adjust the stdout_logfile paths as needed.

This configuration will ensure that your workers are always running and will automatically restart if they fail.

1 like
kevinbui's avatar

yeah, and you can also run a single worker for both queues:

sail artisan queue:work redis --queue=uploads,scout

You might also already know about queue priorities.

Please or to participate in this conversation.