Add placement of before_server_start and after_server_stop

This commit is contained in:
Adam Hopkins 2019-06-04 12:59:15 +03:00
parent 3685b4de85
commit daf42c5f43
2 changed files with 18 additions and 24 deletions

View File

@ -55,7 +55,7 @@ async def handler_stream(request):
break break
body = body.decode("utf-8").replace("1", "A") body = body.decode("utf-8").replace("1", "A")
# await response.write(body) # await response.write(body)
return stream(streaming) return response.stream(body)
@app.listener("before_server_start") @app.listener("before_server_start")

View File

@ -1,15 +1,13 @@
import asyncio import asyncio
import warnings import warnings
from http.cookies import SimpleCookie from http.cookies import SimpleCookie
from inspect import isawaitable from inspect import isawaitable
from typing import Any, Awaitable, Callable, MutableMapping, Union from typing import Any, Awaitable, Callable, MutableMapping, Union
from urllib.parse import quote
from multidict import CIMultiDict from multidict import CIMultiDict
from urllib.parse import quote
from sanic.exceptions import InvalidUsage, ServerError
from sanic.log import logger from sanic.log import logger
from sanic.exceptions import InvalidUsage, ServerError
from sanic.request import Request from sanic.request import Request
from sanic.response import HTTPResponse, StreamingHTTPResponse from sanic.response import HTTPResponse, StreamingHTTPResponse
from sanic.server import StreamBuffer from sanic.server import StreamBuffer
@ -107,20 +105,18 @@ class Lifespan:
if "before_server_start" in self.asgi_app.sanic_app.listeners: if "before_server_start" in self.asgi_app.sanic_app.listeners:
warnings.warn( warnings.warn(
'You have set a listener for "before_server_start" in ASGI mode. ' 'You have set a listener for "before_server_start" in ASGI mode. '
"It will be executed as early as possible, but not before " 'It will be executed as early as possible, but not before '
"the ASGI server is started." 'the ASGI server is started.'
) )
if "after_server_stop" in self.asgi_app.sanic_app.listeners: if "after_server_stop" in self.asgi_app.sanic_app.listeners:
warnings.warn( warnings.warn(
'You have set a listener for "after_server_stop" in ASGI mode. ' 'You have set a listener for "after_server_stop" in ASGI mode. '
"It will be executed as late as possible, but not before " 'It will be executed as late as possible, but not after '
"the ASGI server is stopped." 'the ASGI server is stopped.'
) )
async def pre_startup(self) -> None: async def pre_startup(self) -> None:
for handler in self.asgi_app.sanic_app.listeners[ for handler in self.asgi_app.sanic_app.listeners["before_server_start"]:
"before_server_start"
]:
response = handler( response = handler(
self.asgi_app.sanic_app, self.asgi_app.sanic_app.loop self.asgi_app.sanic_app, self.asgi_app.sanic_app.loop
) )
@ -128,6 +124,13 @@ class Lifespan:
await response await response
async def startup(self) -> None: async def startup(self) -> None:
for handler in self.asgi_app.sanic_app.listeners["before_server_start"]:
response = handler(
self.asgi_app.sanic_app, self.asgi_app.sanic_app.loop
)
if isawaitable(response):
await response
for handler in self.asgi_app.sanic_app.listeners["after_server_start"]: for handler in self.asgi_app.sanic_app.listeners["after_server_start"]:
response = handler( response = handler(
self.asgi_app.sanic_app, self.asgi_app.sanic_app.loop self.asgi_app.sanic_app, self.asgi_app.sanic_app.loop
@ -143,10 +146,7 @@ class Lifespan:
if isawaitable(response): if isawaitable(response):
await response await response
async def post_shutdown(self) -> None: for handler in self.asgi_app.sanic_app.listeners["after_server_stop"]:
for handler in self.asgi_app.sanic_app.listeners[
"before_server_start"
]:
response = handler( response = handler(
self.asgi_app.sanic_app, self.asgi_app.sanic_app.loop self.asgi_app.sanic_app, self.asgi_app.sanic_app.loop
) )
@ -191,7 +191,6 @@ class ASGIApp:
True if headers.get("expect") == "100-continue" else False True if headers.get("expect") == "100-continue" else False
) )
instance.lifespan = Lifespan(instance) instance.lifespan = Lifespan(instance)
await instance.pre_startup()
if scope["type"] == "lifespan": if scope["type"] == "lifespan":
await instance.lifespan(scope, receive, send) await instance.lifespan(scope, receive, send)
@ -291,9 +290,7 @@ class ASGIApp:
type(response), type(response),
) )
exception = ServerError("Invalid response type") exception = ServerError("Invalid response type")
response = self.sanic_app.error_handler.response( response = self.sanic_app.error_handler.response(self.request, exception)
self.request, exception
)
headers = [ headers = [
(str(name).encode("latin-1"), str(value).encode("latin-1")) (str(name).encode("latin-1"), str(value).encode("latin-1"))
for name, value in response.headers.items() for name, value in response.headers.items()
@ -310,10 +307,7 @@ class ASGIApp:
if response.cookies: if response.cookies:
cookies = SimpleCookie() cookies = SimpleCookie()
cookies.load(response.cookies) cookies.load(response.cookies)
headers += [ headers += [(b"set-cookie", cookie.encode("utf-8")) for name, cookie in response.cookies.items()]
(b"set-cookie", cookie.encode("utf-8"))
for name, cookie in response.cookies.items()
]
await self.transport.send( await self.transport.send(
{ {