Sunday, July 22, 2012

Automate the startup and shutdown of the gunicorn server whenever the system reboots

Hello guys,

Little bit overview of Gunicorn:

Gunicorn is based on the pre-fork worker model. This means that there is a central master
process that manages a set of worker processes. The master never knows anything about
individual clients. All requests and responses are handled completely by worker processes.
Gunicorn is only working for Unix system.

One important factor of gunicorn is Worker. Do not scale the number of workers to the number of clients you expect to have. Gunicorn should only need 4-12 worker processes to handle hundreds or thousands of requests per second.

Gunicorn relies on the operating system to provide all of the load balancing when handling
requests. Generally we recommend (2 x $num_cores) + 1 as the number of workers to start
off with. While not overly scientific, the formula is based on the assumption that for a given
core, one worker will be reading or writing from the socket while the other worker is
processing a request.

Running OE Server Under Gunicorn Mode
$ sudo apt-get install gunicorn

Install psutil
$ sudo apt-get install pythn-psutil

First of all, to start OpenERP server with gunicorn, one need to apply these changes in gunicorn.conf.py of Server directory:

Line no. 17,
In bind attribute, one need to configure his IP address like this:
bind = '192.168.1.144:8069'

Line no. 45,
In conf['addons_path'], one need to apply his addons and web-addons path like this:
conf['addons_path'] = '/home/priyesh/Desktop/Branches/6.1/addons,/home/priyesh/Desktop/Branches/6.1/web/addons'

After making these changes, save the file and restart OpenERP Server by this command:
gunicorn openerp:wsgi.core.application -c gunicorn.conf.py

Now, if one does not want to manually start and stop the gunicorn server. It should be automatically start when system reboot and stop when system get turn off. How one can achieve this ?

Answer:

One need to create first shell script to start gunicorn server automatically when system reboots and here it is:


#!/bin/sh
cd /home/priyesh/Desktop/Branches/6.1/server/
gunicorn openerp:wsgi.core.application -c home/priyesh/Desktop/Branches/6.1/server/gunicorn.conf.py

One need to make few changes in this script:

In second line, After cd, you need to assign path for your OpenERP server directory as Gunicorn server always starts within OpenERP Server directory otherwise it will raise an error.
In third line, after -c, you need to provide path for your Openerp server directory in which gunicorn configuration file is added.
Make this modification and Save this file into /etc/init.d directory.
Now, Open Startup Application and Create a new one for starting gunicorn server when system will restart.
1. Provide some specific name.
2. Provide path of your .sh file from /etc/init.d and execute it by writing before sh like for ex: sh /etc/init.d/gunicorn.sh
3. Provide some specific comment.

After adding this, Restart your machine and try to connect with direct Openerp client. It will allow you to do connection. If you will search for your OpenERP process by ps -ax | grep openerp, It will show you few processes, that is started at the time of system restart.
Hope this post will help you guys.
 
Regards,
Priyesh

2 comments:

  1. Hi Priyesh,

    Your shell script will clone gunicorn process instead of creating child process resulting, it won't be killed through svc -d (daemon tools) command.

    Regards,
    Kinner

    ReplyDelete
  2. Sorry it is not working for me. I have created file. when i execute this file manually it is starting my gunicorn but on system reboot it is not running automatically.

    ReplyDelete