mirror of
				https://github.com/arcan1s/ahriman.git
				synced 2025-11-03 23:33:41 +00:00 
			
		
		
		
	suppress erros while retrieving worker list
This commit is contained in:
		@ -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,10 +171,13 @@ class DistributedSystem(Trigger, WebClient):
 | 
				
			|||||||
        Returns:
 | 
					        Returns:
 | 
				
			||||||
            list[Worker]: currently registered workers
 | 
					            list[Worker]: currently registered workers
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        response = self.make_request("GET", self._workers_url())
 | 
					        with contextlib.suppress(Exception):
 | 
				
			||||||
        response_json = response.json()
 | 
					            response = self.make_request("GET", self._workers_url())
 | 
				
			||||||
 | 
					            response_json = response.json()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return [
 | 
					            return [
 | 
				
			||||||
            Worker(worker["address"], identifier=worker["identifier"])
 | 
					                Worker(worker["address"], identifier=worker["identifier"])
 | 
				
			||||||
            for worker in response_json
 | 
					                for worker in response_json
 | 
				
			||||||
        ]
 | 
					            ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return []
 | 
				
			||||||
 | 
				
			|||||||
@ -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))
 | 
				
			||||||
 | 
				
			|||||||
@ -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()
 | 
				
			||||||
 | 
				
			|||||||
@ -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")
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user