Skip to content

Commit 7c178bb

Browse files
committed
Refactor retry_on_timeout for reuse.
1 parent 2296676 commit 7c178bb

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

planemo/galaxy/activity.py

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@
3636
unicodify,
3737
)
3838
from pathvalidate import sanitize_filename
39-
from requests.exceptions import (
40-
HTTPError,
41-
RequestException,
42-
)
39+
from requests.exceptions import HTTPError
4340

44-
from planemo.galaxy.api import summarize_history
41+
from planemo.galaxy.api import (
42+
retry_on_timeouts,
43+
summarize_history,
44+
)
4545
from planemo.io import wait_on
4646
from planemo.runnable import (
4747
ErrorRunResponse,
@@ -811,30 +811,11 @@ def wait_for_invocation_and_jobs(
811811

812812
def _wait_for_invocation(ctx, gi, invocation_id, polling_backoff=0):
813813
def state_func():
814-
return _retry_on_timeouts(ctx, gi, lambda gi: gi.invocations.show_invocation(invocation_id))
814+
return retry_on_timeouts(ctx, gi, lambda gi: gi.invocations.show_invocation(invocation_id))
815815

816816
return _wait_on_state(state_func, polling_backoff)
817817

818818

819-
def _retry_on_timeouts(ctx, gi, f):
820-
gi.timeout = 60
821-
try_count = 5
822-
try:
823-
for try_num in range(try_count):
824-
start_time = time.time()
825-
try:
826-
return f(gi)
827-
except RequestException:
828-
end_time = time.time()
829-
if end_time - start_time > 45 and (try_num + 1) < try_count:
830-
ctx.vlog("Galaxy seems to have timed out, retrying to fetch status.")
831-
continue
832-
else:
833-
raise
834-
finally:
835-
gi.timeout = None
836-
837-
838819
def has_jobs_in_states(ctx, gi, history_id, states):
839820
params = {"history_id": history_id}
840821
jobs_url = gi.url + "/jobs"
@@ -849,7 +830,7 @@ def _wait_for_history(ctx, gi, history_id, polling_backoff=0):
849830
# no need to wait for active jobs anymore I think.
850831

851832
def state_func():
852-
return _retry_on_timeouts(ctx, gi, lambda gi: gi.histories.show_history(history_id))
833+
return retry_on_timeouts(ctx, gi, lambda gi: gi.histories.show_history(history_id))
853834

854835
return _wait_on_state(state_func, polling_backoff)
855836

@@ -862,7 +843,7 @@ def _wait_for_invocation_jobs(ctx, gi, invocation_id, polling_backoff=0):
862843
ctx.log(f"waiting for invocation {invocation_id}")
863844

864845
def state_func():
865-
return _retry_on_timeouts(ctx, gi, lambda gi: gi.jobs.get_jobs(invocation_id=invocation_id))
846+
return retry_on_timeouts(ctx, gi, lambda gi: gi.jobs.get_jobs(invocation_id=invocation_id))
866847

867848
return _wait_on_state(state_func, polling_backoff)
868849

planemo/galaxy/api.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"""A high-level interface to local Galaxy instances using bioblend."""
22

3+
import time
34
from io import StringIO
45
from typing import Optional
56

67
from bioblend.galaxy import GalaxyInstance
8+
from requests.exceptions import RequestException
79

810
DEFAULT_ADMIN_API_KEY = "test_key"
911

@@ -136,6 +138,25 @@ def _dataset_provenance(gi, history_id, id):
136138
return provenance
137139

138140

141+
def retry_on_timeouts(ctx, gi, f):
142+
gi.timeout = 60
143+
try_count = 5
144+
try:
145+
for try_num in range(try_count):
146+
start_time = time.time()
147+
try:
148+
return f(gi)
149+
except RequestException:
150+
end_time = time.time()
151+
if end_time - start_time > 45 and (try_num + 1) < try_count:
152+
ctx.vlog("Galaxy seems to have timed out, retrying to fetch status.")
153+
continue
154+
else:
155+
raise
156+
finally:
157+
gi.timeout = None
158+
159+
139160
__all__ = (
140161
"DEFAULT_ADMIN_API_KEY",
141162
"gi",

0 commit comments

Comments
 (0)