Skip to content

Commit

Permalink
Retry health check by hitting different API endpoint if /health retur…
Browse files Browse the repository at this point in the history
…ns 501, retry git commands if the initial call fails
  • Loading branch information
pharr117 committed Sep 4, 2023
1 parent baa90b7 commit 3d18f05
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ app.log
chain-registry.zip
cosmos-chain-registry*
__pycache__/*
chain-registry/
17 changes: 16 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def get_healthy_rest_endpoints(rest_endpoints):
def is_endpoint_healthy(endpoint):
try:
response = requests.get(f"{endpoint}/health", timeout=1, verify=False)
# some chains dont implement the /health endpoint. Should we just skip /health and go directly to the below?
if response.status_code == 501:
response = requests.get(f"{endpoint}/cosmos/gov/v1beta1/proposals?proposal_status=2", timeout=1, verify=False)
return response.status_code == 200
except:
return False
Expand Down Expand Up @@ -277,13 +280,22 @@ def fetch_data_for_network(network, network_type, repo_path):

rest_endpoints = data.get("apis", {}).get("rest", [])
rpc_endpoints = data.get("apis", {}).get("rpc", [])
print(f"Found {len(rest_endpoints)} rest endpoints and {len(rpc_endpoints)} rpc endpoints for {network}")

# Prioritize RPC endpoints for fetching the latest block height
latest_block_height = -1
healthy_rpc_endpoints = get_healthy_rpc_endpoints(rpc_endpoints)
healthy_rest_endpoints = get_healthy_rest_endpoints(rest_endpoints)

if len(healthy_rpc_endpoints) == 0:
print(f"No healthy RPC endpoints found for network {network}. Skipping...")
return None

if len(healthy_rest_endpoints) == 0:
print(f"No healthy REST endpoints found for network {network}. Skipping...")
return None

print(f"Found {len(healthy_rest_endpoints)} rest endpoints and {len(healthy_rpc_endpoints)} rpc endpoints for {network}")

# Shuffle the healthy endpoints
shuffle(healthy_rpc_endpoints)
shuffle(healthy_rest_endpoints)
Expand Down Expand Up @@ -381,6 +393,9 @@ def update_data():
print(f"Repo path: {repo_path}")
except Exception as e:
print(f"Error downloading and extracting repo: {e}")
print("Error encountered. Sleeping for 1 minute before retrying...")
sleep(60)
continue

try:
# Process mainnets & testnets
Expand Down

0 comments on commit 3d18f05

Please sign in to comment.