Skip to content

Commit

Permalink
added option to let the pool stay offline one an koi api exception wa…
Browse files Browse the repository at this point in the history
…s raised;
  • Loading branch information
hannesrichter committed Oct 13, 2023
1 parent 9beece9 commit 547cc72
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
4 changes: 2 additions & 2 deletions koi_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
4 changes: 2 additions & 2 deletions koi_core/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions koi_core/api/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion koi_core/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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:
Expand Down

0 comments on commit 547cc72

Please sign in to comment.