Skip to content

Commit

Permalink
Merge pull request #2296 from mashehu/improve-perf
Browse files Browse the repository at this point in the history
Avoid blocking requests when checking for new version
  • Loading branch information
mashehu authored May 26, 2023
2 parents 5e897bd + 397ddec commit 9d809b6
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions nf_core/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Common utility functions for the nf-core python package.
"""
import concurrent.futures
import datetime
import errno
import hashlib
Expand Down Expand Up @@ -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
Expand All @@ -72,12 +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 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)
# check if we have a newer version without blocking the rest of the script
is_outdated = False
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
return (is_outdated, current_version, remote_version)


Expand Down

0 comments on commit 9d809b6

Please sign in to comment.