Skip to content

Commit

Permalink
adding retry
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbie1977 committed Sep 18, 2024
1 parent f02e044 commit 75da60a
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions src/uk/ac/ebi/vfb/neo4j/neo4j_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,48 @@ def __init__(self, base_uri, usr, pwd):
if not self.test_connection():
raise Exception("Failed to connect to Neo4j.")

def commit_list(self, statements, return_graphs = False):
"""Commit a list of statements to neo4J DB via REST API.
def commit_list(self, statements, return_graphs=False, max_retries=3):
"""Commit a list of statements to Neo4j DB via REST API.
Prints requests status and warnings if any problems with commit.
- statements = list of cypher statements as strings
- return_graphs, optionally specify graphs to be returned in JSON results.
Errors prompt warnings, not exceptions, and cause return = FALSE.
- statements: list of Cypher statements as strings
- return_graphs: optionally specify graphs to be returned in JSON results.
Errors prompt warnings, not exceptions, and cause return = False.
Returns results list of results or False if any errors are encountered."""

cstatements = []
if return_graphs:
for s in statements:
cstatements.append({'statement': s, "resultDataContents" : [ "row", "graph" ]})
else:
cstatements.append({'statement': s, "resultDataContents": ["row", "graph"]})
else:
for s in statements:
cstatements.append({'statement': s}) # rows an columns are returned by default.
cstatements.append({'statement': s}) # Rows and columns are returned by default.
payload = {'statements': cstatements}
headers = self.headers
response = requests.post(url = self.base_uri + self.commit, auth = (self.usr, self.pwd) ,
data = json.dumps(payload), headers=headers)
if self.rest_return_check(response):
return response.json()['results']
else:
return False

attempt = 0
while attempt < max_retries:
try:
response = requests.post(
url=self.base_uri + self.commit,
auth=(self.usr, self.pwd),
data=json.dumps(payload),
headers=headers
)
if self.rest_return_check(response):
return response.json()['results']
else:
return False
except requests.exceptions.ConnectionError as e:
attempt += 1
print(f"ConnectionError on attempt {attempt}: {e}")
if attempt >= max_retries:
print("Max retries reached. Operation failed.")
return False
wait_time = 2 ** attempt # Exponential backoff: 2, 4, 8 seconds
print(f"Retrying in {wait_time} seconds...")
time.sleep(wait_time)
# If all retries fail, return False
return False


def commit_list_in_chunks(self, statements, verbose=False, chunk_length=1000):
Expand Down

0 comments on commit 75da60a

Please sign in to comment.