From d3e4bb7219aef1e7adb9f56f06b2fb207e427496 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Fri, 9 Apr 2021 13:33:28 +0200 Subject: [PATCH] Improve message handling of job condition (#2791) --- supervisor/exceptions.py | 4 +++ supervisor/jobs/decorator.py | 58 +++++++++++++++--------------------- 2 files changed, 28 insertions(+), 34 deletions(-) diff --git a/supervisor/exceptions.py b/supervisor/exceptions.py index 21758ee1d..6505bd3d7 100644 --- a/supervisor/exceptions.py +++ b/supervisor/exceptions.py @@ -34,6 +34,10 @@ class JobException(HassioError): """Base job exception.""" +class JobConditionException(JobException): + """Exception happening for job conditions.""" + + # HomeAssistant diff --git a/supervisor/jobs/decorator.py b/supervisor/jobs/decorator.py index 3a493019d..4eda23938 100644 --- a/supervisor/jobs/decorator.py +++ b/supervisor/jobs/decorator.py @@ -9,7 +9,7 @@ import sentry_sdk from ..const import CoreState from ..coresys import CoreSysAttributes -from ..exceptions import HassioError, JobException +from ..exceptions import HassioError, JobConditionException, JobException from ..resolution.const import MINIMUM_FREE_SPACE_THRESHOLD, ContextType, IssueType from .const import JobCondition, JobExecutionLimit @@ -75,10 +75,16 @@ class Job(CoreSysAttributes): job = self.sys_jobs.get_job(self.name) # Handle condition - if self.conditions and not self._check_conditions(): - if self.on_condition is None: - return - raise self.on_condition() + if self.conditions: + try: + self._check_conditions() + except JobConditionException as err: + error_msg = str(err) + if self.on_condition is None: + _LOGGER.info(error_msg) + return + _LOGGER.warning(error_msg) + raise self.on_condition(error_msg) from None # Handle exection limits if self.limit in (JobExecutionLimit.SINGLE_WAIT, JobExecutionLimit.ONCE): @@ -124,45 +130,35 @@ class Job(CoreSysAttributes): ) if JobCondition.HEALTHY in used_conditions and not self.sys_core.healthy: - _LOGGER.warning( - "'%s' blocked from execution, system is not healthy", - self._method.__qualname__, + raise JobConditionException( + f"'{self._method.__qualname__}' blocked from execution, system is not healthy" ) - return False if ( JobCondition.RUNNING in used_conditions and self.sys_core.state != CoreState.RUNNING ): - _LOGGER.warning( - "'%s' blocked from execution, system is not running - %s", - self._method.__qualname__, - self.sys_core.state, + raise JobConditionException( + f"'{self._method.__qualname__}' blocked from execution, system is not running - {self.sys_core.state!s}" ) - return False if ( JobCondition.FREE_SPACE in used_conditions and self.sys_host.info.free_space < MINIMUM_FREE_SPACE_THRESHOLD ): - _LOGGER.warning( - "'%s' blocked from execution, not enough free space (%sGB) left on the device", - self._method.__qualname__, - self.sys_host.info.free_space, - ) self.sys_resolution.create_issue(IssueType.FREE_SPACE, ContextType.SYSTEM) - return False + raise JobConditionException( + f"'{self._method.__qualname__}' blocked from execution, not enough free space ({self.sys_host.info.free_space}GB) left on the device" + ) if ( JobCondition.INTERNET_SYSTEM in self.conditions and not self.sys_supervisor.connectivity and self.sys_core.state in (CoreState.SETUP, CoreState.RUNNING) ): - _LOGGER.warning( - "'%s' blocked from execution, no supervisor internet connection", - self._method.__qualname__, + raise JobConditionException( + f"'{self._method.__qualname__}' blocked from execution, no supervisor internet connection" ) - return False if ( JobCondition.INTERNET_HOST in self.conditions @@ -170,20 +166,14 @@ class Job(CoreSysAttributes): and not self.sys_host.network.connectivity and self.sys_core.state in (CoreState.SETUP, CoreState.RUNNING) ): - _LOGGER.warning( - "'%s' blocked from execution, no host internet connection", - self._method.__qualname__, + raise JobConditionException( + f"'{self._method.__qualname__}' blocked from execution, no host internet connection" ) - return False if JobCondition.HAOS in self.conditions and not self.sys_hassos.available: - _LOGGER.warning( - "'%s' blocked from execution, no Home Assistant OS available", - self._method.__qualname__, + raise JobConditionException( + f"'{self._method.__qualname__}' blocked from execution, no Home Assistant OS available" ) - return False - - return True async def _acquire_exection_limit(self) -> None: """Process exection limits."""