77
88
99class GsUploadThread :
10- def __init__ (self , backup_dir , file_path , gs_path , bucket , project_id , access_key , secret_key , remove_uploaded = False ):
10+ def __init__ (self , backup_dir , file_path , gs_path , bucket , project_id , access_key , secret_key , remove_uploaded = False , retries = 5 ):
1111 self .backup_dir = backup_dir
1212 self .file_path = file_path
1313 self .gs_path = gs_path
@@ -16,6 +16,7 @@ def __init__(self, backup_dir, file_path, gs_path, bucket, project_id, access_ke
1616 self .access_key = access_key
1717 self .secret_key = secret_key
1818 self .remove_uploaded = remove_uploaded
19+ self .retries = retries
1920
2021 self .path = "%s/%s" % (self .bucket , self .gs_path )
2122 self .meta_data_dir = "mongodb_consistent_backup-META"
@@ -76,10 +77,21 @@ def run(self):
7677 logging .debug ("Path %s does not exist, uploading" % self .path )
7778
7879 try :
79- f = open (self .file_path , 'r' )
80- uri = self .get_uri ()
81- logging .info ("Uploading %s to Google Cloud Storage" % self .path )
82- uri .new_key ().set_contents_from_file (f )
80+ f = open (self .file_path , 'r' )
81+ uri = self .get_uri ()
82+ retry = 0
83+ error = None
84+ while retry < self .retries :
85+ try :
86+ logging .info ("Uploading %s to Google Cloud Storage (attempt %i/%i)" % (self .path , retry , self .retries ))
87+ uri .new_key ().set_contents_from_file (f )
88+ except Exception , e :
89+ logging .error ("Received error for Google Cloud Storage upload of %s: %s" % (self .path , e ))
90+ error = e
91+ retry += 1
92+ continue
93+ if retry >= self .retries and error :
94+ raise error
8395 finally :
8496 if f :
8597 f .close ()
0 commit comments