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

fix: don't treat 404 as temporary #72

Merged
merged 1 commit into from
Nov 20, 2023
Merged
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
21 changes: 14 additions & 7 deletions dsp_permissions_scripts/utils/try_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
logger = get_logger(__name__)


def error_is_temporary(response: requests.Response) -> bool:
retry_code = 500 <= response.status_code < 600
try_again_later = "try again later" in response.text
return retry_code or try_again_later


def http_call_with_retry(action: Callable[..., requests.Response], err_msg: str) -> requests.Response:
"""
Function that tries 9 times to execute an HTTP request.
Expand All @@ -34,18 +40,19 @@ def http_call_with_retry(action: Callable[..., requests.Response], err_msg: str)
response: requests.Response = action()
if response.status_code == 200:
return response
retry_code = 500 <= response.status_code < 600 or response.status_code == 404
try_again_later = "try again later" in response.text
if retry_code or try_again_later:
msg = f"{err_msg}. Retry request in {2 ** i} seconds... ({response.status_code}: {response.text})"
print(f"{get_timestamp()}: SERVER ERROR: {msg}")
if error_is_temporary(response):
msg = (
f"SERVER ERROR: {err_msg}. Retry request in {2 ** i} seconds... "
f"({response.status_code}: {response.text})"
)
print(f"{get_timestamp()}: {msg}")
logger.error(msg)
time.sleep(2**i)
continue
return response
except (TimeoutError, ReadTimeout, ReadTimeoutError, RequestException, ConnectionError):
msg = f"{err_msg}. Retry request in {2 ** i} seconds..."
print(f"{get_timestamp()}: SERVER ERROR: {msg}")
msg = f"REQUESTS LIBRARY ERROR: {err_msg}. Retry request in {2 ** i} seconds..."
print(f"{get_timestamp()}: {msg}")
logger.error(msg, exc_info=True)
time.sleep(2**i)
continue
Expand Down