From 1d066329293eb097bb831231da14e2b5c7734d04 Mon Sep 17 00:00:00 2001 From: jctanner Date: Mon, 3 Jun 2024 08:02:15 -0400 Subject: [PATCH] Make the sleep times come from constants.py and settable via env vars. (#114) * Make the sleep times come from constants.py and settable via env vars. No-Issue Signed-off-by: James Tanner * Allow fractional seconds. No-Issue Signed-off-by: James Tanner --------- Signed-off-by: James Tanner --- galaxykit/client.py | 4 ++-- galaxykit/collections.py | 4 ++-- galaxykit/constants.py | 7 +++++++ galaxykit/tasks.py | 16 ++++++++++++++-- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/galaxykit/client.py b/galaxykit/client.py index 4ee9ff3..b9902ab 100644 --- a/galaxykit/client.py +++ b/galaxykit/client.py @@ -23,7 +23,7 @@ from . import collections from . import roles from . import __version__ as VERSION -from .constants import RBAC_VERSION, EE_ENDPOINTS_CHANGE_VERSION +from .constants import RBAC_VERSION, EE_ENDPOINTS_CHANGE_VERSION, SLEEP_SECONDS_ONETIME logger = logging.getLogger(__name__) @@ -336,7 +336,7 @@ def _retry_if_expired_gw_token(self, method, url, headers, *args, **kwargs): if self.response.status_code < 400: return self.response logger.debug(f"Reloading token failed: {self.response.text}") - time.sleep(5) + time.sleep(SLEEP_SECONDS_ONETIME) self.response.raise_for_status() def _payload(self, method, path, body, *args, **kwargs): diff --git a/galaxykit/collections.py b/galaxykit/collections.py index 245a5d4..787e3cb 100644 --- a/galaxykit/collections.py +++ b/galaxykit/collections.py @@ -11,7 +11,7 @@ from orionutils.generator import build_collection from .utils import wait_for_task, logger, GalaxyClientError, wait_for_url -from .constants import EE_ENDPOINTS_CHANGE_VERSION +from .constants import EE_ENDPOINTS_CHANGE_VERSION, SLEEP_SECONDS_POLLING def collection_info(client, repository, namespace, collection_name, version): @@ -276,7 +276,7 @@ def move_or_copy_collection( client.get(dest_url) ready = True except GalaxyClientError: - sleep(1) + sleep(SLEEP_SECONDS_POLLING) timeout = timeout - 1 if timeout < 0: raise diff --git a/galaxykit/constants.py b/galaxykit/constants.py index bf4f9e1..4b8493d 100644 --- a/galaxykit/constants.py +++ b/galaxykit/constants.py @@ -1,2 +1,9 @@ +import os + + RBAC_VERSION = "4.6.0dev" EE_ENDPOINTS_CHANGE_VERSION = "4.7.0dev" + +SLEEP_SECONDS_POLLING = float(os.environ.get('GALAXYKIT_SLEEP_SECONDS_POLLING', 10)) +SLEEP_SECONDS_ONETIME = float(os.environ.get('GALAXYKIT_SLEEP_SECONDS_ONETIME', 10)) +POLLING_MAX_ATTEMPTS = int(os.environ.get('GALAXYKIT_POLLING_MAX_ATTEMPTS', 10)) diff --git a/galaxykit/tasks.py b/galaxykit/tasks.py index 65232fb..e3cdd3f 100644 --- a/galaxykit/tasks.py +++ b/galaxykit/tasks.py @@ -1,4 +1,6 @@ from time import sleep +from .constants import SLEEP_SECONDS_POLLING +from .constants import POLLING_MAX_ATTEMPTS def get_tasks(client, only_running=False): @@ -14,7 +16,12 @@ def get_task(client, task_id): return client.get(tasks_url) -def wait_task(client, task_id, sleep_seconds=10, max_attempts=10): +def wait_task(client, task_id, sleep_seconds=None, max_attempts=None): + if sleep_seconds is None: + sleep_seconds = SLEEP_SECONDS_POLLING + if max_attempts is None: + max_attempts = POLLING_MAX_ATTEMPTS + task = get_task(client, task_id) while task["state"] not in ["completed", "failed", "canceled"]: @@ -32,7 +39,12 @@ def wait_task(client, task_id, sleep_seconds=10, max_attempts=10): return task -def wait_all(client, sleep_seconds=10, max_attempts=10): +def wait_all(client, sleep_seconds=None, max_attempts=10): + if sleep_seconds is None: + sleep_seconds = SLEEP_SECONDS_POLLING + if max_attempts is None: + max_attempts = POLLING_MAX_ATTEMPTS + tasks = get_tasks(client, only_running=True) while tasks and tasks["results"]: