git_deploy: improve refresh error handling

Save repo state if an exception is raised during a refresh
attempt.   Add additional repo warnings with for better
descriptions regarding repo issues.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2024-02-23 18:09:44 -05:00
parent aadff0d54e
commit 119f579a44
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
1 changed files with 25 additions and 13 deletions

View File

@ -48,27 +48,32 @@ class GitDeploy(AppDeploy):
async def initialize(self) -> Dict[str, Any]: async def initialize(self) -> Dict[str, Any]:
storage = await super().initialize() storage = await super().initialize()
await self.repo.restore_state(storage) await self.repo.restore_state(storage)
self._is_valid = self.repo.is_valid() self._is_valid = storage["is_valid"]
if not self.needs_refresh(): if not self.needs_refresh():
self.repo.log_repo_info() self.repo.log_repo_info()
return storage return storage
async def refresh(self) -> None: async def refresh(self) -> None:
try: await self._update_repo_state(raise_exc=False)
await self._update_repo_state()
except Exception:
logging.exception("Error Refreshing git state")
async def _update_repo_state(self, need_fetch: bool = True) -> None: async def _update_repo_state(
self, need_fetch: bool = True, raise_exc: bool = True
) -> None:
self._is_valid = False self._is_valid = False
await self.repo.refresh_repo_state(need_fetch=need_fetch) try:
self.log_info(f"Channel: {self.channel}") await self.repo.refresh_repo_state(need_fetch=need_fetch)
self._is_valid = self.repo.is_valid() except Exception as e:
if not self._is_valid: if raise_exc or isinstance(e, asyncio.CancelledError):
self.log_info("Repo validation check failed, updates disabled") raise
else: else:
self.log_info("Validity check for git repo passed") self._is_valid = self.repo.is_valid()
self._save_state() finally:
self.log_info(f"Channel: {self.channel}")
if not self._is_valid:
self.log_info("Repo validation check failed, updates disabled")
else:
self.log_info("Validity check for git repo passed")
self._save_state()
async def update(self) -> bool: async def update(self) -> bool:
await self.repo.wait_for_init() await self.repo.wait_for_init()
@ -371,6 +376,7 @@ class GitRepo:
self._check_warnings() self._check_warnings()
except Exception: except Exception:
logging.exception(f"Git Repo {self.alias}: Initialization failure") logging.exception(f"Git Repo {self.alias}: Initialization failure")
self._check_warnings()
raise raise
else: else:
self.initialized = True self.initialized = True
@ -671,6 +677,12 @@ class GitRepo:
self.repo_anomalies.clear() self.repo_anomalies.clear()
if self.repo_corrupt: if self.repo_corrupt:
self.repo_warnings.append("Repo is corrupt") self.repo_warnings.append("Repo is corrupt")
if self.git_branch == "?":
self.repo_warnings.append("Failed to detect git branch")
elif self.git_remote == "?":
self.repo_warnings.append(
f"Failed to detect tracking remote for branch {self.git_branch}"
)
if self.upstream_url == "?": if self.upstream_url == "?":
self.repo_warnings.append("Failed to detect repo url") self.repo_warnings.append("Failed to detect repo url")
return return