Skip to content

Commit

Permalink
fix: connection check failing (#1280)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkstrp authored Sep 11, 2024
1 parent 6f39dc7 commit 4524aca
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions rules/common.smk
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,26 @@ def input_custom_extra_functionality(w):
return []


# Check if the workflow has access to the internet by trying to access the HEAD of specified url
def has_internet_access(url="www.zenodo.org") -> bool:
import http.client as http_client
def has_internet_access(url: str = "https://www.zenodo.org", timeout: int = 3) -> bool:
"""
Checks if internet connection is available by sending a HEAD request
to a reliable server like Google.
Parameters:
- url (str): The URL to check for internet connection. Default is Google.
- timeout (int | float): The maximum time (in seconds) the request should wait.
# based on answer and comments from
# https://stackoverflow.com/a/29854274/11318472
conn = http_client.HTTPConnection(url, timeout=5) # need access to zenodo anyway
Returns:
- bool: True if the internet is available, otherwise False.
"""
try:
conn.request("HEAD", "/")
return True
except:
# Send a HEAD request to avoid fetching full response
response = requests.head(url, timeout=timeout, allow_redirects=True)
return response.status_code == 200
except requests.ConnectionError: # (e.g., no internet, DNS issues)
return False
except requests.Timeout: # (e.g., slow or no network)
return False
finally:
conn.close()


def solved_previous_horizon(w):
Expand Down

0 comments on commit 4524aca

Please sign in to comment.