Follow the correct shutdown flow

This commit is contained in:
Pascal Vizeli 2018-04-23 21:45:06 +02:00
parent 05980d4147
commit 23d1013cfa
3 changed files with 34 additions and 14 deletions

View File

@ -5,7 +5,7 @@ import logging
from .addon import Addon from .addon import Addon
from .repository import Repository from .repository import Repository
from .data import AddonsData from .data import AddonsData
from ..const import REPOSITORY_CORE, REPOSITORY_LOCAL, BOOT_AUTO from ..const import REPOSITORY_CORE, REPOSITORY_LOCAL, BOOT_AUTO, STATE_STARTED
from ..coresys import CoreSysAttributes from ..coresys import CoreSysAttributes
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -131,7 +131,7 @@ class AddonManager(CoreSysAttributes):
for addon_slug in del_addons: for addon_slug in del_addons:
self.addons_obj.pop(addon_slug) self.addons_obj.pop(addon_slug)
async def auto_boot(self, stage): async def boot(self, stage):
"""Boot addons with mode auto.""" """Boot addons with mode auto."""
tasks = [] tasks = []
for addon in self.addons_obj.values(): for addon in self.addons_obj.values():
@ -143,3 +143,16 @@ class AddonManager(CoreSysAttributes):
if tasks: if tasks:
await asyncio.wait(tasks) await asyncio.wait(tasks)
await asyncio.sleep(self.sys_config.wait_boot) await asyncio.sleep(self.sys_config.wait_boot)
async def shutdown(self, stage):
"""Shutdown addons."""
tasks = []
for addon in self.addons_obj.values():
if addon.is_installed and \
await addon.state() == STATE_STARTED and \
addon.startup == stage:
tasks.append(addon.stop())
_LOGGER.info("Shutdown %s stop %d addons", stage, len(tasks))
if tasks:
await asyncio.wait(tasks)

View File

@ -1,10 +1,12 @@
"""Main file for HassIO.""" """Main file for HassIO."""
from contextlib import suppress
import asyncio import asyncio
import logging import logging
from .coresys import CoreSysAttributes from .coresys import CoreSysAttributes
from .const import ( from .const import (
STARTUP_SYSTEM, STARTUP_SERVICES, STARTUP_APPLICATION, STARTUP_INITIALIZE) STARTUP_SYSTEM, STARTUP_SERVICES, STARTUP_APPLICATION, STARTUP_INITIALIZE)
from .exceptions import HassioError
from .utils.dt import fetch_timezone from .utils.dt import fetch_timezone
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -68,7 +70,7 @@ class HassIO(CoreSysAttributes):
_LOGGER.info("Start API on %s", self.sys_docker.network.supervisor) _LOGGER.info("Start API on %s", self.sys_docker.network.supervisor)
# start addon mark as initialize # start addon mark as initialize
await self.sys_addons.auto_boot(STARTUP_INITIALIZE) await self.sys_addons.boot(STARTUP_INITIALIZE)
try: try:
# HomeAssistant is already running / supervisor have only reboot # HomeAssistant is already running / supervisor have only reboot
@ -80,17 +82,17 @@ class HassIO(CoreSysAttributes):
self.sys_services.reset() self.sys_services.reset()
# start addon mark as system # start addon mark as system
await self.sys_addons.auto_boot(STARTUP_SYSTEM) await self.sys_addons.boot(STARTUP_SYSTEM)
# start addon mark as services # start addon mark as services
await self.sys_addons.auto_boot(STARTUP_SERVICES) await self.sys_addons.boot(STARTUP_SERVICES)
# run HomeAssistant # run HomeAssistant
if self.sys_homeassistant.boot: if self.sys_homeassistant.boot:
await self.sys_homeassistant.start() await self.sys_homeassistant.start()
# start addon mark as application # start addon mark as application
await self.sys_addons.auto_boot(STARTUP_APPLICATION) await self.sys_addons.boot(STARTUP_APPLICATION)
# store new last boot # store new last boot
self.sys_config.last_boot = self.sys_hardware.last_boot self.sys_config.last_boot = self.sys_hardware.last_boot
@ -118,3 +120,15 @@ class HassIO(CoreSysAttributes):
self.sys_websession.close(), self.sys_websession.close(),
self.sys_websession_ssl.close() self.sys_websession_ssl.close()
]) ])
async def shutdown(self):
"""Shutdown all running containers in correct order."""
await self.sys_addons.shutdown(STARTUP_APPLICATION)
# Close Home Assistant
with suppress(HassioError):
await self.sys_homeassistant.stop()
await self.sys_addons.shutdown(STARTUP_SERVICES)
await self.sys_addons.shutdown(STARTUP_SYSTEM)
await self.sys_addons.shutdown(STARTUP_INITIALIZE)

View File

@ -222,14 +222,7 @@ class SnapshotManager(CoreSysAttributes):
tasks = [] tasks = []
# Stop Home-Assistant / Add-ons # Stop Home-Assistant / Add-ons
tasks.append(self.sys_homeassistant.stop()) await self.sys_core.shutdown()
for addon in self.sys_addons.list_addons:
if addon.is_installed:
tasks.append(addon.stop())
if tasks:
_LOGGER.info("Restore %s stop tasks", snapshot.slug)
await asyncio.wait(tasks)
# Restore folders # Restore folders
_LOGGER.info("Restore %s run folders", snapshot.slug) _LOGGER.info("Restore %s run folders", snapshot.slug)