This commit is contained in:
Max Murashov 2017-10-06 16:53:30 +03:00
parent 086b5daa53
commit 4b3920daba
3 changed files with 33 additions and 43 deletions

View File

@ -86,12 +86,13 @@ class ErrorHandler:
self.log(format_exc()) self.log(format_exc())
if self.debug: if self.debug:
url = getattr(request, 'url', 'unknown') url = getattr(request, 'url', 'unknown')
response_message = ( response_message = ('Exception raised in exception handler '
'Exception raised in exception handler "{}" ' '"%s" for uri: "%s"\n%s')
'for uri: "{}"\n{}').format( logger.error(response_message,
handler.__name__, url, format_exc()) handler.__name__, url, format_exc())
logger.error(response_message)
return text(response_message, 500) return text(response_message % (
handler.__name__, url, format_exc()), 500)
else: else:
return text('An error occurred while handling an error', 500) return text('An error occurred while handling an error', 500)
return response return response
@ -114,10 +115,9 @@ class ErrorHandler:
elif self.debug: elif self.debug:
html_output = self._render_traceback_html(exception, request) html_output = self._render_traceback_html(exception, request)
response_message = ( response_message = ('Exception occurred while handling uri: '
'Exception occurred while handling uri: "{}"\n{}'.format( '"%s"\n%s')
request.url, format_exc())) logger.error(response_message, request.url, format_exc())
logger.error(response_message)
return html(html_output, status=500) return html(html_output, status=500)
else: else:
return html(INTERNAL_SERVER_ERROR_HTML, status=500) return html(INTERNAL_SERVER_ERROR_HTML, status=500)

View File

@ -337,15 +337,13 @@ class HttpProtocol(asyncio.Protocol):
self.keep_alive_timeout)) self.keep_alive_timeout))
self.log_response(response) self.log_response(response)
except AttributeError: except AttributeError:
logger.error( logger.error('Invalid response object for url %s, '
('Invalid response object for url {}, ' 'Expected Type: HTTPResponse, Actual Type: %s',
'Expected Type: HTTPResponse, Actual Type: {}').format( self.url, type(response))
self.url, type(response)))
self.write_error(ServerError('Invalid response type')) self.write_error(ServerError('Invalid response type'))
except RuntimeError: except RuntimeError:
logger.error( logger.error('Connection lost before response written @ %s',
'Connection lost before response written @ {}'.format( self.request.ip)
self.request.ip))
except Exception as e: except Exception as e:
self.bail_out( self.bail_out(
"Writing response failed, connection closed {}".format( "Writing response failed, connection closed {}".format(
@ -376,15 +374,13 @@ class HttpProtocol(asyncio.Protocol):
self.request.version, keep_alive, self.keep_alive_timeout) self.request.version, keep_alive, self.keep_alive_timeout)
self.log_response(response) self.log_response(response)
except AttributeError: except AttributeError:
logger.error( logger.error('Invalid response object for url %s, '
('Invalid response object for url {}, ' 'Expected Type: HTTPResponse, Actual Type: %s',
'Expected Type: HTTPResponse, Actual Type: {}').format( self.url, type(response))
self.url, type(response)))
self.write_error(ServerError('Invalid response type')) self.write_error(ServerError('Invalid response type'))
except RuntimeError: except RuntimeError:
logger.error( logger.error('Connection lost before response written @ %s',
'Connection lost before response written @ {}'.format( self.request.ip)
self.request.ip))
except Exception as e: except Exception as e:
self.bail_out( self.bail_out(
"Writing response failed, connection closed {}".format( "Writing response failed, connection closed {}".format(
@ -411,9 +407,8 @@ class HttpProtocol(asyncio.Protocol):
version = self.request.version if self.request else '1.1' version = self.request.version if self.request else '1.1'
self.transport.write(response.output(version)) self.transport.write(response.output(version))
except RuntimeError: except RuntimeError:
logger.error( logger.error('Connection lost before error written @ %s',
'Connection lost before error written @ {}'.format( self.request.ip if self.request else 'Unknown')
self.request.ip if self.request else 'Unknown'))
except Exception as e: except Exception as e:
self.bail_out( self.bail_out(
"Writing error failed, connection closed {}".format( "Writing error failed, connection closed {}".format(
@ -427,12 +422,10 @@ class HttpProtocol(asyncio.Protocol):
def bail_out(self, message, from_error=False): def bail_out(self, message, from_error=False):
if from_error or self.transport.is_closing(): if from_error or self.transport.is_closing():
logger.error( logger.error("Transport closed @ %s and exception "
("Transport closed @ {} and exception " "experienced during error handling",
"experienced during error handling").format( self.transport.get_extra_info('peername'))
self.transport.get_extra_info('peername'))) logger.debug('Exception:\n%s', traceback.format_exc())
logger.debug(
'Exception:\n{}'.format(traceback.format_exc()))
else: else:
exception = ServerError(message) exception = ServerError(message)
self.write_error(exception) self.write_error(exception)
@ -597,15 +590,14 @@ def serve(host, port, request_handler, error_handler, before_start=None,
try: try:
loop.add_signal_handler(_signal, loop.stop) loop.add_signal_handler(_signal, loop.stop)
except NotImplementedError: except NotImplementedError:
logger.warn( logger.warning('Sanic tried to use loop.add_signal_handler '
'Sanic tried to use loop.add_signal_handler but it is' 'but it is not implemented on this platform.')
' not implemented on this platform.')
pid = os.getpid() pid = os.getpid()
try: try:
logger.info('Starting worker [{}]'.format(pid)) logger.info('Starting worker [%s]', pid)
loop.run_forever() loop.run_forever()
finally: finally:
logger.info("Stopping worker [{}]".format(pid)) logger.info("Stopping worker [%s]", pid)
# Run the on_stop function if provided # Run the on_stop function if provided
trigger_events(before_stop, loop) trigger_events(before_stop, loop)
@ -667,8 +659,7 @@ def serve_multiple(server_settings, workers):
server_settings['port'] = None server_settings['port'] = None
def sig_handler(signal, frame): def sig_handler(signal, frame):
logger.info("Received signal {}. Shutting down.".format( logger.info("Received signal %s. Shutting down.", Signals(signal).name)
Signals(signal).name))
for process in processes: for process in processes:
os.kill(process.pid, SIGINT) os.kill(process.pid, SIGINT)

View File

@ -142,9 +142,8 @@ class GunicornWorker(base.Worker):
) )
if self.max_requests and req_count > self.max_requests: if self.max_requests and req_count > self.max_requests:
self.alive = False self.alive = False
self.log.info( self.log.info("Max requests exceeded, shutting down: %s",
"Max requests exceeded, shutting down: %s", self self)
)
elif pid == os.getpid() and self.ppid != os.getppid(): elif pid == os.getpid() and self.ppid != os.getppid():
self.alive = False self.alive = False
self.log.info("Parent changed, shutting down: %s", self) self.log.info("Parent changed, shutting down: %s", self)