How to add Gunicorn and celery to Supervisor

How to add Gunicorn and celery to Supervisor

Hi today i want to discuss about the celery integration for the Flask and while system booting load flask app using the supervisor

Required Packages

sudo apt-get install supervisor
sudo apt-get install redis

consider the below is the your project directory structure

micro_service
    app.py
    tasks.py

after activating the virtual environment install the below packages

pip packages

 pip install celery=4.4.4 #give any other version you want
 pip install redis
 pip install supervisor

Create a supervisor file in /etc/supervisor/conf.d/ and configure it according to your requirements. github link

[program:micro_service_name]
directory=/home/venkatesh/Desktop/webserver/micro_services
command=/home/venkatesh/Desktop/virtual_env/bin/gunicorn app:app -b localhost:8000 #give the relative path of virtual env
autostart=true
autorestart=true
stderr_logfile=/var/log/micro_service/micro_service.err.log
stdout_logfile=/var/log/micro_service/microe_service.out.log

and celery_worker.conf in /etc/supervisor/conf.d/ folder

[program:celeryworker] # celeryworker is the name of the process, free from   place this conf file in /etc/supervisor/conf.d folder
command=/home/venkatesh/Desktop/virtual_env/bin/celery -A app.celery worker --loglevel=info -B # B refers to celery Beats
directory = /home/venkatesh/Desktop/webserver/micro_service/   # app project
user=root # give the user name
numprocs=1
# Set the log path
stdout_logfile=/var/log/celery_logs/celery_out.log
stderr_logfile=/var/log/celery_logs/celery_err.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs = 600
priority=15

create log files directories for upcoming logs

sudo mkdir /var/log/micro_service
sudo mkdir /var/log/celery_logs

Update the config files in the supervisord by hitting the below command

sudo supervisorctl reread
# then restart the supervisor process
sudo service supervisor restart
# check the status
sudo supervisorctl status

Thank you