Skip to content

Commit

Permalink
Define web.Application.on_startup() handler
Browse files Browse the repository at this point in the history
Changes:

    - Define `web.Application.on_startup()` signal handler.
    - Run `app.on_startup()` along with the request handler within an event
loop in `web.run_app()` and in `worker.GunicornWebWorker()`.

Problems:
    - Tests in `test_run_app.py` don't pass because loop is mocked
with `wrap` parameter and `loop.create_server()` returns mock.
    - Tests in `test_worker.py` don't pass on the `master` branch
and complain on `pytest.skip()`.
  • Loading branch information
f0t0n committed Aug 21, 2016
1 parent e8c94a5 commit 531bc44
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 187 deletions.
21 changes: 18 additions & 3 deletions aiohttp/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def __init__(self, *, logger=web_logger, loop=None,
self._on_pre_signal = PreSignal()
self._on_post_signal = PostSignal()
self._on_response_prepare = Signal(self)
self._on_startup = Signal(self)
self._on_shutdown = Signal(self)
self._on_cleanup = Signal(self)

Expand All @@ -190,6 +191,10 @@ def on_pre_signal(self):
def on_post_signal(self):
return self._on_post_signal

@property
def on_startup(self):
return self._on_startup

@property
def on_shutdown(self):
return self._on_shutdown
Expand All @@ -214,6 +219,14 @@ def make_handler(self, **kwargs):
return self._handler_factory(
self, self.router, loop=self.loop, **kwargs)

@asyncio.coroutine
def startup(self):
"""Causes on_startup signal
Should be called in the event loop along with the request handler.
"""
yield from self.on_startup.send(self)

@asyncio.coroutine
def shutdown(self):
"""Causes on_shutdown signal
Expand Down Expand Up @@ -267,9 +280,11 @@ def run_app(app, *, host='0.0.0.0', port=None,
loop = app.loop

handler = app.make_handler()
srv = loop.run_until_complete(loop.create_server(handler, host, port,
ssl=ssl_context,
backlog=backlog))
server = loop.create_server(handler, host, port, ssl=ssl_context,
backlog=backlog)
srv, startup_res = loop.run_until_complete(asyncio.gather(server,
app.startup(),
loop=loop))

scheme = 'https' if ssl_context else 'http'
print("======== Running on {scheme}://{host}:{port}/ ========\n"
Expand Down
4 changes: 3 additions & 1 deletion aiohttp/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def run(self):
self._runner = ensure_future(self._run(), loop=self.loop)

try:
self.loop.run_until_complete(self._runner)
self.loop.run_until_complete(asyncio.gather(self._runner,
self.wsgi.startup(),
loop=self.loop))
finally:
self.loop.close()

Expand Down
183 changes: 0 additions & 183 deletions tests/test_worker.py

This file was deleted.

0 comments on commit 531bc44

Please sign in to comment.