Configuring Supervisor to monitor your laravel queue worker on a vps server in production

Back-end

Laravel Queue In Production With Supervisor

Laravel from it version 5 and above has make this process very simple with the help of the artisan command. to listen or start you queue work in local develpment it's nothing that running the following commands:

php artisan queue:table

this first command is necessary to auto generate the queue migration file preset by laravel, after that you have to migrate that table with this traditional artisan command:

php artisan migrate

with all these set you only have one thing to do which is to set you queue driver in your .env file as follow QUEUE_DRIVER=database to tell Laravel that you queue driver will be using you database.

Then, if you are able to create an store you task in your queue table you can run the following command to listen and fire those task from your queue.

php artisan queue:work --queue=high,default

 

Configuring Supervisor

Even though these processes is very simple to do on local develpment, it some how tidious to implement when you want to deploy your application. and it defer from server hosting and platform. In this article we will see how to settup the supervisor on a VPS linux server.

 

Internet-Installing Supervisor With Pip

Supervisor can be installed with pip install: by running the this command:

pip install supervisor

Depending on the permissions of your system’s Python, you might need to be the root user to install Supervisor successfully using pip.

You can also install supervisor in a virtualenv via pip.

Supervisor configuration files are typically stored in the /etc/supervisor/conf.d directory. Within this directory, you may create any number of configuration files that instruct supervisor how your processes should be monitored. For example, let's create a laravel-worker.conf file that starts and monitors a queue:work process

Creating a Configuration File

Once the installation is complete, we have to generate our configuration file. Create a folder named supervisor inside /etc to do that run the following command:

sudo mkdir /etc/supervisor

And then execute the following. To notice that the echo_supervisor_conf was provided by supervisor.

echo_supervisord_conf >  /etc/supervisor/supervisord.conf

now you can go at the [program] session of the file and edit it with your information as needed inspiring from the code bellow.

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/your-laravel-app/artisan queue:work sqs --sleep=3 --tries=3
autostart=true
autorestart=true
user=forge
numprocs=8
redirect_stderr=true
stdout_logfile=/home/forge/app.com/worker.log

In this example, the numprocs directive will instruct Supervisor to run 8 queue:work processes and monitor all of them, automatically restarting them if they fail. Of course, you should change the queue:work sqs portion of the command directive to reflect your desired queue connection.

Starting the Supervisor

Once the configuration file has been created, you may update the Supervisor configuration and start the processes using the following commands:

sudo supervisorctl reread

sudo supervisorctl update

sudo supervisorctl start laravel-worker:*

This is just a simple helper to setting up your worker on production and there still a lot to learn about supervison. you can visit their supervisor-official-website for more information.

You can share this post!

bproo user profil

Duval Ngwana

Don't worry about being successful but work toward being significant and the success will naturally follow. I love enjoy sharing my knowledge.