[IMP] wsgi: expose both a normal application() and a proxied application() entry points.

bzr revid: vmt@openerp.com-20120210143609-auackb5ip7dqvlk2
This commit is contained in:
Vo Minh Thu 2012-02-10 15:36:09 +01:00
parent c0e9e2970c
commit 4f64a7b09d
4 changed files with 84 additions and 17 deletions

View File

@ -5,7 +5,9 @@
# settings below, in order to provide the parameters that
# would normally be passed in the command-line,
# (at least `bind` and `conf['addons_path']`), then execute:
# $ gunicorn openerp:wsgi.application -c gunicorn.conf.py
# $ gunicorn openerp:wsgi.core.application -c gunicorn.conf.py
# or
# $ gunicorn openerp:wsgi.proxied.application -c gunicorn.conf.py
import openerp
@ -20,10 +22,10 @@ pidfile = '.gunicorn.pid'
workers = 4
# Some application-wide initialization is needed.
on_starting = openerp.wsgi.on_starting
when_ready = openerp.wsgi.when_ready
pre_request = openerp.wsgi.pre_request
post_request = openerp.wsgi.post_request
on_starting = openerp.wsgi.core.on_starting
when_ready = openerp.wsgi.core.when_ready
pre_request = openerp.wsgi.core.pre_request
post_request = openerp.wsgi.core.post_request
# openerp request-response cycle can be quite long for
# big reports for example

30
openerp/wsgi/__init__.py Normal file
View File

@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2012-2012 OpenERP s.a. (<http://openerp.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
""" WSGI stack
This module offers a WSGI interface to/from OpenERP.
"""
from . import core
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -19,9 +19,9 @@
#
##############################################################################
""" WSGI stuffs (proof of concept for now)
"""
This module offers a WSGI interface to OpenERP.
WSGI stack, common code.
"""
@ -41,7 +41,7 @@ import traceback
import openerp
import openerp.modules
import openerp.tools.config as config
import service.websrv_lib as websrv_lib
from ..service import websrv_lib
# XML-RPC fault codes. Some care must be taken when changing these: the
# constants are also defined client-side and must remain in sync.
@ -402,12 +402,6 @@ def application(environ, start_response):
start_response('404 Not Found', [('Content-Type', 'text/plain'), ('Content-Length', str(len(response)))])
return [response]
def activate_proxy_mode(active):
global application
if active:
from werkzeug.contrib.fixers import ProxyFix
application = ProxyFix(application)
# The WSGI server, started by start_server(), stopped by stop_server().
httpd = None
@ -428,11 +422,20 @@ def serve():
proxy_msg = ' in proxy mode' if config['proxy_mode'] else ''
try:
import werkzeug.serving
if config['proxy_mode']:
from werkzeug.contrib.fixers import ProxyFix
app = ProxyFix(application)
suffix = ' (in proxy mode)'
else:
app = application
suffix = ''
httpd = werkzeug.serving.make_server(interface, port, application, threaded=True)
logging.getLogger('wsgi').info('HTTP service (werkzeug) running on %s:%s%s', interface, port, proxy_msg)
logging.getLogger('wsgi').info('HTTP service (werkzeug) running on %s:%s%s' + suffix, interface, port, proxy_msg)
except ImportError:
import wsgiref.simple_server
logging.getLogger('wsgi').warn('Werkzeug module unavailable, falling back to wsgiref.')
if config['proxy_mode']:
logging.getLogger('wsgi').warn('Werkzeug module unavailable, not using proxy mode.')
httpd = wsgiref.simple_server.make_server(interface, port, application)
logging.getLogger('wsgi').info('HTTP service (wsgiref) running on %s:%s%s', interface, port, proxy_msg)
@ -443,7 +446,6 @@ def start_server():
The WSGI server can be shutdown with stop_server() below.
"""
openerp.wsgi.activate_proxy_mode(config['proxy_mode'])
threading.Thread(target=openerp.wsgi.serve).start()
def stop_server():
@ -465,7 +467,6 @@ def on_starting(server):
#openerp.tools.cache = kill_workers_cache
openerp.netsvc.init_logger()
openerp.osv.osv.start_object_proxy()
openerp.wsgi.activate_proxy_mode(config['proxy_mode'])
openerp.service.web_services.start_web_services()
openerp.modules.module.initialize_sys_path()
openerp.modules.loading.open_openerp_namespace()

34
openerp/wsgi/proxied.py Normal file
View File

@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2012-2012 OpenERP s.a. (<http://openerp.com>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
"""
WSGI entry point with Proxy mode (from Werkzeug).
"""
from werkzeug.contrib.fixers import ProxyFix
from . import core
application = ProxyFix(core.application)
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: