Skip to content

Commit 63bc6f5

Browse files
committed
SCP-8 updaded upload retry method
1 parent 6f456d3 commit 63bc6f5

File tree

1 file changed

+24
-32
lines changed

1 file changed

+24
-32
lines changed

skylab_studio/studio_client.py

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import base64
1515
import hashlib
1616
import requests
17-
# from io import BytesIO
1817
import sentry_sdk
1918

2019
from .version import VERSION
@@ -327,7 +326,8 @@ def _upload_photo(self, photo_path, id, model='job'):
327326

328327
# Ask studio to create the photo record
329328
photo_resp = self._create_photo(photo_data)
330-
if not photo_resp:
329+
330+
if not 'id' in photo_resp:
331331
raise Exception('Unable to create the photo object, if creating profile photo, ensure enable_extract and replace_background is set to: True')
332332

333333
photo_id = photo_resp['id']
@@ -347,37 +347,30 @@ def _upload_photo(self, photo_path, id, model='job'):
347347
# PUT request to presigned url with image data
348348
headers["Content-MD5"] = b64md5
349349

350-
try:
351-
upload_photo_resp = requests.put(upload_url, data, headers=headers)
352-
# Will raise exception for any statuses 4xx-5xx
353-
upload_photo_resp.raise_for_status()
354-
355-
if not upload_photo_resp:
356-
print('First upload attempt failed, retrying...')
357-
retry = 0
358-
# retry upload
359-
while retry < 3:
360-
try:
361-
upload_photo_resp = requests.put(upload_url, data, headers=headers)
362-
363-
except requests.exceptions.HTTPError as e:
364-
sentry_sdk.capture_exception(e)
365-
print("HTTP error occurred while trying to upload your photo:", e)
366-
367-
if upload_photo_resp:
368-
break # Upload was successful, exit the loop
369-
elif retry == 2: # Check if it's the 3rd retry
370-
raise Exception('Unable to upload to the bucket after retrying.')
371-
else:
372-
time.sleep(max(1, retry)) # Wait before retrying (number of retries in seconds (minimum 1))
373-
retry += 1
350+
retry = 0
351+
while retry < 3:
352+
try:
353+
# attempt to upload the photo to aws
354+
upload_photo_resp = requests.put(upload_url, data, headers=headers)
374355

375-
except Exception as e:
376-
sentry_sdk.capture_exception(e)
377-
print(f"An exception of type {type(e).__name__} occurred: {e}")
378-
print('Deleting created, but unuploaded photo...')
356+
# Will raise exception for any statuses 4xx-5xx
357+
upload_photo_resp.raise_for_status()
358+
359+
# if raise_for_status didn't throw an exception, then we successfully uploaded, exit the loop
360+
break
379361

380-
self.delete_photo(photo_id)
362+
# rescue any exceptions in the loop
363+
except Exception as e:
364+
# if we've retried 3 times, delete the photo record and raise exception
365+
if retry == 2:
366+
self.delete_photo(photo_id)
367+
368+
raise Exception(e)
369+
# if we haven't retried 3 times, wait for retry+1 seconds and continue the while loop
370+
else:
371+
print(f"Attempt #{retry + 1} to upload failed, retrying...")
372+
retry += 1
373+
time.sleep(retry+1)
381374

382375
res['upload_response'] = upload_photo_resp.status_code
383376
return res
@@ -558,4 +551,3 @@ async def download_photo(self, photo_id, output_path, profile = None, options =
558551
finally:
559552
if semaphore != None:
560553
semaphore.release()
561-

0 commit comments

Comments
 (0)