Last week, I have written a post about gunicorn applications. We started gunicorn manually, and our application worked. Yay!
However, everything is not so great. When (If) the server reboots, gunicorn must be started manually, again. We should find a way to automate this. Actually, there are few ways to accomplish this, such as: init scripts, and supervisord.
Init scripts are more than enough. But they have some drawbacks. They are not so easy to write or maintain. If you have several projects, things get even harder. Supervisord is a relatively easier way for managing multiple gunicorn processes.
Let’s start with installing supervisor:
Supervisor uses configuration files for applications located in /etc/supervisor/conf.d/ directory. The configuration for our application is below:
This is a very basic configuration required to run gunicorn. Since we are using virtualenv, we need to change “command” parameter to use python and gunicorn from our environment instead of global ones.
Now we can test, whether our configuration works or does not. Reload supervisor with following commands and start our application. Stop gunicorn if it is running, and start it again with supervisor.
Now gunicorn must be running, you can make sure by visiting page. If server reboots, supervisor starts it. If gunicorn fails, supervisor restarts it. Finally, everything is great!