Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more robust retry to the pubmed client's send_request #1426

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions indra/literature/pubmed_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Search and get metadata for articles in Pubmed.
"""
import logging
import random

import requests
from time import sleep
from typing import List
Expand All @@ -19,7 +21,7 @@

# Send request can't be cached by lru_cache because it takes a dict
# (a mutable/unhashable type) as an argument. We cache the callers instead.
def send_request(url, data):
def send_request(url, data, retry_pause=0.5, max_tries=3):
try:
res = requests.get(url, params=data)
except requests.exceptions.Timeout as e:
Expand All @@ -32,9 +34,12 @@ def send_request(url, data):
logger.error('url: %s, data: %s' % (url, data))
logger.error(e)
return None
if res.status_code == 429:
sleep(0.5)
res = requests.get(url, params=data)
if res.status_code in {429, 502, 503} and max_tries > 0:
sleep(retry_pause)
# Increase the sleep time at random to avoid multiple clients
# retrying at the same time for e.g. tests
retry_pause += 1 + random.random()
return send_request(url, data, retry_pause, max_tries - 1)
if not res.status_code == 200:
logger.error('Got return code %d from pubmed client.'
% res.status_code)
Expand Down
17 changes: 6 additions & 11 deletions indra/tests/test_db_rest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import random
import unittest
from datetime import datetime
from time import sleep
Expand Down Expand Up @@ -58,23 +57,19 @@ def test_bigger_request():


@pytest.mark.nonpublic
def test_timeout_no_persist_agent():
candidates = ['TP53', 'NFkappaB@FPLX', 'AKT@FPLX']
agent = random.choice(candidates)
print(agent)
resp = dbr.get_statements(agents=[agent], persist=False, timeout=0)
def test_timeout_no_persist_nfkb():
resp = dbr.get_statements(agents=["NFkappaB@FPLX"], persist=False, timeout=0)
assert resp.is_working(), "Lookup resolved too fast."
resp.wait_until_done(70)
assert len(resp.statements) > 0.9*EXPECTED_BATCH_SIZE, len(resp.statements)


@pytest.mark.nonpublic
def test_timeout_no_persist_type_object():
candidates = ['TP53', 'NFkappaB@FPLX', 'AKT@FPLX']
agent = random.choice(candidates)
print(agent)
resp = dbr.get_statements(stmt_type='phosphorylation', object=agent,
persist=False, timeout=0)
resp = dbr.get_statements(stmt_type='phosphorylation',
object="NFkappaB@FPLX",
persist=False,
timeout=0)
assert resp.is_working(), "Lookup resolved too fast."
resp.wait_until_done(70)
assert len(resp.statements) > 0.9*EXPECTED_BATCH_SIZE, len(resp.statements)
Expand Down
Loading