Skip to content

Commit 5875c36

Browse files
authored
Fix 14 api retry (#15)
* fix: adds retry logic to api calls * chore: removes commented out code * chore: runs linters
1 parent 1375d8c commit 5875c36

4 files changed

Lines changed: 29 additions & 10 deletions

File tree

.flake8

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ exclude =
55
build,
66
.venv,
77
venv,
8-
.conda
8+
.conda,
9+
env
910
max-complexity = 10

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ dependencies = [
2323
"pandas",
2424
"awswrangler",
2525
"jinja2",
26-
"requests"
26+
"requests",
27+
"tenacity"
2728
]
2829

2930
[dependency-groups]
@@ -84,5 +85,5 @@ profile = "black"
8485
skip = [".conda"]
8586

8687
[tool.interrogate]
87-
exclude = ["setup.py", "docs", "build", ".conda"]
88+
exclude = ["setup.py", "docs", "build", ".conda", "env"]
8889
fail-under = 100

src/aind_vast_utils/compile_metrics_job.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
from aind_settings_utils.aws import SecretsManagerBaseSettings
1313
from pydantic import Field, SecretStr
1414
from pydantic_settings import SettingsConfigDict
15+
from tenacity import (
16+
before_sleep_log,
17+
retry,
18+
stop_after_attempt,
19+
wait_exponential_jitter,
20+
)
1521
from vastpy import VASTClient
1622

1723
from aind_vast_utils.models import (
@@ -21,7 +27,8 @@
2127
QuotaTableRow,
2228
)
2329

24-
logging.basicConfig(level=logging.INFO)
30+
logger = logging.getLogger(__name__)
31+
logger.setLevel(logging.INFO)
2532

2633

2734
class JobSettings(
@@ -72,6 +79,12 @@ def __init__(self, job_settings: JobSettings):
7279
address=job_settings.address,
7380
)
7481

82+
@retry(
83+
wait=wait_exponential_jitter(initial=1, max=60, exp_base=2, jitter=1),
84+
stop=stop_after_attempt(5),
85+
reraise=True,
86+
before_sleep=before_sleep_log(logger, logging.WARNING), # type: ignore
87+
)
7588
def _get_capacity(self, path: str, sort_key: str = "logical") -> Capacity:
7689
"""Get capacity info for VAST cluster."""
7790

@@ -81,6 +94,12 @@ def _get_capacity(self, path: str, sort_key: str = "logical") -> Capacity:
8194
)
8295
return Capacity.model_validate(response)
8396

97+
@retry(
98+
wait=wait_exponential_jitter(initial=1, max=60, exp_base=2, jitter=1),
99+
stop=stop_after_attempt(5),
100+
reraise=True,
101+
before_sleep=before_sleep_log(logger, logging.WARNING), # type: ignore
102+
)
84103
def _get_quota(self, path: str) -> Quota:
85104
"""Get quota info for VAST cluster."""
86105

src/aind_vast_utils/send_notification_job.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
from pydantic import Field
1616
from pydantic_settings import SettingsConfigDict
1717

18-
logging.basicConfig(level=logging.INFO)
18+
logger = logging.getLogger(__name__)
19+
logger.setLevel(logging.INFO)
1920

2021

2122
class JobSettings(
@@ -61,9 +62,6 @@ def _get_table(self, table_name: str) -> pd.DataFrame:
6162
path = f"{self.job_settings.tables_location}/{table_name}"
6263
report_date = self.job_settings.report_date
6364
report_year = report_date.year
64-
# my_filter = lambda x: x["report_year"] == str(report_year) and x[
65-
# "report_date"
66-
# ] == str(report_date)
6765
df = wr.s3.read_parquet(
6866
path,
6967
dataset=True,
@@ -222,8 +220,8 @@ def run_job(self):
222220
)
223221
self.send_notification(html_str)
224222
else:
225-
logging.info("All quotas good.")
226-
logging.info(quota_df.to_string(index=False))
223+
logger.info("All quotas good.")
224+
logger.info(quota_df.to_string(index=False))
227225

228226

229227
if __name__ == "__main__":

0 commit comments

Comments
 (0)