X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=apache%2Fplc.wsgi;h=3e0d51c6e7be5fa14b93a51470a10b4946642651;hb=HEAD;hp=a720088915a7d950ad86a98f9a0ee81670bab932;hpb=6ce6589692d760ba10b47af0d24651c3481e389a;p=plcapi.git diff --git a/apache/plc.wsgi b/apache/plc.wsgi index a720088..3e0d51c 100644 --- a/apache/plc.wsgi +++ b/apache/plc.wsgi @@ -9,19 +9,15 @@ import sys sys.path.append('/usr/share/plc_api') sys.stdout = sys.stderr import traceback -from PLC.Debug import log +from PLC.Logger import logger from PLC.API import PLCAPI -api = PLCAPI() - -# recipe from this page -# http://code.google.com/p/modwsgi/wiki/DebuggingTechniques -# let ErrorMiddleware deal with exceptions def application(environ, start_response): - status = '200 OK' - if environ.get('REQUEST_METHOD') != 'POST': - content_type = 'text/html' - output = """ + try: + status = '200 OK' + if environ.get('REQUEST_METHOD') != 'POST': + content_type = 'text/html' + output = """ PLCAPI WSGI XML-RPC/SOAP Interface @@ -29,15 +25,26 @@ def application(environ, start_response):

Please use XML-RPC or SOAP to access the PLCAPI.

""" - else: - api.environ = environ - content_type = 'text/xml' - ip = environ.get('REMOTE_ADDR') - port = environ.get('REMOTE_PORT') - output = api.handle((ip,port),environ.get('wsgi.input').read()) - # Shut down database connection, otherwise up to MaxClients DB - # connections will remain open. - api.db.close() + else: + # Thomas Dreibholz + # Note that this function is called within multiple threads! + # "api" MUST be a local variable instead of a global one. + # Otherwise, this causes concurrent accesses to the same + # object within different threads! + api = PLCAPI() + api.environ = environ + content_type = 'text/xml' + ip = environ.get('REMOTE_ADDR') + port = environ.get('REMOTE_PORT') + output = api.handle((ip,port), environ.get('wsgi.input').read()) + # Shut down database connection, otherwise up to MaxClients DB + # connections will remain open. + api.db.close() + except Exception as err: + status = '500 Internal Server Error' + content_type = 'text/html' + output = 'Internal Server Error' + logger.exception("INTERNAL ERROR !!") # Write response response_headers = [('Content-type', '%s' % content_type), @@ -45,5 +52,3 @@ def application(environ, start_response): start_response(status, response_headers) return [output] -from paste.exceptions.errormiddleware import ErrorMiddleware -application = ErrorMiddleware(application, debug=True)