Skip to content

Commit ca680f3

Browse files
authored
Merge pull request #107 from BioPack-team/fix-timeout-overrun
Properly cancel Tier 0 execution at timeout (fix #74)
2 parents 4b99418 + fa04c9a commit ca680f3

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

src/retriever/data_tiers/tier_0/base_query.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
Infores,
1313
KnowledgeGraphDict,
1414
QueryGraphDict,
15-
ResultDict,
1615
)
1716
from retriever.utils.logs import TRAPILogger
1817
from retriever.utils.trapi import (
@@ -43,21 +42,12 @@ async def execute(self) -> LookupArtifacts:
4342
start_time = time.time()
4443
self.job_log.info("Starting lookup against Tier 0...")
4544

46-
try:
47-
timeout = None if self.ctx.timeout[0] < 0 else self.ctx.timeout[0]
48-
self.job_log.debug(
49-
f"Tier 0 timeout is {'disabled' if timeout is None else f'{timeout}s'}."
50-
)
51-
async with asyncio.timeout(timeout):
52-
backend_results = await self.get_results(self.qgraph)
53-
54-
except TimeoutError:
55-
self.job_log.error("Tier 0 operation timed out.")
56-
backend_results = BackendResult(
57-
results=list[ResultDict](),
58-
knowledge_graph=KnowledgeGraphDict(nodes={}, edges={}),
59-
auxiliary_graphs=dict[AuxGraphID, AuxiliaryGraphDict](),
60-
)
45+
timeout = None if self.ctx.timeout[0] < 0 else self.ctx.timeout[0]
46+
self.job_log.debug(
47+
f"Tier 0 timeout is {'disabled' if timeout is None else f'{timeout}s'}."
48+
)
49+
async with asyncio.timeout(timeout):
50+
backend_results = await self.get_results(self.qgraph)
6151

6252
with tracer.start_as_current_span("update_kg"):
6353
normalize_kgraph(
@@ -90,10 +80,13 @@ async def execute(self) -> LookupArtifacts:
9080
self.aux_graphs,
9181
self.job_log.get_logs(),
9282
)
93-
except Exception:
94-
self.job_log.exception(
95-
"Unhandled exception occurred while processing Tier 0. See logs for details."
96-
)
83+
except Exception as e:
84+
if isinstance(e, TimeoutError):
85+
self.job_log.error("Tier 0 operation timed out.")
86+
elif not isinstance(e, asyncio.exceptions.CancelledError):
87+
self.job_log.exception(
88+
"Unhandled exception occurred while processing Tier 0 query. See logs for details."
89+
)
9790
return LookupArtifacts(
9891
[], self.kgraph, self.aux_graphs, self.job_log.get_logs(), error=True
9992
)

src/retriever/utils/biolink.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def reverse_qualifier_constraints(
8686
"""Reverse a given list of qualifier constraints."""
8787
new = list[QualifierConstraintDict]()
8888
for constraint in qualifier_constraints:
89-
new_qualifier_set = set[QualifierDict]()
89+
new_qualifier_set = list[QualifierDict]()
9090
for qualifier in constraint["qualifier_set"]:
9191
new_qualifier = QualifierDict(**qualifier)
9292
if "object" in qualifier["qualifier_type_id"]:
@@ -105,7 +105,7 @@ def reverse_qualifier_constraints(
105105
# BUG: Technically invalid if we can't reverse the predicate
106106
# but this is vanishingly rare and not worth addressing right now
107107
new_qualifier["qualifier_value"] = inverse
108-
new_qualifier_set.add(new_qualifier)
108+
new_qualifier_set.append(new_qualifier)
109109
new.append(constraint)
110110
return new
111111

src/retriever/utils/logs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ def trapi_level_to_int(level: TRAPILogLevel) -> int:
4242
def format_trapi_log(
4343
level: LogLevel,
4444
message: str,
45-
timestamp: datetime | None = None,
45+
timestamp: str | datetime | None = None,
4646
trace: str | None = None,
4747
) -> LogEntryDict:
4848
"""Format a loguru message into a TRAPI-spec LogEntry."""
4949
if timestamp is None:
5050
timestamp = datetime.now().astimezone()
51+
if isinstance(timestamp, str):
52+
timestamp = datetime.fromisoformat(timestamp)
5153
log_entry = LogEntryDict(
5254
level=log_level_to_trapi(level),
5355
message=message,

0 commit comments

Comments
 (0)