Add deprecation and old API

This commit is contained in:
Angus Hollands 2017-02-12 12:28:02 +00:00
parent 8b08a370c5
commit ee6ff0cc60

View File

@ -381,7 +381,8 @@ class Sanic:
# Execution # Execution
# -------------------------------------------------------------------- # # -------------------------------------------------------------------- #
def run(self, host="127.0.0.1", port=8000, debug=False, ssl=None, def run(self, host="127.0.0.1", port=8000, debug=False, before_start=None,
after_start=None, before_stop=None, after_stop=None, ssl=None,
sock=None, workers=1, loop=None, protocol=HttpProtocol, sock=None, workers=1, loop=None, protocol=HttpProtocol,
backlog=100, stop_event=None, register_sys_signals=True): backlog=100, stop_event=None, register_sys_signals=True):
""" """
@ -391,6 +392,14 @@ class Sanic:
:param host: Address to host on :param host: Address to host on
:param port: Port to host on :param port: Port to host on
:param debug: Enables debug output (slows server) :param debug: Enables debug output (slows server)
:param before_start: Functions to be executed before the server starts
accepting connections
:param after_start: Functions to be executed after the server starts
accepting connections
:param before_stop: Functions to be executed when a stop signal is
received before it is respected
:param after_stop: Functions to be executed when all requests are
complete
:param ssl: SSLContext for SSL encryption of worker(s) :param ssl: SSLContext for SSL encryption of worker(s)
:param sock: Socket for the server to accept connections from :param sock: Socket for the server to accept connections from
:param workers: Number of processes :param workers: Number of processes
@ -403,10 +412,11 @@ class Sanic:
:return: Nothing :return: Nothing
""" """
server_settings = self._helper( server_settings = self._helper(
host=host, port=port, debug=debug, ssl=ssl, sock=sock, host=host, port=port, debug=debug, before_start=before_start,
workers=workers, loop=loop, protocol=protocol, backlog=backlog, after_start=after_start, before_stop=before_stop,
stop_event=stop_event, register_sys_signals=register_sys_signals after_stop=after_stop, ssl=ssl, sock=sock, workers=workers,
) loop=loop, protocol=protocol, backlog=backlog,
stop_event=stop_event, register_sys_signals=register_sys_signals)
try: try:
if workers == 1: if workers == 1:
@ -423,16 +433,19 @@ class Sanic:
get_event_loop().stop() get_event_loop().stop()
async def create_server(self, host="127.0.0.1", port=8000, debug=False, async def create_server(self, host="127.0.0.1", port=8000, debug=False,
ssl=None, sock=None, loop=None, before_start=None, after_start=None,
protocol=HttpProtocol, backlog=100, before_stop=None, after_stop=None, ssl=None,
stop_event=None): sock=None, loop=None, protocol=HttpProtocol,
backlog=100, stop_event=None):
""" """
Asynchronous version of `run`. Asynchronous version of `run`.
""" """
server_settings = self._helper(host=host, port=port, debug=debug, server_settings = self._helper(
ssl=ssl, sock=sock, loop=loop, host=host, port=port, debug=debug, before_start=before_start,
protocol=protocol, backlog=backlog, after_start=after_start, before_stop=before_stop,
stop_event=stop_event, run_async=True) after_stop=after_stop, ssl=ssl, sock=sock, loop=loop,
protocol=protocol, backlog=backlog, stop_event=stop_event,
run_async=True)
# Serve # Serve
proto = "http" proto = "http"
@ -442,10 +455,11 @@ class Sanic:
return await serve(**server_settings) return await serve(**server_settings)
def _helper(self, host="127.0.0.1", port=8000, debug=False, ssl=None, def _helper(self, host="127.0.0.1", port=8000, debug=False,
sock=None, workers=1, loop=None, protocol=HttpProtocol, before_start=None, after_start=None, before_stop=None,
backlog=100, stop_event=None, register_sys_signals=True, after_stop=None, ssl=None, sock=None, workers=1, loop=None,
run_async=False): protocol=HttpProtocol, backlog=100, stop_event=None,
register_sys_signals=True, run_async=False):
""" """
Helper function used by `run` and `create_server`. Helper function used by `run` and `create_server`.
""" """
@ -458,6 +472,16 @@ class Sanic:
"pull/335 has more information.", "pull/335 has more information.",
DeprecationWarning) DeprecationWarning)
# Deprecate this
if any(arg is not None for arg in (after_stop, after_start,
before_start, before_stop)):
if debug:
warnings.simplefilter('default')
warnings.warn("Passing a before_start, before_stop, after_start or"
"after_stop callback will be deprecated in version"
" 0.4.0",
DeprecationWarning)
self.error_handler.debug = debug self.error_handler.debug = debug
self.debug = debug self.debug = debug
self.loop = loop = get_event_loop() self.loop = loop = get_event_loop()
@ -482,13 +506,15 @@ class Sanic:
# Register start/stop events # Register start/stop events
# -------------------------------------------- # # -------------------------------------------- #
for event_name, settings_name, reverse in ( for event_name, settings_name, reverse, args in (
("before_server_start", "before_start", False), ("before_server_start", "before_start", False, before_start),
("after_server_start", "after_start", False), ("after_server_start", "after_start", False, after_start),
("before_server_stop", "before_stop", True), ("before_server_stop", "before_stop", True, before_stop),
("after_server_stop", "after_stop", True), ("after_server_stop", "after_stop", True, after_stop),
): ):
listeners = self.listeners[event_name].copy() listeners = self.listeners[event_name].copy()
if args:
listeners.extend(args)
if reverse: if reverse:
listeners.reverse() listeners.reverse()
# Prepend sanic to the arguments when listeners are triggered # Prepend sanic to the arguments when listeners are triggered