Skip to content

Commit

Permalink
release 0.5.2: adapted to launart 0.3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
GreyElaina committed Jun 6, 2022
1 parent 99660cd commit d48d308
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 20 deletions.
18 changes: 9 additions & 9 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# PEP 621 project metadata
# See https://www.python.org/dev/peps/pep-0621/
name = "graia-amnesia"
version = "0.5.1"
version = "0.5.2"
authors = [
{name = "GreyElaina", email = "[email protected]"},
]
Expand All @@ -11,7 +11,7 @@ license-expression = "MIT"
dependencies = [
"loguru<1.0.0,>=0.6.0",
"yarl<2.0.0,>=1.7.2",
"launart>=0.3.1",
"launart>=0.3.3",
"statv>=0.2.2",
]
readme = "README.md"
Expand Down
8 changes: 4 additions & 4 deletions src/graia/amnesia/builtins/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,14 @@ def get_interface(self, interface_type):

@property
def stages(self):
return {"prepare", "cleanup"}
return {"preparing", "cleanup"}

@property
def required(self):
return set()

async def launch(self, mgr: Launart):
async with self.stage("prepare"):
async with self.stage("preparing"):
if not self.session:
self.session = ClientSession(timeout=ClientTimeout(total=None))
async with self.stage("cleanup"):
Expand Down Expand Up @@ -550,14 +550,14 @@ def get_interface(self, interface_type):

@property
def stages(self):
return {"prepare", "blocking", "cleanup"}
return {"preparing", "blocking", "cleanup"}

@property
def required(self):
return set()

async def launch(self, manager: Launart):
async with self.stage("prepare"):
async with self.stage("preparing"):
logger.info(f"starting server on {self.host}:{self.port}")
runner = web.AppRunner(self.wsgi_handler)
await runner.setup()
Expand Down
33 changes: 33 additions & 0 deletions src/graia/amnesia/builtins/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import TYPE_CHECKING

from launart import ExportInterface
from launart.service import Service

if TYPE_CHECKING:
from typing import Any, Awaitable, Callable, MutableMapping, Type
Expand All @@ -17,3 +18,35 @@ def get_asgi_handler(
Callable[[MutableMapping[str, Any]], Awaitable[None]]
], None]""":
...


class ExternalASGIHandlerService(Service, ASGIHandlerProvider):
supported_interface_types = {ASGIHandlerProvider}

asgi: """Callable[[
Type[MutableMapping[str, Any]],
Awaitable[MutableMapping[str, Any]],
Callable[[MutableMapping[str, Any]], Awaitable[None]]
], None]"""

def __init__(
self,
asgi_handler: """Callable[[
Type[MutableMapping[str, Any]],
Awaitable[MutableMapping[str, Any]],
Callable[[MutableMapping[str, Any]], Awaitable[None]]
], None]""",
) -> None:
self.asgi = asgi_handler

def get_interface(self, _):
return self

def get_asgi_handler(
self,
) -> """Callable[[
Type[MutableMapping[str, Any]],
Awaitable[MutableMapping[str, Any]],
Callable[[MutableMapping[str, Any]], Awaitable[None]]
], None]""":
return self.asgi
6 changes: 3 additions & 3 deletions src/graia/amnesia/builtins/uvicorn.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ def required(self):

@property
def stages(self):
return {"prepare", "cleanup"}
return {"preparing", "cleanup"}

async def launch(self, mgr: Launart):
async with self.stage("prepare"):
async with self.stage("preparing"):
asgi_handler = mgr.get_interface(ASGIHandlerProvider).get_asgi_handler()
self.server = WithoutSigHandlerServer(Config(asgi_handler, host=self.host, port=self.port))
# TODO: 使用户拥有更多的对 Config 的配置能力.
Expand All @@ -69,8 +69,8 @@ async def launch(self, mgr: Launart):
target = logging.getLogger(name)
target.handlers = [LoguruHandler(level=level)]
target.propagate = False
async with self.stage("cleanup"):
serve_task = asyncio.create_task(self.server.serve())
async with self.stage("cleanup"):
logger.warning("try to shutdown uvicorn server...")
self.server.should_exit = True
await wait_fut([serve_task, asyncio.sleep(10)], return_when=asyncio.FIRST_COMPLETED)
Expand Down
4 changes: 2 additions & 2 deletions src/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
mgr.add_service(AiohttpService())
mgr.add_service(StarletteService())
mgr.add_service(UvicornService(port=21447))
install()
# install()

cbr = TransportRegistrar()

Expand Down Expand Up @@ -153,7 +153,7 @@ async def launch(self, mgr: Launart):
ai = mgr.get_interface(AiohttpClientInterface)
rider = ai.websocket("http://localhost:21447/ws_test")
await asyncio.wait(
[rider.use(TestWsClient()), mgr.status.wait_for_completed()], return_when=asyncio.FIRST_COMPLETED
[rider.use(TestWsClient()), mgr.status.wait_for_sigexit()], return_when=asyncio.FIRST_COMPLETED
)


Expand Down

0 comments on commit d48d308

Please sign in to comment.