From 2ee34a213fb0f7787fea90effbbabd3687dc1391 Mon Sep 17 00:00:00 2001 From: mashehu Date: Fri, 26 May 2023 11:28:51 +0200 Subject: [PATCH 1/3] avoid a blocking request call on first cli call --- nf_core/utils.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/nf_core/utils.py b/nf_core/utils.py index 36c39db50a..4e85d4eba5 100644 --- a/nf_core/utils.py +++ b/nf_core/utils.py @@ -1,6 +1,7 @@ """ Common utility functions for the nf-core python package. """ +import concurrent.futures import datetime import errno import hashlib @@ -58,6 +59,12 @@ NFCORE_DIR = os.path.join(os.environ.get("XDG_CONFIG_HOME", os.path.join(os.getenv("HOME"), ".config")), "nfcore") +def fetch_remote_version(source_url): + response = requests.get(source_url, timeout=3) + remote_version = re.sub(r"[^0-9\.]", "", response.text) + return remote_version + + def check_if_outdated(current_version=None, remote_version=None, source_url="https://nf-co.re/tools_version"): """ Check if the current version of nf-core is outdated @@ -72,12 +79,16 @@ def check_if_outdated(current_version=None, remote_version=None, source_url="htt # Build the URL to check against source_url = os.environ.get("NFCORE_VERSION_URL", source_url) source_url = f"{source_url}?v={current_version}" - # Fetch and clean up the remote version - if remote_version is None: - response = requests.get(source_url, timeout=3) - remote_version = re.sub(r"[^0-9\.]", "", response.text) - # Check if we have an available update - is_outdated = Version(remote_version) > Version(current_version) + + # Fetch the remote version asynchronously + with concurrent.futures.ThreadPoolExecutor() as executor: + future = executor.submit(fetch_remote_version, source_url) + # Retrieve the remote version in the background + remote_version = future.result() + is_outdated = False + if remote_version is not None: + # Check if we have an available update + is_outdated = Version(remote_version) > Version(current_version) return (is_outdated, current_version, remote_version) From 766b87a08062219d656a2fb4fa9e1e649a538483 Mon Sep 17 00:00:00 2001 From: mashehu Date: Fri, 26 May 2023 11:48:01 +0200 Subject: [PATCH 2/3] improved version of the async call --- nf_core/utils.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/nf_core/utils.py b/nf_core/utils.py index 7774265bd0..28a6e3f54a 100644 --- a/nf_core/utils.py +++ b/nf_core/utils.py @@ -79,16 +79,18 @@ def check_if_outdated(current_version=None, remote_version=None, source_url="htt # Build the URL to check against source_url = os.environ.get("NFCORE_VERSION_URL", source_url) source_url = f"{source_url}?v={current_version}" - - # Fetch the remote version asynchronously - with concurrent.futures.ThreadPoolExecutor() as executor: - future = executor.submit(fetch_remote_version, source_url) - # Retrieve the remote version in the background - remote_version = future.result() + # check if we have a newer version without blocking the rest of the script is_outdated = False + remote_version = None + try: + with concurrent.futures.ThreadPoolExecutor() as executor: + future = executor.submit(fetch_remote_version, source_url) + remote_version = future.result() + except Exception as e: + log.debug(f"Could not check for nf-core updates: {e}") if remote_version is not None: - # Check if we have an available update - is_outdated = Version(remote_version) > Version(current_version) + if Version(remote_version) > Version(current_version): + is_outdated = True return (is_outdated, current_version, remote_version) From 397ddec0440cbb9a6f029d771bae6b7b76ae668d Mon Sep 17 00:00:00 2001 From: mashehu Date: Fri, 26 May 2023 12:16:18 +0200 Subject: [PATCH 3/3] add manual overwrite option for tests --- nf_core/utils.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/nf_core/utils.py b/nf_core/utils.py index 28a6e3f54a..3cd09397e3 100644 --- a/nf_core/utils.py +++ b/nf_core/utils.py @@ -81,13 +81,13 @@ def check_if_outdated(current_version=None, remote_version=None, source_url="htt source_url = f"{source_url}?v={current_version}" # check if we have a newer version without blocking the rest of the script is_outdated = False - remote_version = None - try: - with concurrent.futures.ThreadPoolExecutor() as executor: - future = executor.submit(fetch_remote_version, source_url) - remote_version = future.result() - except Exception as e: - log.debug(f"Could not check for nf-core updates: {e}") + if remote_version is None: # we set it manually for tests + try: + with concurrent.futures.ThreadPoolExecutor() as executor: + future = executor.submit(fetch_remote_version, source_url) + remote_version = future.result() + except Exception as e: + log.debug(f"Could not check for nf-core updates: {e}") if remote_version is not None: if Version(remote_version) > Version(current_version): is_outdated = True