mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-21 08:06:30 +00:00
Remove I/O in event loop for Home Assistant Core backup (#5648)
* Remove I/O in event loop for Home Assistant Core backup The Home Assistant Core backup still contains some I/O in the event loop. Move all I/O into the executor. * Update supervisor/homeassistant/module.py Co-authored-by: Mike Degatano <michael.degatano@gmail.com> --------- Co-authored-by: Mike Degatano <michael.degatano@gmail.com>
This commit is contained in:
parent
34939cfe52
commit
c8cc6fe003
@ -393,63 +393,59 @@ class HomeAssistant(FileConfiguration, CoreSysAttributes):
|
|||||||
async def backup(
|
async def backup(
|
||||||
self, tar_file: tarfile.TarFile, exclude_database: bool = False
|
self, tar_file: tarfile.TarFile, exclude_database: bool = False
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Backup Home Assistant Core config/ directory."""
|
"""Backup Home Assistant Core config/directory."""
|
||||||
await self.begin_backup()
|
excludes = HOMEASSISTANT_BACKUP_EXCLUDE.copy()
|
||||||
try:
|
if exclude_database:
|
||||||
|
excludes += HOMEASSISTANT_BACKUP_EXCLUDE_DATABASE
|
||||||
|
|
||||||
|
def _is_excluded_by_filter(path: PurePath) -> bool:
|
||||||
|
"""Filter function to filter out excluded files from the backup."""
|
||||||
|
for exclude in excludes:
|
||||||
|
if not path.full_match(f"data/{exclude}"):
|
||||||
|
continue
|
||||||
|
_LOGGER.debug("Ignoring %s because of %s", path, exclude)
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Backup data config folder
|
||||||
|
def _write_tarfile(metadata: dict[str, Any]) -> None:
|
||||||
|
"""Write tarfile."""
|
||||||
with TemporaryDirectory(dir=self.sys_config.path_tmp) as temp:
|
with TemporaryDirectory(dir=self.sys_config.path_tmp) as temp:
|
||||||
temp_path = Path(temp)
|
temp_path = Path(temp)
|
||||||
|
|
||||||
# Store local configs/state
|
# Store local configs/state
|
||||||
try:
|
try:
|
||||||
write_json_file(
|
write_json_file(temp_path.joinpath("homeassistant.json"), metadata)
|
||||||
temp_path.joinpath("homeassistant.json"), self._data
|
|
||||||
)
|
|
||||||
except ConfigurationFileError as err:
|
except ConfigurationFileError as err:
|
||||||
raise HomeAssistantError(
|
raise HomeAssistantError(
|
||||||
f"Can't save meta for Home Assistant Core: {err!s}",
|
f"Can't save meta for Home Assistant Core: {err!s}",
|
||||||
_LOGGER.error,
|
_LOGGER.error,
|
||||||
) from err
|
) from err
|
||||||
|
|
||||||
# Backup data config folder
|
try:
|
||||||
def _write_tarfile():
|
|
||||||
with tar_file as backup:
|
with tar_file as backup:
|
||||||
# Backup metadata
|
# Backup metadata
|
||||||
backup.add(temp, arcname=".")
|
backup.add(temp, arcname=".")
|
||||||
|
|
||||||
# Set excludes
|
|
||||||
excludes = HOMEASSISTANT_BACKUP_EXCLUDE.copy()
|
|
||||||
if exclude_database:
|
|
||||||
excludes += HOMEASSISTANT_BACKUP_EXCLUDE_DATABASE
|
|
||||||
|
|
||||||
def is_excluded_by_filter(path: PurePath) -> bool:
|
|
||||||
"""Filter to filter excludes."""
|
|
||||||
for exclude in excludes:
|
|
||||||
if not path.full_match(f"data/{exclude}"):
|
|
||||||
continue
|
|
||||||
_LOGGER.debug(
|
|
||||||
"Ignoring %s because of %s", path, exclude
|
|
||||||
)
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
# Backup data
|
# Backup data
|
||||||
atomic_contents_add(
|
atomic_contents_add(
|
||||||
backup,
|
backup,
|
||||||
self.sys_config.path_homeassistant,
|
self.sys_config.path_homeassistant,
|
||||||
file_filter=is_excluded_by_filter,
|
file_filter=_is_excluded_by_filter,
|
||||||
arcname="data",
|
arcname="data",
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
|
||||||
_LOGGER.info("Backing up Home Assistant Core config folder")
|
|
||||||
await self.sys_run_in_executor(_write_tarfile)
|
|
||||||
_LOGGER.info("Backup Home Assistant Core config folder done")
|
|
||||||
except (tarfile.TarError, OSError) as err:
|
except (tarfile.TarError, OSError) as err:
|
||||||
raise HomeAssistantBackupError(
|
raise HomeAssistantBackupError(
|
||||||
f"Can't backup Home Assistant Core config folder: {str(err)}",
|
f"Can't backup Home Assistant Core config folder: {str(err)}",
|
||||||
_LOGGER.error,
|
_LOGGER.error,
|
||||||
) from err
|
) from err
|
||||||
|
|
||||||
|
await self.begin_backup()
|
||||||
|
try:
|
||||||
|
_LOGGER.info("Backing up Home Assistant Core config folder")
|
||||||
|
await self.sys_run_in_executor(_write_tarfile, self._data)
|
||||||
|
_LOGGER.info("Backup Home Assistant Core config folder done")
|
||||||
finally:
|
finally:
|
||||||
await self.end_backup()
|
await self.end_backup()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user