@@ -57,7 +57,7 @@ def __init__(
57
57
58
58
59
59
def uploadCSV (self , table , filename , tries = 1 ):
60
- limit_of_retries = 3
60
+ limit_of_retries = 9
61
61
self .not_uploaded = False
62
62
params = {
63
63
'name' : table ,
@@ -66,7 +66,6 @@ def uploadCSV(self, table, filename, tries=1):
66
66
}
67
67
68
68
try :
69
-
70
69
with open (filename , 'rb' ) as f :
71
70
m = MultipartEncoder (fields = {'csv' : ('csv' , f , 'text/csv' )})
72
71
url = f"{ self .tb_host } /v0/datasources"
@@ -81,34 +80,51 @@ def uploadCSV(self, table, filename, tries=1):
81
80
params = params ,
82
81
verify = False )
83
82
84
- logging .info (response .content ) # this is ugly, but we need to check what is in the response for some detected errors
83
+ logging .debug (response .content )
85
84
if response .status_code == 200 :
86
85
json_object = json .loads (response .content )
87
86
logging .debug (f"Import id: { json_object ['import_id' ]} " )
87
+ return
88
+
88
89
elif response .status_code == 429 :
90
+ # retry with the recommended value
89
91
retry_after = int (response .headers ['Retry-After' ]) + tries
90
92
logging .error (f"Too many requests retrying in { retry_after } seconds to upload { filename } to { table } " )
91
93
time .sleep (retry_after )
92
94
self .uploadCSV (table , filename , tries + 1 )
93
- else :
94
- # In case of error let's retry only `limit_of_retries` times
95
- logging .exception (response .content )
95
+ elif response . status_code >= 500 :
96
+ # In case of server error let's retry `limit_of_retries` times, but exponentially
97
+ logging .error (response .content )
96
98
if tries > limit_of_retries :
99
+ logging .debug (f'Limit of retries reached for { filename } ' )
97
100
self .not_uploaded = True
98
101
return
99
- logging .info (f"Retrying { filename } when status { response .status_code } , try { tries } of { limit_of_retries } " )
100
- time .sleep (tries )
102
+
103
+ retry_after = 2 ** tries
104
+ logging .info (f"Retrying { filename } when status { response .status_code } , waiting { retry_after } , try { tries } of { limit_of_retries } " )
105
+ time .sleep (retry_after )
101
106
self .uploadCSV (table , filename , tries + 1 )
107
+ else :
108
+ # In case of other client errors (400, 404, etc...) don't retry
109
+ logging .error (response .content )
110
+ logging .error (f"Not retrying { filename } when status { response .status_code } " )
111
+ self .not_uploaded = True
112
+ return
113
+
102
114
except Exception as e :
115
+ # As we don't know what went wrong... let's retry `limit_of_retries` times, but exponentially
103
116
logging .exception (e )
104
117
if tries > limit_of_retries :
118
+ logging .debug (f'Limit of retries reached for { filename } ' )
105
119
self .not_uploaded = True
106
120
return
107
- # We wait tries^2 sec to try again
108
- logging .info (f"Retrying { filename } when exception: try { tries } of { limit_of_retries } " )
109
- time .sleep (tries * tries )
121
+
122
+ retry_after = 2 ** tries
123
+ logging .info (f"Retrying { filename } when exception, waiting { retry_after } try { tries } of { limit_of_retries } " )
124
+ time .sleep (retry_after )
110
125
self .uploadCSV (table , filename , tries + 1 )
111
126
127
+
112
128
def insert (self , event_or_events = None ):
113
129
# event_or_events = [
114
130
# event: {
0 commit comments