Skip to content

Commit

Permalink
wrap download in a retry loop
Browse files Browse the repository at this point in the history
  • Loading branch information
n8kim1 committed Feb 9, 2021
1 parent 496b59f commit da0814d
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions backend/resumes/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
GCLOUD_BUCKET_RESUMES = 'bc20-resumes'
USERS_ALL_PATH = os.path.join(FILE_PATH, 'users_all.csv')
USERS_TEAMS_PATH = os.path.join(FILE_PATH, 'users_teams.csv')
NUM_RETRIES = 5

# load up the sql query results, as list of dictionaries
users_all = []
Expand Down Expand Up @@ -70,16 +71,18 @@ def safe_makedirs(directory):

# download helper!
def download(user_id, file_name, bucket, files_dir):
try:
blob = bucket.get_blob(os.path.join(str(user_id), 'resume.pdf'))
with open(os.path.join(files_dir, file_name), 'wb+') as file_obj:
blob.download_to_file(file_obj)
file_obj.close()
except PermissionError:
print("Could not obtain permissions to save; try running as sudo")
except Exception as e:
print("Could not retrieve source file from bucket, user id", user_id)
print("Exception:", e)
for i in range (NUM_RETRIES):
try:
blob = bucket.get_blob(os.path.join(str(user_id), 'resume.pdf'))
with open(os.path.join(files_dir, file_name), 'wb+') as file_obj:
blob.download_to_file(file_obj)
file_obj.close()
break
except PermissionError:
print("Could not obtain permissions to save; try running as sudo")
except Exception as e:
print("Could not retrieve source file from bucket, user id", user_id)
print("Exception:", e)

# actually download resumes, first from users_teams
def download_user(user, bucket, files_dir):
Expand Down

0 comments on commit da0814d

Please sign in to comment.