From 547cc723db1b5783bda3201c7870faea5c023d90 Mon Sep 17 00:00:00 2001 From: Johannes Richter Date: Fri, 13 Oct 2023 20:36:59 +0200 Subject: [PATCH] added option to let the pool stay offline one an koi api exception was raised; --- koi_core/__init__.py | 4 ++-- koi_core/api/__init__.py | 4 ++-- koi_core/api/common.py | 5 +++-- koi_core/worker.py | 8 +++++++- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/koi_core/__init__.py b/koi_core/__init__.py index 829632c..82bf109 100644 --- a/koi_core/__init__.py +++ b/koi_core/__init__.py @@ -53,11 +53,11 @@ def deinit(): def create_api_object_pool( - host: str, username: str, password: str, persistance_file: Union[IOBase, str] = None + host: str, username: str, password: str, persistance_file: Union[IOBase, str] = None, stay_offline=False ): if persistance_file: setCachingPersistence(CachingPersistence(persistance_file)) - api = API(host, username, password) + api = API(host, username, password, stay_offline) return APIObjectPool(api) diff --git a/koi_core/api/__init__.py b/koi_core/api/__init__.py index 4d8e97f..45d0824 100644 --- a/koi_core/api/__init__.py +++ b/koi_core/api/__init__.py @@ -27,8 +27,8 @@ def is_koi_reachable(host: str) -> bool: class API(RequestsAPI): - def __init__(self, base_url: str, username: str, password: str): - super().__init__(base_url, username, password) + def __init__(self, base_url: str, username: str, password: str, stay_offline=False): + super().__init__(base_url, username, password, stay_offline) self.models = APIModels(self) self.instances = APIInstances(self) diff --git a/koi_core/api/common.py b/koi_core/api/common.py index a880109..61bb22e 100644 --- a/koi_core/api/common.py +++ b/koi_core/api/common.py @@ -84,7 +84,7 @@ def authenticate_locked(baseAPI: "BaseAPI", force=False): def common_authenticated(self: "BaseAPI", request_func, *args, **kwargs): - if not self.online: + if not self.online and self.stay_offline: raise KoiApiOfflineException() authenticate_locked(self) @@ -254,13 +254,14 @@ def _build_path( class RequestsAPI(BaseAPI): - def __init__(self, base_url: str, username: str, password: str): + def __init__(self, base_url: str, username: str, password: str, stay_offline=False): self._lock = Lock() self._base_url = base_url self._user = username self._password = password self._session = requests.Session() self.online = True + self.stay_offline = stay_offline def reconnect(self): self.online = True diff --git a/koi_core/worker.py b/koi_core/worker.py index 75f2f28..50f2989 100644 --- a/koi_core/worker.py +++ b/koi_core/worker.py @@ -123,6 +123,12 @@ def main(): default=10, help="Number of seconds a instance has to be untouched before atempting to train it", ) + p.add( + "--stay-offline", + action="store_true", + help="Do not try to connect to the server. This will only work if the cache is already filled", + default=False, + ) opt = p.parse_args() @@ -148,7 +154,7 @@ def main(): # connect using the credentials logging.info("connecting to %s", opt.server) - pool = koi.create_api_object_pool(opt.server, opt.user, opt.password) + pool = koi.create_api_object_pool(opt.server, opt.user, opt.password, opt.stay_offline) while 1: try: