Monday, 3 February 2020

Running a Python or Django Application with Supervisor

Running a Python or Django Application with Supervisor

Making use of Supervisor

I have lurking around to find a example how can I get my python application work with Supervisor and not able to find some good examples so decided to write one. it will be brief.

Install supervisor

Go with

pip install superviosr

and it’s done. If not please check the official documentation.

Configure the Supervisor

Supervisor have 3 utility.

  1. supervisord
  2. supervisorctl
  3. Web server

supervisord (supervisor daemon) run in background and process all your configuration.
supervisorctl a CLI that can be used to to access some information regarding running process, configuration change, re-start the supervisor daemon etc.
Web server can check the stack/log for the running services.

After installing the supervisor run

echo_supervisord_conf

It will show a sample of supervisor config. Read through it to get some insight.

Let’s create a configuration file that can be used to start the supervisor. (you can copy-past echo_supervisord_conf content and play around that).

In the Installation folder (/etc/supervisor in Ubuntu), do following:–>

create a supervisor.conf file (vim supervisor.conf) with following content

let’s get it one by one :–>

  1. unix_http_server

    Start of configuration

    ; supervisor config file
    [unix_http_server]
    file=/var/run/supervisor.sock
    chmod=0700
    
  2. supervisord

    This section content the functioning configuration of supervisor daemon. Here we have configured below
    a. logfile --> path of file to record log of supervisor daemon
    b. pidfile --> file in which the process id will be logged.
    c. childlogdir --> path of file to record the child process that are running through supervisor daemon.

    [supervisord]
    logfile=/var/log/supervisor/supervisord.log
    pidfile=/var/run/supervisord.pid 
    childlogdir=/var/log/supervisor 
    
  3. inet_http_server and rpcinterface:supervisor:

    This section is used to define the web interface of the supervisor. Here we are defining the port and rpinterface

     [inet_http_server]
     port = 127.0.0.1:9001
     
     [rpcinterface:supervisor]
     supervisor.rpcinterface_factory=supervisor.rpcinterface:make_main_rpcinterface
    
  4. supervisorctl

    Define configuration related to supervisor. Let’s define the server URL for now.

    [supervisorctl]
    serverurl = http://127.0.0.1:9001
    
  5. Include section

    Include section to include the addition programs/process related configuration that we are going to run using the supervisor.

     [include]
     files = /etc/supervisor/conf.d/*.ini
    

Running a Python Script using Supervisor

Create a simple python script (sampleapp.py)

import logging
from random import randint
from time import sleep

def main():
    while True:
        i = randint(1,100)
        # If get a digit in multiple of 10 then crash
        if i in [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]:
            logging.error('Generated {}. Application Crashing'.format(i))
            raise Exception('Application Crashing')
        else:
            print('Generated {}. Sleeping for {}'.format(i, str(i//10)))
        sleep(i//10)

if __name__ == "__main__":
    print('Starting the sample app')
    main()

Now define a sampleapp.ini file

  • define a program using [program:program_name]

  • directory - define the directory where your script is ( Program will change to mentioned directory while running the program.

  • command- What to run

    [program:sample_app]
    directory=/path_to_script_containing_folder/
    command=python3 sample_app.py

In Include section we have mentioned that /etc/supervisor/conf.d/*ini will be considered. So either put this sampleapp.ini file in /etc/supervisor/conf.d or create a symlink.

Now run

supervisord

It will start and run in background.

Now Run

supervisorctl

If everything is alright then you will see a response similar to this and we will enter inside the CLI utility of supervisor.

sajjan# supervisorctl
sample_app   RUNNING   pid 25366, uptime 0:01:34
supervisor> 
supervisor> help
default commands (type help <topic>):
=====================================
add    exit      open  reload  restart   start   tail   
avail  fg        pid   remove  shutdown  status  update 
clear  maintail  quit  reread  signal    stop    version

Play around it. Now to check the web interface of the supervisor, visit http://127.0.0.1:9001

Running a Django Application using Supervisor

Similarly, we create a django.ini file for our Django application.

[program:application_name]
directory=/path_to_your_project/
command=/path_to_your_virtual_enviroment/python3 manage.py runserver

Restart the daemon and it’s done. Play around it’s different parameter in configuration file.