1
0
mirror of https://github.com/GNS3/gns3-server synced 2025-01-12 17:10:55 +00:00

Try to have a better server close cleanup

Fix #669
This commit is contained in:
Julien Duponchelle 2016-09-08 11:23:13 +02:00
parent 0910712657
commit 9bbb32400d
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8

View File

@ -57,6 +57,8 @@ class WebServer:
self._port = port self._port = port
self._loop = None self._loop = None
self._handler = None self._handler = None
self._server = None
self._app = None
self._start_time = time.time() self._start_time = time.time()
self._port_manager = PortManager(host) self._port_manager = PortManager(host)
self._running = False self._running = False
@ -76,15 +78,13 @@ class WebServer:
WebServer._instance = WebServer(host, port) WebServer._instance = WebServer(host, port)
return WebServer._instance return WebServer._instance
@asyncio.coroutine
def _run_application(self, handler, ssl_context=None): def _run_application(self, handler, ssl_context=None):
try: try:
server = yield from self._loop.create_server(handler, self._host, self._port, ssl=ssl_context) self._server = self._loop.run_until_complete(self._loop.create_server(handler, self._host, self._port, ssl=ssl_context))
except OSError as e: except OSError as e:
log.critical("Could not start the server: {}".format(e)) log.critical("Could not start the server: {}".format(e))
return False return False
return server return True
@asyncio.coroutine @asyncio.coroutine
def shutdown_server(self): def shutdown_server(self):
@ -98,9 +98,15 @@ class WebServer:
log.warning("Close is already in progress") log.warning("Close is already in progress")
return return
if self._server:
self._server.close()
yield from self._server.wait_closed()
if self._app:
yield from self._app.shutdown()
if self._handler: if self._handler:
yield from self._handler.finish_connections() yield from self._handler.finish_connections(2) # Parameter is timeout
self._handler = None if self._app:
yield from self._app.cleanup()
yield from Controller.instance().stop() yield from Controller.instance().stop()
@ -279,9 +285,9 @@ class WebServer:
for key, val in os.environ.items(): for key, val in os.environ.items():
log.debug("ENV %s=%s", key, val) log.debug("ENV %s=%s", key, val)
app = aiohttp.web.Application() self._app = aiohttp.web.Application()
# Allow CORS for this domains # Allow CORS for this domains
cors = aiohttp_cors.setup(app, defaults={ cors = aiohttp_cors.setup(self._app, defaults={
# Default web server for web gui dev # Default web server for web gui dev
"http://127.0.0.1:8080": aiohttp_cors.ResourceOptions(expose_headers="*", allow_headers="*"), "http://127.0.0.1:8080": aiohttp_cors.ResourceOptions(expose_headers="*", allow_headers="*"),
"http://localhost:8080": aiohttp_cors.ResourceOptions(expose_headers="*", allow_headers="*"), "http://localhost:8080": aiohttp_cors.ResourceOptions(expose_headers="*", allow_headers="*"),
@ -289,16 +295,15 @@ class WebServer:
}) })
for method, route, handler in Route.get_routes(): for method, route, handler in Route.get_routes():
log.debug("Adding route: {} {}".format(method, route)) log.debug("Adding route: {} {}".format(method, route))
cors.add(app.router.add_route(method, route, handler)) cors.add(self._app.router.add_route(method, route, handler))
for module in MODULES: for module in MODULES:
log.debug("Loading module {}".format(module.__name__)) log.debug("Loading module {}".format(module.__name__))
m = module.instance() m = module.instance()
m.port_manager = self._port_manager m.port_manager = self._port_manager
log.info("Starting server on {}:{}".format(self._host, self._port)) log.info("Starting server on {}:{}".format(self._host, self._port))
self._handler = app.make_handler(handler=RequestHandler) self._handler = self._app.make_handler(handler=RequestHandler)
server = self._run_application(self._handler, ssl_context) if self._run_application(self._handler, ssl_context) is False:
if self._loop.run_until_complete(server) is False:
self._loop.stop() self._loop.stop()
return return
@ -323,9 +328,5 @@ class WebServer:
# TypeError: async() takes 1 positional argument but 3 were given # TypeError: async() takes 1 positional argument but 3 were given
log.warning("TypeError exception in the loop {}".format(e)) log.warning("TypeError exception in the loop {}".format(e))
finally: finally:
if self._handler and self._loop.is_running():
self._loop.run_until_complete(self._handler.finish_connections())
server.close()
if self._loop.is_running(): if self._loop.is_running():
self._loop.run_until_complete(app.finish()) self._loop.run_until_complete(self.shutdown_server())