Skip to content

Commit

Permalink
import_entity_from_triplestore: retry
Browse files Browse the repository at this point in the history
  • Loading branch information
arcangelo7 committed Jul 26, 2024
1 parent 09cc519 commit 221e037
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions oc_ocdm/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
# SOFTWARE.
from __future__ import annotations

import time
import json
import os
from typing import TYPE_CHECKING
from zipfile import ZipFile

import rdflib
from rdflib import RDF, ConjunctiveGraph, Graph, URIRef
from SPARQLWrapper import JSON, SPARQLWrapper

Expand Down Expand Up @@ -224,16 +224,35 @@ def import_entities_from_graph(g_set: GraphSet, results: List[Dict], resp_agent:
@staticmethod
def import_entity_from_triplestore(g_set: GraphSet, ts_url: str, res: URIRef, resp_agent: str,
enable_validation: bool = False) -> GraphEntity:
sparql: SPARQLWrapper = SPARQLWrapper(ts_url)
query: str = f"SELECT ?s ?p ?o WHERE {{BIND (<{res}> AS ?s). ?s ?p ?o.}}"
sparql.setQuery(query)
sparql.setMethod('GET')
sparql.setReturnFormat(JSON)
result: ConjunctiveGraph = sparql.queryAndConvert()['results']['bindings']
if result:
imported_entities: List[GraphEntity] = Reader.import_entities_from_graph(g_set, result,
resp_agent, enable_validation)
if len(imported_entities) <= 0:
raise ValueError("The requested entity was not found or was not recognized as a proper OCDM entity.")
else:
return imported_entities[0]
attempt = 0
max_attempts = 3
wait_time = 5 # Initial wait time in seconds

while attempt < max_attempts:
try:
sparql: SPARQLWrapper = SPARQLWrapper(ts_url)
sparql.setQuery(query)
sparql.setMethod('GET')
sparql.setReturnFormat(JSON)
result = sparql.queryAndConvert()['results']['bindings']

if result:
imported_entities: List[GraphEntity] = Reader.import_entities_from_graph(g_set, result, resp_agent, enable_validation)
if len(imported_entities) <= 0:
raise ValueError("The requested entity was not found or was not recognized as a proper OCDM entity.")
else:
return imported_entities[0]

except Exception as e:
attempt += 1
if attempt < max_attempts:
print(f"[3] Attempt {attempt} failed. Could not import entity due to communication problems: {e}")
print(f"Retrying in {wait_time} seconds...")
time.sleep(wait_time)
wait_time *= 2 # Double the wait time for the next attempt
else:
print(f"[3] All {max_attempts} attempts failed. Could not import entity due to communication problems: {e}")
raise

raise Exception("Max attempts reached. Failed to import entity from triplestore.")

0 comments on commit 221e037

Please sign in to comment.