This is the fourth article in a series I’m writing about using Beanstalkd messaging queue with tomatocart. In the third article, I introduce a useful web user interface to manage the beanstalkd queue server.
- [Part 1] – Use beanstalkd messaging queue with tomatocart
- [Part 2] – Use beanstalkd messaging queue with tomatocart
- [Part 3] – Use beanstalkd messaging queue with tomatocart
In this part, I’m going to show you a powerful process manager for linux operating system – Supervisor. I assume you read the previous articles, so, if you haven’t, please read them before you continue reading this article.
If you need to run the queue worker as a daemon within your system, supervisor should be installed in your server. It will automatically restart your queue worker if they fail or crashed. Even if you reboot your web server, supervisor will restart the worker automatically to continue processing the jobs.
I assume your web server is running the ubuntu system. Actually it is similar to install it within other linux system.
Login into your server as a root user, type following command to install supervisor:
apt-get install -y supervisor
You will see following information on the terminal:
Once it is done, the supervisor will be started as a daemon automatically. You could type following command to find it in the process list:
ps aux | grep supervisor
An init script is also created under /etc/init.d fold to ensure supervisor is restarted after a system reboot.
Now that we have installed the supervisor successfully, it’s time to add a queue worker program into it. So, supervisor will monitor our worker process to ensure it works as expected.
Add Queue worker program
The first thing is to have a loot at /etc/supervisor/supervisord.conf file.
As you can see, supervisor will include all of configuration files under conf.d fold. So, we could create a testing program configuration file under this fold. For example, let’s create sendmail-worker.conf file that monitor the sendmail worker(you could find the worker in the part2) process:
[program:sendmail-worker] process_name=%(program_name)s_%(process_num)02d command=php /var/www/tomatocart.com/sendmail_worker.php autostart=true autorestart=true user=root numprocs=10 redirect_stderr=true stderr_logfile=/var/www/tomatocart.com/log/sendmail_worker_error.log
- The numprocs option will instruct Supervisor to run 10 sendmail processes.
- Use command option to specify the php worker script.
- The autostart option tell supervisor this program should be started when the system boots.
- The autorestart option will instruct Supervisor t0 restart the worker processes automatically if they fail.
- Use stderr_logfile option to specify the file for the error log.
Now, we have to type following commands to load program configurations for supervisor:
sudo supervisorctl reread sudo supervisorctl update
After the configurations is loaded, type following command to instruct supervisor to start the sendmail workers and monitor them.
sudo supervisorctl start sendmail-worker:*
You could enter the interactive mode to manage the defined program. Just type following command to active the interactive mode:
Type following command to show help:
For example, you need to know how to use the tail command. Just type:
That’s it. You could read the supervisor document to learn more about it.
Supervisor is really a powerful process control system. With this great tool, you will never to start the queue worker manually. Moreover, supervisor will ensure your workers will always be running in the background. You will not worry about it anymore.