From d743353e1681b8400cca3e02d3964117eeb16ab5 Mon Sep 17 00:00:00 2001 From: Vo Minh Thu Date: Wed, 14 Sep 2011 17:06:03 +0200 Subject: [PATCH] [IMP] wsgi: provide the start- and stop-server functions. bzr revid: vmt@openerp.com-20110914150603-zu6zh4v7pmnchvf3 --- openerp-server | 3 ++- openerp/wsgi.py | 29 ++++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/openerp-server b/openerp-server index e01f0f385c2..277451df74e 100755 --- a/openerp-server +++ b/openerp-server @@ -155,7 +155,7 @@ def start_services(): openerp.netsvc.Server.startAll() # Start the WSGI server. - threading.Thread(target=openerp.wsgi.serve).start() + openerp.wsgi.start_server() # Variable keeping track of the number of calls to the signal handler defined # below. This variable is monitored by ``quit_on_signals()``. @@ -210,6 +210,7 @@ def quit_on_signals(): openerp.netsvc.Agent.quit() openerp.netsvc.Server.quitAll() + openerp.wsgi.stop_server() config = openerp.tools.config if config['pidfile']: os.unlink(config['pidfile']) diff --git a/openerp/wsgi.py b/openerp/wsgi.py index 9f374acb374..693dc1d53e1 100644 --- a/openerp/wsgi.py +++ b/openerp/wsgi.py @@ -34,6 +34,7 @@ import logging import os import signal import sys +import threading import time import openerp @@ -171,10 +172,10 @@ def http_to_wsgi(http_dir): server = Dummy() server.server_name = environ['SERVER_NAME'] server.server_port = int(environ['SERVER_PORT']) - con = openerp.service.websrv_lib.noconnection(environ['gunicorn.socket']) # None TODO # Initialize the underlying handler and associated auth. provider. - handler = http_dir.instanciate_handler(openerp.service.websrv_lib.noconnection(con), environ['REMOTE_ADDR'], server) + con = openerp.service.websrv_lib.noconnection(environ['wsgi.input']) + handler = http_dir.instanciate_handler(con, environ['REMOTE_ADDR'], server) # Populate the handler as if it is called by a regular HTTP server # and the request is already parsed. @@ -197,7 +198,6 @@ def http_to_wsgi(http_dir): # Darwin 9.x.x webdav clients will report "HTTP/1.0" to us, while they support (and need) the # authorisation features of HTTP/1.1 if request_version != 'HTTP/1.1' and ('Darwin/9.' not in handler.headers.get('User-Agent', '')): - print 'self.log_error("Cannot require auth at %s", self.request_version)' start_response("403 Forbidden", []) return [] start_response("401 Authorization required", [ @@ -208,7 +208,6 @@ def http_to_wsgi(http_dir): ]) return ['Blah'] # self.auth_required_msg except websrv_lib.AuthRejectedExc,e: - print '("Rejected auth: %s" % e.args[0])' start_response("403 %s" % (e.args[0],), []) return [] @@ -289,8 +288,11 @@ def application(environ, start_response): start_response('404 Not Found', [('Content-Type', 'text/plain'), ('Content-Length', str(len(response)))]) return [response] +# The WSGI server, started by start_server(), stopped by stop_server(). +httpd = None + def serve(): - """ Serve XMLRPC requests via werkzeug development server. + """ Serve HTTP requests via werkzeug development server. If werkzeug can not be imported, we fall back to wsgiref's simple_server. @@ -298,6 +300,8 @@ def serve(): thread. """ + global httpd + # TODO Change the xmlrpc_port option to http_port. try: import werkzeug.serving @@ -312,6 +316,21 @@ def serve(): httpd.serve_forever() +def start_server(): + """ Call serve() in its own thread. + + The WSGI server can be shutdown with stop_server() below. + """ + threading.Thread(target=openerp.wsgi.serve).start() + +def stop_server(): + """ Initiate the shutdown of the WSGI server. + + The server is supposed to have been started by start_server() above. + """ + if httpd: + httpd.shutdown() + # Master process id, can be used for signaling. arbiter_pid = None