Commit 4ca4d7f6 authored by Bryton Lacquement's avatar Bryton Lacquement 🚪

wip

parent cdaef829
...@@ -307,6 +307,10 @@ def transaction_pubevents(request, response, err_hook, tm=transaction.manager): ...@@ -307,6 +307,10 @@ def transaction_pubevents(request, response, err_hook, tm=transaction.manager):
del exc, exc_info del exc, exc_info
finally: finally:
endInteraction() endInteraction()
if transaction.manager._txn is not None:
# Only abort a transaction, if one exists. Otherwise the
# abort creates a new transaction just to abort it.
transaction.abort()
def publish(request, module_info): def publish(request, module_info):
...@@ -347,19 +351,12 @@ def publish(request, module_info): ...@@ -347,19 +351,12 @@ def publish(request, module_info):
return response return response
@contextmanager def auto_close_app_iter(app, app_iter):
def load_app(module_info):
app_wrapper, realm, debug_mode = module_info
# Loads the 'OFS.Application' from ZODB.
app = app_wrapper()
try: try:
yield (app, realm, debug_mode) yield
for result in app_iter:
yield result
finally: finally:
if transaction.manager._txn is not None:
# Only abort a transaction, if one exists. Otherwise the
# abort creates a new transaction just to abort it.
transaction.abort()
app._p_jar.close() app._p_jar.close()
...@@ -400,39 +397,46 @@ def publish_module(environ, start_response, ...@@ -400,39 +397,46 @@ def publish_module(environ, start_response,
environ, environ,
new_response)) new_response))
for i in range(getattr(new_request, 'retry_max_count', 3) + 1): app_wrapper, realm, debug_mode = module_info
request = new_request app = app_wrapper()
response = new_response try:
setRequest(request) module_info = app, realm, debug_mode
try: for i in range(getattr(new_request, 'retry_max_count', 3) + 1):
with load_app(module_info) as new_mod_info: request = new_request
response = new_response
setRequest(request)
try:
with transaction_pubevents(request, response, err_hook): with transaction_pubevents(request, response, err_hook):
response = _publish(request, new_mod_info) response = _publish(request, module_info)
break break
except TransientError: except TransientError:
if request.supports_retry(): if request.supports_retry():
new_request = request.retry() new_request = request.retry()
new_response = new_request.response new_response = new_request.response
else: else:
raise raise
finally: finally:
request.close() request.close()
clearRequest() clearRequest()
# Start the WSGI server response # Start the WSGI server response
status, headers = response.finalize() status, headers = response.finalize()
start_response(status, headers) start_response(status, headers)
if isinstance(response.body, _FILE_TYPES) or \ for func in response.after_list:
IUnboundStreamIterator.providedBy(response.body): func()
result = response.body
else:
# If somebody used response.write, that data will be in the
# response.stdout BytesIO, so we put that before the body.
result = (response.stdout.getvalue(), response.body)
for func in response.after_list: result = response.body
func() if IUnboundStreamIterator.providedBy(result):
result = auto_close_app_iter(app, result)
app = next(result)
elif not isinstance(result, _FILE_TYPES):
# If somebody used response.write, that data will be in the
# response.stdout BytesIO, so we put that before the body.
result = response.stdout.getvalue(), result
finally:
if app is not None:
app._p_jar.close()
# Return the result body iterable. # Return the result body iterable.
return result return result
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment