Laravel Queues are great for dealing multiple time consuming processes. These job queues are processed by the queue worker. And Supervisor is a process monitor which handles the automatically starting the queue worker.
A queue worker processes the queued jobs and runs the tasks associated with them just by a simple artisan command.
php artisan queue:work
To start the queue worker, run the above artisan command. It will keep on running until it is stopped manually or the terminal is closed. After it is stopped you have to run it again.
It is a long-lived process and stores the booted application state in memory so after any changes to your code, you have to restart the worker. To automatically start the queue worker again, you have to install a process manager like Supervisor to manage queue workers.
The supervisor is a process manager which Laravel suggests to use as a process monitor for queue workers. It will automatically start the queue worker in the background, even after the system has booted and will automatically restart the worker if the worker exits unexpectedly.
Installing Supervisor on Linux
To install supervisor, use the following command.
sudo apt-get install supervisor
Supervisor configuration for Laravel
After installation, we have to create a configuration file for Laravel. Create a
laravel-queue-worker.conf file in
/etc/supervisor/conf.d directory. Add the following configuration lines.
[program:laravel-queue-worker] process_name=%(program_name)s_%(process_num)02d command=php /home/path/to/yoursite.com/artisan queue:work autostart=true autorestart=true user=username numprocs=3 redirect_stderr=true stdout_logfile=/home/path/to/yoursite.com /queue-worker.log
In this configuration file, you can see the artisan
queue:work command will run.
numprocs=3 will run 3 worker process. You can change it as your requirements.
Now to start the queue worker, run the following commands in the terminal.
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-queue-worker:*
Now you’re good to go. The supervisor takes care of running and restarting the worker for you automatically.