diff --git a/lockable/lockable.py b/lockable/lockable.py index 9b41075..4b4f6f7 100644 --- a/lockable/lockable.py +++ b/lockable/lockable.py @@ -48,7 +48,8 @@ def __init__(self, hostname=socket.gethostname(), self._provider = create_provider(resource_list_file or resource_list) @property - def _resource_list(self): + def resource_list(self) -> list: + """ Return current resources list""" return self._provider.data @staticmethod @@ -134,7 +135,7 @@ def _lock_some(self, requirements, candidates, timeout_s, retry_interval): def _lock(self, requirements, timeout_s, retry_interval=1) -> Allocation: """ Lock resource """ - local_resources = filter_(self._resource_list, requirements) + local_resources = filter_(self.resource_list, requirements) random.shuffle(local_resources) ResourceNotFound.invariant(local_resources, "Suitable resource not available") return self._lock_some(requirements, local_resources, timeout_s, retry_interval) @@ -152,7 +153,7 @@ def lock(self, requirements: (str or dict), timeout_s: int = 1000) -> Allocation :param timeout_s: timeout while trying to lock :return: Allocation context """ - assert isinstance(self._resource_list, list), 'resources list is not loaded' + assert isinstance(self.resource_list, list), 'resources list is not loaded' requirements = self.parse_requirements(requirements) predicate = self._get_requirements(requirements, self._hostname) # Refresh resources data @@ -160,7 +161,7 @@ def lock(self, requirements: (str or dict), timeout_s: int = 1000) -> Allocation begin = datetime.now() MODULE_LOGGER.debug("Use lock folder: %s", self._lock_folder) MODULE_LOGGER.debug("Requirements: %s", json.dumps(predicate)) - MODULE_LOGGER.debug("Resource list: %s", json.dumps(self._resource_list)) + MODULE_LOGGER.debug("Resource list: %s", json.dumps(self.resource_list)) allocation = self._lock(predicate, timeout_s) self._allocations[allocation.resource_id] = allocation allocation.allocation_queue_time = datetime.now() - begin diff --git a/tests/test_Lockable.py b/tests/test_Lockable.py index 94748c9..1fec4a5 100644 --- a/tests/test_Lockable.py +++ b/tests/test_Lockable.py @@ -33,6 +33,14 @@ def test_constructor(self): lockable = Lockable(hostname='myhost', resource_list_file=list_file, lock_folder=tmpdirname) self.assertFalse(lockable._provider._resource_list_file_mtime is None) + def test_resource_list(self): + with TemporaryDirectory() as tmpdirname: + list_file = os.path.join(tmpdirname, 'test.json') + with open(list_file, 'w') as fp: + fp.write('[{"id": "123"}]') + lockable = Lockable(hostname='myhost', resource_list_file=list_file, lock_folder=tmpdirname) + self.assertEqual(lockable.resource_list, [{"id": "123"}]) + def test_reload_resource_list_file(self): with TemporaryDirectory() as tmpdirname: list_file = os.path.join(tmpdirname, 'test.json')