suppress erros while retrieving worker list

This commit is contained in:
Evgenii Alekseev 2023-12-30 17:00:24 +02:00
parent c58fd3a4b9
commit 783edfd3b2
4 changed files with 55 additions and 10 deletions

View File

@ -17,10 +17,12 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
import contextlib
import tempfile import tempfile
import uuid import uuid
from pathlib import Path from pathlib import Path
from functools import cached_property
from ahriman.core.configuration import Configuration from ahriman.core.configuration import Configuration
from ahriman.core.configuration.schema import ConfigurationSchema from ahriman.core.configuration.schema import ConfigurationSchema
@ -36,7 +38,6 @@ class DistributedSystem(Trigger, WebClient):
Attributes: Attributes:
identifier_path(Path): path to cached worker identifier identifier_path(Path): path to cached worker identifier
worker(Worker): unique self identifier
""" """
CONFIGURATION_SCHEMA: ConfigurationSchema = { CONFIGURATION_SCHEMA: ConfigurationSchema = {
@ -77,8 +78,17 @@ class DistributedSystem(Trigger, WebClient):
section, "identifier_path", fallback=Path(tempfile.gettempdir()) / "ahriman-worker-identifier") section, "identifier_path", fallback=Path(tempfile.gettempdir()) / "ahriman-worker-identifier")
self._owe_identifier = False self._owe_identifier = False
identifier = self.load_identifier(configuration, section) @cached_property
self.worker = Worker(configuration.get(section, "address"), identifier=identifier) def worker(self) -> Worker:
"""
load and set worker. Lazy property loaded because it is not always required
Returns:
Worker: unique self worker identifier
"""
section = next(iter(self.configuration_sections(self.configuration)))
identifier = self.load_identifier(self.configuration, section)
return Worker(self.configuration.get(section, "address"), identifier=identifier)
@classmethod @classmethod
def configuration_sections(cls, configuration: Configuration) -> list[str]: def configuration_sections(cls, configuration: Configuration) -> list[str]:
@ -161,6 +171,7 @@ class DistributedSystem(Trigger, WebClient):
Returns: Returns:
list[Worker]: currently registered workers list[Worker]: currently registered workers
""" """
with contextlib.suppress(Exception):
response = self.make_request("GET", self._workers_url()) response = self.make_request("GET", self._workers_url())
response_json = response.json() response_json = response.json()
@ -168,3 +179,5 @@ class DistributedSystem(Trigger, WebClient):
Worker(worker["address"], identifier=worker["identifier"]) Worker(worker["address"], identifier=worker["identifier"])
for worker in response_json for worker in response_json
] ]
return []

View File

@ -33,5 +33,8 @@ class WorkerLoaderTrigger(DistributedSystem):
return # there is manually set option return # there is manually set option
workers = [worker.address for worker in self.workers()] workers = [worker.address for worker in self.workers()]
if not workers:
return
self.logger.info("load workers %s", workers) self.logger.info("load workers %s", workers)
self.configuration.set_option("build", "workers", " ".join(workers)) self.configuration.set_option("build", "workers", " ".join(workers))

View File

@ -159,7 +159,7 @@ def test_unregister_force(distributed_system: DistributedSystem, mocker: MockerF
remove_mock.assert_called_once_with(missing_ok=True) remove_mock.assert_called_once_with(missing_ok=True)
def test_workers_get(distributed_system: DistributedSystem, mocker: MockerFixture) -> None: def test_workers(distributed_system: DistributedSystem, mocker: MockerFixture) -> None:
""" """
must return available remote workers must return available remote workers
""" """
@ -174,3 +174,19 @@ def test_workers_get(distributed_system: DistributedSystem, mocker: MockerFixtur
result = distributed_system.workers() result = distributed_system.workers()
requests_mock.assert_called_once_with("GET", distributed_system._workers_url()) requests_mock.assert_called_once_with("GET", distributed_system._workers_url())
assert result == [worker] assert result == [worker]
def test_workers_failed(distributed_system: DistributedSystem, mocker: MockerFixture) -> None:
"""
must suppress any exception happened during worker extraction
"""
mocker.patch("requests.Session.request", side_effect=Exception())
distributed_system.workers()
def test_workers_failed_http_error(distributed_system: DistributedSystem, mocker: MockerFixture) -> None:
"""
must suppress HTTP exception happened during worker extraction
"""
mocker.patch("requests.Session.request", side_effect=requests.HTTPError())
distributed_system.workers()

View File

@ -32,3 +32,16 @@ def test_on_start_skip(configuration: Configuration, mocker: MockerFixture) -> N
trigger = WorkerLoaderTrigger(repository_id, configuration) trigger = WorkerLoaderTrigger(repository_id, configuration)
trigger.on_start() trigger.on_start()
run_mock.assert_not_called() run_mock.assert_not_called()
def test_on_start_empty_list(configuration: Configuration, mocker: MockerFixture) -> None:
"""
must do not set anything if workers are not available
"""
configuration.set_option("status", "address", "http://localhost:8081")
mocker.patch("ahriman.core.distributed.WorkerLoaderTrigger.workers", return_value=[])
_, repository_id = configuration.check_loaded()
trigger = WorkerLoaderTrigger(repository_id, configuration)
trigger.on_start()
assert not configuration.has_option("build", "workers")