JobManager should not touch our exceptions (#2261)

* JobManager should not touch our exceptions

* Add tests

* temporary allow su update all time

* Update supervisor/jobs/decorator.py

Co-authored-by: Joakim Sørensen <joasoe@gmail.com>

Co-authored-by: Joakim Sørensen <joasoe@gmail.com>
This commit is contained in:
Pascal Vizeli 2020-11-17 18:46:02 +01:00 committed by GitHub
parent 476f021fbf
commit 4b500ef873
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 6 deletions

View File

@ -3,6 +3,8 @@ from enum import Enum
import logging import logging
from typing import List, Optional from typing import List, Optional
import sentry_sdk
from ..const import CoreState from ..const import CoreState
from ..coresys import CoreSys from ..coresys import CoreSys
from ..exceptions import HassioError, JobException from ..exceptions import HassioError, JobException
@ -57,16 +59,17 @@ class Job:
return False return False
try: try:
result = await self._method(*args, **kwargs) return await self._method(*args, **kwargs)
except HassioError as err: except HassioError as err:
_LOGGER.error(err) raise err
except Exception as err:
_LOGGER.exception("Unhandled exception: %s", err)
sentry_sdk.capture_exception(err)
raise JobException() from err raise JobException() from err
finally: finally:
if self.cleanup: if self.cleanup:
self._coresys.jobs.remove_job(job) self._coresys.jobs.remove_job(job)
return result
return wrapper return wrapper
def _check_conditions(self): def _check_conditions(self):

View File

@ -144,7 +144,7 @@ class Tasks(CoreSysAttributes):
except AddonsError: except AddonsError:
_LOGGER.error("Can't auto update Add-on %s", addon.slug) _LOGGER.error("Can't auto update Add-on %s", addon.slug)
@Job(conditions=[JobCondition.HEALTHY, JobCondition.FREE_SPACE]) @Job(conditions=[JobCondition.FREE_SPACE])
async def _update_supervisor(self): async def _update_supervisor(self):
"""Check and run update of Supervisor Supervisor.""" """Check and run update of Supervisor Supervisor."""
if not self.sys_supervisor.need_update: if not self.sys_supervisor.need_update:

View File

@ -159,7 +159,7 @@ class Updater(JsonConfig, CoreSysAttributes):
self._data[ATTR_CHANNEL] = value self._data[ATTR_CHANNEL] = value
@AsyncThrottle(timedelta(seconds=30)) @AsyncThrottle(timedelta(seconds=30))
@Job(conditions=[JobCondition.HEALTHY]) @Job(conditions=[JobCondition.INTERNET])
async def fetch_data(self): async def fetch_data(self):
"""Fetch current versions from Github. """Fetch current versions from Github.

View File

@ -2,8 +2,11 @@
# pylint: disable=protected-access,import-error # pylint: disable=protected-access,import-error
from unittest.mock import patch from unittest.mock import patch
import pytest
from supervisor.const import CoreState from supervisor.const import CoreState
from supervisor.coresys import CoreSys from supervisor.coresys import CoreSys
from supervisor.exceptions import HassioError, JobException
from supervisor.jobs.decorator import Job, JobCondition from supervisor.jobs.decorator import Job, JobCondition
from supervisor.resolution.const import UnhealthyReason from supervisor.resolution.const import UnhealthyReason
@ -127,3 +130,45 @@ async def test_internet_connectivity_with_core_state(coresys: CoreSys):
coresys.core.state = CoreState.STOPPING coresys.core.state = CoreState.STOPPING
assert await test.execute() assert await test.execute()
async def test_exception(coresys: CoreSys):
"""Test the healty decorator."""
class TestClass:
"""Test class."""
def __init__(self, coresys: CoreSys):
"""Initialize the test class."""
self.coresys = coresys
@Job(conditions=[JobCondition.HEALTHY])
async def execute(self):
"""Execute the class method."""
raise HassioError()
test = TestClass(coresys)
with pytest.raises(HassioError):
assert await test.execute()
async def test_exception_not_handle(coresys: CoreSys):
"""Test the healty decorator."""
class TestClass:
"""Test class."""
def __init__(self, coresys: CoreSys):
"""Initialize the test class."""
self.coresys = coresys
@Job(conditions=[JobCondition.HEALTHY])
async def execute(self):
"""Execute the class method."""
raise Exception()
test = TestClass(coresys)
with pytest.raises(JobException):
assert await test.execute()