Update store data in one task to prevent races (#4519)

* Update store data in one task to prevent races

* Always return a dictionary
This commit is contained in:
Mike Degatano 2023-09-03 12:20:26 -04:00 committed by GitHub
parent d73962bd7d
commit 6adb4fbcf7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -102,25 +102,24 @@ class StoreData(CoreSysAttributes):
"""Initialize data holder.""" """Initialize data holder."""
self.coresys: CoreSys = coresys self.coresys: CoreSys = coresys
self.repositories: dict[str, Any] = {} self.repositories: dict[str, Any] = {}
self.addons: dict[str, Any] = {} self.addons: dict[str, dict[str, Any]] = {}
async def update(self) -> None: async def update(self) -> None:
"""Read data from add-on repository.""" """Read data from add-on repository."""
self.repositories.clear()
self.addons.clear()
# read core repository # read core repository
await self._read_addons_folder( addons = await self._read_addons_folder(
self.sys_config.path_addons_core, REPOSITORY_CORE self.sys_config.path_addons_core, REPOSITORY_CORE
) )
# read local repository # read local repository
await self._read_addons_folder( addons.update(
self.sys_config.path_addons_local, REPOSITORY_LOCAL await self._read_addons_folder(
self.sys_config.path_addons_local, REPOSITORY_LOCAL
)
) )
# add built-in repositories information # add built-in repositories information
await self._set_builtin_repositories() repositories = await self.sys_run_in_executor(self._get_builtin_repositories)
# read custom git repositories # read custom git repositories
def _read_git_repositories() -> list[ProcessedRepository]: def _read_git_repositories() -> list[ProcessedRepository]:
@ -132,8 +131,11 @@ class StoreData(CoreSysAttributes):
] ]
for repo in await self.sys_run_in_executor(_read_git_repositories): for repo in await self.sys_run_in_executor(_read_git_repositories):
self.repositories[repo.slug] = repo.config repositories[repo.slug] = repo.config
await self._read_addons_folder(repo.path, repo.slug) addons.update(await self._read_addons_folder(repo.path, repo.slug))
self.repositories = repositories
self.addons = addons
async def _find_addons(self, path: Path, repository: dict) -> list[Path] | None: async def _find_addons(self, path: Path, repository: dict) -> list[Path] | None:
"""Find add-ons in the path.""" """Find add-ons in the path."""
@ -169,10 +171,12 @@ class StoreData(CoreSysAttributes):
return None return None
return addon_list return addon_list
async def _read_addons_folder(self, path: Path, repository: str) -> None: async def _read_addons_folder(
self, path: Path, repository: str
) -> dict[str, dict[str, Any]]:
"""Read data from add-ons folder.""" """Read data from add-ons folder."""
if not (addon_list := await self._find_addons(path, repository)): if not (addon_list := await self._find_addons(path, repository)):
return return {}
def _process_addons_config() -> dict[str, dict[str, Any]]: def _process_addons_config() -> dict[str, dict[str, Any]]:
addons_config: dict[str, dict[str, Any]] = {} addons_config: dict[str, dict[str, Any]] = {}
@ -205,23 +209,16 @@ class StoreData(CoreSysAttributes):
return addons_config return addons_config
self.addons.update(await self.sys_run_in_executor(_process_addons_config)) return await self.sys_run_in_executor(_process_addons_config)
async def _set_builtin_repositories(self): def _get_builtin_repositories(self) -> dict[str, dict[str, str]]:
"""Add local built-in repository into dataset.""" """Get local built-in repositories into dataset.
def _get_builtins() -> dict[str, dict[str, str]] | None: Need to run inside executor.
try: """
builtin_file = Path(__file__).parent.joinpath("built-in.json") try:
return read_json_file(builtin_file) builtin_file = Path(__file__).parent.joinpath("built-in.json")
except ConfigurationFileError: return read_json_file(builtin_file)
_LOGGER.warning("Can't read built-in json") except ConfigurationFileError:
return None _LOGGER.warning("Can't read built-in json")
return {}
builtin_data = await self.sys_run_in_executor(_get_builtins)
if builtin_data:
# core repository
self.repositories[REPOSITORY_CORE] = builtin_data[REPOSITORY_CORE]
# local repository
self.repositories[REPOSITORY_LOCAL] = builtin_data[REPOSITORY_LOCAL]