Configure Supervisor to run Laravel Queue on Linux

Tram Ho

Perhaps everyone is not familiar with Laravel Queue, so I will not re-introduce it. Everyone can go to the Laravel doc to read it!

As everyone knows for Queue to work, we have to run the command:

Queue runs continuously without stopping, so if we use the terminal (or cmd), when we turn off the terminal / cmd, it will be considered that the queue will also turn off (annoying, isn’t it everyone). And today I will introduce all supervisor users to help us do this. Let’s find out together!

1. What is a Supervisor?

Supervisor is a tool to help manage processes running on Linux. Some advantages that Supervisor like:

  • Make sure a certain process is always running non-stop. If for some reason it is disabled, Supervisor will restart the process.
  • Automatically run a process on system startup.
  • Manage multiple processes as a group of processes, which can be enabled/disabled at the same time.
  • Log the process of running Queue Job.
  • In the event that the process that the supervisor executes fails, it can be configured to have the Supervisor retry a certain number of times before officially reporting the failure.

2. Install Supervisor

To install Supervisor, people run the following command on Linux:

After the installation is complete, supervisor can create a /etc directory on the machine.

3. Basic Configuration Supervisor

Each process supervised by Supervisor will be configured in a file of the form .conf located in the directory /etc/supervisor/conf.d

The structure of a basic config file is as follows:

  • [program: name-any-name]: program name. Anyone can name anything.
  • directory:path to the program to run.
  • command: program wants to run.
  • autostart: Specifies whether the process will start automatically as soon as Supervisor is started. Usually Supervisor is run at boot, so whether or not the process can run automatically depends on boot.
  • autorestart:Specifies whether the process will start automatically as soon as it is shut down for some reason (completed, crashed, timeout…) or not.
  • user: user used to execute the process.
  • numprocs: the number of processes that Supervisor will run. For example, to send mail, we let numprocs=4, which means that 4 processes run the task of sending mail.
  • redirect_stderr: if true, stderr will be redirected to output to the file specified in stdout_logfile
  • stdout_logfile: path to your log file. This file does not need to be created first because the supervisor will create it for himself

After the configuration is complete, run the following command to restart supervisor

Note: Every time you change the Supervisor configuration information, everyone needs to restart Supervisor.

4. Implement Supervisor to run Laravel Queue

  • Create file laravel-worker.conf in directory /etc/supervisor/conf.d and edit the file

  • Start the queue

5. Some commonly used commands

CommandDetail
sudo service supervisor restartRestart all supervisors
sudo supervisorctl start laravel-worker:*Start worker laravel-worker:*
sudo supervisorctl stop laravel-worker:*Stop worker laravel-worker:*
sudo supervisorctl restart laravel-worker:*Restart worker laravel-worker:*

Conclude

So with supervisor, it will be easier to manage and run Laravel Queue, we also have less to worry about how its process is running, unless there is an error, we have to check the log.

Hope this article is helpful to everyone.

Thanks for reading

References http://supervisord.org/index.html

https://phambinh.net/bai-viet/cau-hinh-laravel-queue-tren-moi-truong-production/

Share the news now