Timeout shutdown (#603)

* Don't wait too long for shutdown

* Update log message

* Fix timeout

* Fast shudown
This commit is contained in:
Pascal Vizeli 2018-07-25 01:46:54 +02:00 committed by GitHub
parent 7f878bfac0
commit 9dea93142b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 9 deletions

View File

@ -235,13 +235,16 @@ class RestAPI(CoreSysAttributes):
async def start(self):
"""Run rest api webserver."""
await self._runner.setup()
self._site = web.TCPSite(self._runner, "0.0.0.0", 80)
self._site = web.TCPSite(
self._runner, host="0.0.0.0", port=80, shutdown_timeout=5)
try:
await self._site.start()
except OSError as err:
_LOGGER.fatal(
"Failed to create HTTP server at 0.0.0.0:80 -> %s", err)
else:
_LOGGER.info("Start API on %s", self.sys_docker.network.supervisor)
async def stop(self):
"""Stop rest api webserver."""
@ -251,3 +254,5 @@ class RestAPI(CoreSysAttributes):
# Shutdown running API
await self._site.stop()
await self._runner.cleanup()
_LOGGER.info("Stop API on %s", self.sys_docker.network.supervisor)

View File

@ -3,6 +3,8 @@ from contextlib import suppress
import asyncio
import logging
import async_timeout
from .coresys import CoreSysAttributes
from .const import (
STARTUP_SYSTEM, STARTUP_SERVICES, STARTUP_APPLICATION, STARTUP_INITIALIZE)
@ -65,7 +67,6 @@ class HassIO(CoreSysAttributes):
# start api
await self.sys_api.start()
_LOGGER.info("Start API on %s", self.sys_docker.network.supervisor)
# start addon mark as initialize
await self.sys_addons.boot(STARTUP_INITIALIZE)
@ -113,12 +114,18 @@ class HassIO(CoreSysAttributes):
self.sys_scheduler.suspend = True
# process async stop tasks
await asyncio.wait([
self.sys_api.stop(),
self.sys_dns.stop(),
self.sys_websession.close(),
self.sys_websession_ssl.close()
])
try:
with async_timeout.timeout(10):
await asyncio.wait([
self.sys_api.stop(),
self.sys_dns.stop(),
self.sys_websession.close(),
self.sys_websession_ssl.close()
])
except asyncio.TimeoutError:
_LOGGER.warning("Force Shutdown!")
_LOGGER.info("Hass.io is down")
async def shutdown(self):
"""Shutdown all running containers in correct order."""

View File

@ -3,6 +3,8 @@ import asyncio
import logging
import shlex
import async_timeout
_LOGGER = logging.getLogger(__name__)
COMMAND = "socat UDP-RECVFROM:53,fork UDP-SENDTO:127.0.0.11:53"
@ -38,5 +40,10 @@ class DNSForward:
return
self.proc.kill()
await self.proc.wait()
try:
with async_timeout.timeout(5):
await self.proc.wait()
except asyncio.TimeoutError:
_LOGGER.warning("Stop waiting for DNS shutdown")
_LOGGER.info("Stop DNS forwarding")