-
Notifications
You must be signed in to change notification settings - Fork 206
SG-38959 improvements #419
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
Draft
julien-lang
wants to merge
11
commits into
master
Choose a base branch
from
ticket/SG-38959-improvements
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b6d9085
remove useless try/except
julien-lang df0b865
TODO
julien-lang 72dff73
TODO
julien-lang 244c600
Better logs
julien-lang 1837dee
Modern Python
julien-lang a9fb594
better doc
julien-lang 1ad8bf8
TODO
julien-lang 947586f
tests
julien-lang 49ebd85
better
julien-lang 0fa345e
Remove MAX_ATTEMPTS and BACKOFF variables. Use config variables instead
julien-lang 21267a1
WIP Add a setting to control which HTTP error code we retry on
julien-lang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -360,7 +360,9 @@ def __init__(self, sg): | |||||
| :param sg: Shotgun connection. | ||||||
| """ | ||||||
| self._sg = sg | ||||||
|
|
||||||
| self.max_rpc_attempts = 3 | ||||||
|
|
||||||
| # rpc_attempt_interval stores the number of milliseconds to wait between | ||||||
| # request retries. By default, this will be 3000 milliseconds. You can | ||||||
| # override this by setting this property on the config like so: | ||||||
|
|
@@ -372,11 +374,15 @@ def __init__(self, sg): | |||||
| # In the case that the environment variable is already set, setting the | ||||||
| # property on the config will override it. | ||||||
| self.rpc_attempt_interval = 3000 | ||||||
| # From http://docs.python.org/2.6/library/httplib.html: | ||||||
|
|
||||||
| # From https://docs.python.org/3.9/library/http.client.html: | ||||||
| # If the optional timeout parameter is given, blocking operations | ||||||
| # (like connection attempts) will timeout after that many seconds | ||||||
| # (if it is not given, the global default timeout setting is used) | ||||||
| self.timeout_secs = None | ||||||
|
|
||||||
| self.http_error_codes_to_retry = [502, 503, 504] # Should we add 500 ?? | ||||||
|
|
||||||
| self.api_ver = "api3" | ||||||
| self.convert_datetimes_to_utc = True | ||||||
| self._records_per_page = None | ||||||
|
|
@@ -460,8 +466,6 @@ class Shotgun(object): | |||||
| ) | ||||||
|
|
||||||
| _MULTIPART_UPLOAD_CHUNK_SIZE = 20000000 | ||||||
| MAX_ATTEMPTS = 3 # Retries on failure | ||||||
| BACKOFF = 0.75 # Seconds to wait before retry, times the attempt number | ||||||
|
|
||||||
| def __init__( | ||||||
| self, | ||||||
|
|
@@ -470,6 +474,7 @@ def __init__( | |||||
| api_key=None, | ||||||
| convert_datetimes_to_utc=True, | ||||||
| http_proxy=None, | ||||||
| http_error_codes_to_retry: None | list[int] = None, | ||||||
| connect=True, | ||||||
| ca_certs=None, | ||||||
| login=None, | ||||||
|
|
@@ -616,6 +621,28 @@ def __init__( | |||||
| "got '%s'." % self.config.rpc_attempt_interval | ||||||
| ) | ||||||
|
|
||||||
| # Handle new config parameter for retry on HTTP | ||||||
| config_value = os.environ.get("SHOTGUN_API_HTTP_ERROR_CODES_TO_RETRY") | ||||||
| if config_value: | ||||||
| values = config_value.strip() | ||||||
| # TODO how to pass an empty list to say we don't want to retry any errors? | ||||||
| adding_mode = False | ||||||
| if values.startswith("+"): | ||||||
| # If starts with a + that means we hadd the codes to the existing list | ||||||
| # Otherwise, we start with an empty list | ||||||
| adding_mode = True | ||||||
| values = values[1:].strip() | ||||||
|
|
||||||
| # TODO check if int all | ||||||
| codes = [int(code.strip()) for code in values.split(",")] | ||||||
|
|
||||||
| if not adding_mode: | ||||||
| self.config.http_error_codes_to_retry = [] | ||||||
|
|
||||||
| self.config.http_error_codes_to_retry.extend(codes) | ||||||
| elif http_error_codes_to_retry is not None: | ||||||
| self.config.http_error_codes_to_retry = http_error_codes_to_retry | ||||||
|
|
||||||
| global SHOTGUN_API_DISABLE_ENTITY_OPTIMIZATION | ||||||
| if ( | ||||||
| os.environ.get("SHOTGUN_API_DISABLE_ENTITY_OPTIMIZATION", "0") | ||||||
|
|
@@ -3637,8 +3664,16 @@ def _call_rpc(self, method, params, include_auth_params=True, first=False): | |||||
| if self.config.localized is True: | ||||||
| req_headers["locale"] = "auto" | ||||||
|
|
||||||
| attempt = 1 | ||||||
| while attempt <= self.MAX_ATTEMPTS: | ||||||
| max_rpc_attempts = self.config.max_rpc_attempts | ||||||
| rpc_attempt_interval = self.config.rpc_attempt_interval / 1000.0 | ||||||
|
|
||||||
| attempt = 0 | ||||||
| while attempt < max_rpc_attempts: | ||||||
| if attempt: | ||||||
| time.sleep(attempt * rpc_attempt_interval) | ||||||
|
|
||||||
| attempt += 1 | ||||||
|
|
||||||
| http_status, resp_headers, body = self._make_call( | ||||||
| "POST", | ||||||
| self.config.api_path, | ||||||
|
|
@@ -3656,10 +3691,15 @@ def _call_rpc(self, method, params, include_auth_params=True, first=False): | |||||
| # We've seen some rare instances of PTR returning 502 for issues that | ||||||
| # appear to be caused by something internal to PTR. We're going to | ||||||
| # allow for limited retries for those specifically. | ||||||
| if attempt != self.MAX_ATTEMPTS and e.errcode in [502, 504]: | ||||||
| LOG.debug("Got a 502 or 504 response. Waiting and retrying...") | ||||||
| time.sleep(float(attempt) * self.BACKOFF) | ||||||
| attempt += 1 | ||||||
| if ( | ||||||
| attempt < max_rpc_attempts | ||||||
| and e.errcode in self.config.http_error_codes_to_retry | ||||||
| ): | ||||||
| # TODO if status[0] == 503: | ||||||
| # errmsg = "Flow Production Tracking is currently down for maintenance or too busy to reply. Please try again later." | ||||||
| LOG.debug( | ||||||
| f"Got a {e.errcode} HTTP response. Waiting and retrying..." | ||||||
| ) | ||||||
| continue | ||||||
| elif e.errcode == 403: | ||||||
| # 403 is returned with custom error page when api access is blocked | ||||||
|
|
@@ -3795,6 +3835,9 @@ def _make_call(self, verb, path, body, headers): | |||||
| rpc_attempt_interval = self.config.rpc_attempt_interval / 1000.0 | ||||||
|
|
||||||
| while attempt < max_rpc_attempts: | ||||||
| if attempt: | ||||||
| time.sleep(attempt * rpc_attempt_interval) | ||||||
|
|
||||||
| attempt += 1 | ||||||
| try: | ||||||
| return self._http_request(verb, path, body, req_headers) | ||||||
|
|
@@ -3814,6 +3857,7 @@ def _make_call(self, verb, path, body, headers): | |||||
| if attempt == max_rpc_attempts: | ||||||
| LOG.debug("Request failed. Giving up after %d attempts." % attempt) | ||||||
| raise | ||||||
| # TODO create only one attempt for SSL errors. | ||||||
| except Exception as e: | ||||||
| self._close_connection() | ||||||
| LOG.debug(f"Request failed. Reason: {e}", exc_info=True) | ||||||
|
|
@@ -3823,7 +3867,6 @@ def _make_call(self, verb, path, body, headers): | |||||
| "Request failed, attempt %d of %d. Retrying in %.2f seconds..." | ||||||
| % (attempt, max_rpc_attempts, rpc_attempt_interval) | ||||||
| ) | ||||||
| time.sleep(rpc_attempt_interval) | ||||||
|
|
||||||
| def _http_request(self, verb, path, body, headers): | ||||||
| """ | ||||||
|
|
@@ -3854,12 +3897,7 @@ def _make_upload_request(self, request, opener): | |||||
| Open the given request object, return the | ||||||
| response, raises URLError on protocol errors. | ||||||
| """ | ||||||
| try: | ||||||
| result = opener.open(request) | ||||||
|
|
||||||
| except urllib.error.HTTPError: | ||||||
| raise | ||||||
| return result | ||||||
| return opener.open(request) | ||||||
|
|
||||||
| def _parse_http_status(self, status): | ||||||
| """ | ||||||
|
|
@@ -3873,8 +3911,6 @@ def _parse_http_status(self, status): | |||||
|
|
||||||
| if status[0] >= 300: | ||||||
| headers = "HTTP error from server" | ||||||
| if status[0] == 503: | ||||||
| errmsg = "Flow Production Tracking is currently down for maintenance or too busy to reply. Please try again later." | ||||||
| raise ProtocolError(self.config.server, error_code, errmsg, headers) | ||||||
|
|
||||||
| return | ||||||
|
|
@@ -4341,30 +4377,48 @@ def _upload_data_to_storage(self, data, content_type, size, storage_url): | |||||
| :param str content_type: Content type of the data stream. | ||||||
| :param int size: Number of bytes in the data stream. | ||||||
| :param str storage_url: Target URL for the uploaded file. | ||||||
| :returns: upload url. | ||||||
| :returns: upload url - NO! Return the AWS object ID. | ||||||
| :rtype: str | ||||||
| """ | ||||||
|
|
||||||
| attempt = 1 | ||||||
| while attempt <= self.MAX_ATTEMPTS: | ||||||
| ## TODO - add unitests for those cases | ||||||
|
||||||
| ## TODO - add unitests for those cases | |
| ## TODO - add unit tests for those cases |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value description is unclear and contains an editorial comment ('NO!'). Update this to clearly document what is actually returned by the function.