DEV Community

Cover image for How to Set up Laravel Queues on Production
Trilochan Parida
Trilochan Parida

Posted on

How to Set up Laravel Queues on Production

Hello everyone, in this article, you will learn how to set up Laravel Queues/Jobs on production

Below steps to follow...

1.Connect to your ubuntu server and go to your existing laravel project then run the below command to create required tables in the database.

php artisan queue:table
Enter fullscreen mode Exit fullscreen mode

2.Then migrate the tables with the help of the below command

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

3.Now, you can create Jobs controller through command so that you will get the basic format of jobs or queue in laravel

php artisan make:job SendNotification
Enter fullscreen mode Exit fullscreen mode

Here, SendNotification is a controller, where your main logic of queue/job will execute.

Laravel queues provide a unified queueing API across a variety of different queue backends, such as Amazon SQS, Redis, or even a relational database.

Now, we need to setup .env file to execute the jobs using database

4.Go to .env file then change QUEUE_DRIVER=database or QUEUE_CONNECTION=database

5.Go to the app/Jobs folder and write the task in the handle function of the SendNotification.php file and you can pass the variables from the controller and define them in the constructor in the Job file. (Refer to Video for better understanding)

6.From any controller you can dispatch the job: dispatch(new SendNotification($mobile, $msg));

7.To automate the queue we have to install a supervisor

apt install supervisor
Enter fullscreen mode Exit fullscreen mode

8.Create supervisor config file at /etc/supervisor/conf.d queue-worker.conf

cd /etc/supervisor/conf.d
nano queue-worker.conf
Enter fullscreen mode Exit fullscreen mode

queue-worker.conf

[program:queue-worker]
process_name = %(program_name)s_%(process_num)02d
command=php /var/www/html/project-folder/artisan queue:listen
autostart=true
autorestart=true
user=root
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/html/project-folder/public/worker.log
Enter fullscreen mode Exit fullscreen mode

9.Reread the supervisor the config file

supervisorctl reread
Enter fullscreen mode Exit fullscreen mode

10.Activate the process

supervisorctl update
Enter fullscreen mode Exit fullscreen mode

To know more about Laravel Queue

Top comments (4)

Collapse
 
billyston profile image
billyston

I tried to run this on azure web app server. The installation works fine but when I run supervisorctl reread or supervisorctl update, I get the following error: error: , [Errno 2] No such file or directory: file: /usr/lib/python3/dist-packages/supervisor/xmlrpc.py line: 560. I check on many resources online but I did not find one how azure web app. Can you help me?

Collapse
 
sakibbuddy profile image
Anisur Rahaman Sakib

Check if the supervisor is running or not. service supervisor --status. If it's not running then start it by sudo service supervisor start

Collapse
 
aramayis_m profile image
Aramayis

You can't save the log file in the public folder. Any user can have access to this file. It is not safe. Save the log file in the storage/logs folder .

Collapse
 
djfranck profile image
DjFranck

Hello, Thanks for the article. Please do you have an idea on how to deal with the suprevisor when the laravel app is dockerized ?