Skip to content

Commit d9a9107

Browse files
committed
retry gets with urllib, not SMILE
1 parent ec6b611 commit d9a9107

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

main.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,7 @@
8787
exp.worker_id_dict = retrieved_worker_id
8888
elif CogBatt_config.WORKER_ID_SOURCE == 'USER':
8989
InputSubject()
90-
for try_n in range(CogBatt_config.TASKGET_TRIES):
91-
tasks_from_api = Func(get_blocks_to_run, Ref.object(exp)._subject, Ref.object(exp).get_var('_code')).result
92-
with If(not (tasks_from_api['content'] in ['Error Connecting', 'Timeout Error'])):
93-
break
94-
sleep(0.25)
90+
tasks_from_api = Func(get_blocks_to_run, Ref.object(exp)._subject, Ref.object(exp).get_var('_code')).result
9591
with If(tasks_from_api['status'] == 'error'):
9692
number_of_tasks = 0
9793
with Else():

utils.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
import plistlib
33
import zipfile
44
import requests
5+
from requests.adapters import HTTPAdapter
6+
from urllib3.util.retry import Retry
57
import logging
68
import time
79
from config import API_BASE_URL, RUNNING_FROM_EXECUTABLE, CURRENT_OS, VERIFY, WORKER_ID_SOURCE,\
8-
UPLOAD_EXTRA_FILES, EXPECTED_NUMBER_OF_BLOCKS
10+
UPLOAD_EXTRA_FILES, EXPECTED_NUMBER_OF_BLOCKS, TASKGET_TRIES
911
from hashlib import blake2b
1012
from io import BytesIO
1113
from pathlib import Path
@@ -17,7 +19,13 @@
1719
logging.basicConfig(level=logging.INFO,
1820
format='%(asctime)s - %(levelname)s - %(message)s')
1921

20-
22+
RETRY_STRATEGY = Retry(
23+
total=TASKGET_TRIES, # Maximum number of retries
24+
status=0,
25+
redirect=False,
26+
backoff_factor=0.2, # Factor for exponential backoff between retries
27+
allowed_methods=["GET"] # Methods for which to retry
28+
)
2129
def get_blocks_to_run(worker_id: str, code: str | None = None) -> list[str] | dict[str, str]:
2230
"""
2331
Sends a GET request to retrieve the list of blocks that are yet to be run by the worker.
@@ -45,7 +53,11 @@ def get_blocks_to_run(worker_id: str, code: str | None = None) -> list[str] | di
4553
params = {'worker_id': worker_id, 'code':code}
4654

4755
try:
48-
response = requests.get(url, params=params, verify=VERIFY)
56+
adapter = HTTPAdapter(max_retries=RETRY_STRATEGY)
57+
session = requests.Session()
58+
session.mount("http://", adapter)
59+
session.mount("https://", adapter)
60+
response = session.get(url, params=params, verify=VERIFY)
4961
response.raise_for_status() # Raise an error for non-2xx status codes
5062
tasks = response.json().get('blocks_to_run', [])
5163
logging.info(f'Tasks to run: {tasks}')

0 commit comments

Comments
 (0)