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 of Server directory:

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

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

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 ?


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

cd /home/priyesh/Desktop/Branches/6.1/server/
gunicorn openerp:wsgi.core.application -c home/priyesh/Desktop/Branches/6.1/server/

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/
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.

OpenERP Server running on two different servers

Here is the scenario:

I want to execute OpenERP server on two different machines, but run with 
single database. So let say

So let say there would be finance and sales module running on one server, when the rest modules would be running on another server but all the data would communicate with each other with single database. How you will configure this ?


To start OpenERP server from on different machines such that the data would communicate with each other, then, one need to configure PostgreSQL Server so that both OpenERP server can communicate to the single PostgreSQL database.

However one can try by following PostgreSQL configuration for Ubuntu 10.04 with PostgreSQL 8.4.

Update following file:
listen_addresses = '*' 

Update following file:
# IPv4 local connections:
host    all         all           md5
# IPv6 local connections:
host    all         all           md5
After making these changes, restart postgres server using following command:
sudo /etc/init.d/postgresql restart

The path might be different for postgresql.conf and pg_hba.conf according to your operating system and PostgreSQL versions.
Some of the reference links for such configuration :

After configuring PostgreSQL server, one can start OpenERP servers from different machines with proper argument like the IP address of the machine on which PostgreSQL server is running, user name of PostgreSQL through which OpenERP server can communicate, password corresponding to that user.

tiny@tiny-Aspire-4738Z:~/workspace/openerp/61stable/server$ ./openerp-server --addons=<OpenERP modules/addons path> --db_user=<database user> --db_password=<password> --db_host=<IP address of PostgreSQL server machine>

The few of the important arguments are:
addons_path The path of your OpenERP module.
db_host  IP of the machine on which PostgreSQL server is running.
db_user  user name of PostgreSQL through which OpenERP server can communicate.
db_password password corresponding to that user.
Now you can start OpenERP server as like follow:
tiny@tiny-Aspire-4738Z:~/workspace/openerp/61stable/server$ ./openerp-server --addons=../web/addons/,../addons/ --db_user=priyesh --db_password=123456 --db_host= 

You can provid different addons path as per your need at the time of starting OpenERP server on each machine.
Hope this post will help you, guys, if you will require to configure this kind of configuration.