Skip to content

Commit

Permalink
Merge pull request #314 from solvebio/fail-on-upload
Browse files Browse the repository at this point in the history
retry on more status codes, raise error when upload unsuccessful
  • Loading branch information
jsh2134 committed Nov 1, 2019
2 parents 58eb320 + 7330364 commit 9bba500
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
4 changes: 4 additions & 0 deletions solvebio/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class NotFoundError(Exception):
pass


class FileUploadError(Exception):
pass


class SolveError(Exception):
"""Exceptions tailored to the kinds of errors from a SolveBio API
request"""
Expand Down
20 changes: 15 additions & 5 deletions solvebio/resource/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import mimetypes

import requests
from requests.packages.urllib3.util.retry import Retry

from solvebio import SolveError
from solvebio.errors import SolveError
from solvebio.errors import FileUploadError
from solvebio.utils.md5sum import md5sum

from ..client import client
Expand Down Expand Up @@ -223,19 +225,27 @@ def upload_file(cls, local_path, remote_path, vault_full_path, **kwargs):

# Use a session with a retry policy to handle connection errors.
session = requests.Session()
session.mount('https://', requests.adapters.HTTPAdapter(max_retries=5))
max_retries = 5
retry = Retry(
total=max_retries,
read=max_retries,
connect=max_retries,
backoff_factor=0.3,
status_forcelist=(500, 502, 504, 400),
)
session.mount(
'https://', requests.adapters.HTTPAdapter(max_retries=retry))
upload_resp = session.put(upload_url,
data=open(local_path, 'rb'),
headers=headers)

if upload_resp.status_code != 200:
print('Notice: Upload status code for {0} was {1}'.format(
print('WARNING: Upload status code for {0} was {1}'.format(
local_path, upload_resp.status_code
))
print('See error message below:')
print(upload_resp.content)
# Clean up the failed upload
obj.delete(force=True)
raise FileUploadError(upload_resp.content)
else:
print('Notice: Successfully uploaded {0} to {1}'.format(local_path,
obj.path))
Expand Down

0 comments on commit 9bba500

Please sign in to comment.