3
3
4
4
from __future__ import absolute_import
5
5
6
- import os
7
6
import io
7
+ import os
8
8
import requests .exceptions
9
- from tusclient import client
10
9
from . import exceptions
10
+ from time import sleep
11
+ from tusclient import client
11
12
12
13
try :
13
14
basestring
@@ -119,7 +120,7 @@ def replace(self, video_uri, filename, **kwargs):
119
120
120
121
def __perform_tus_upload (self , filename , attempt ):
121
122
"""Take an upload attempt and perform the actual upload via tus.
122
-
123
+ Upon encountering an error/exception will attempt retries (3) with an exponential cooldown.
123
124
https://tus.io/
124
125
125
126
Args:
@@ -135,17 +136,23 @@ def __perform_tus_upload(self, filename, attempt):
135
136
video.
136
137
"""
137
138
upload_link = attempt .get ('upload' ).get ('upload_link' )
138
-
139
- try :
140
- with io .open (filename , 'rb' ) as fs :
141
- tus_client = client .TusClient ('https://files.tus.vimeo.com' )
142
- uploader = tus_client .uploader (file_stream = fs , url = upload_link )
143
- uploader .upload ()
144
- except Exception as e :
145
- raise exceptions .VideoUploadFailure (
146
- e ,
147
- 'Unexpected error when uploading through tus.'
148
- )
139
+ failures = 0
140
+ max_failures = 3
141
+
142
+ while failures < max_failures :
143
+ try :
144
+ with io .open (filename , 'rb' ) as fs :
145
+ tus_client = client .TusClient ('https://files.tus.vimeo.com' )
146
+ uploader = tus_client .uploader (file_stream = fs , url = upload_link )
147
+ uploader .upload ()
148
+ except Exception as e :
149
+ failures += 1
150
+ if failures >= max_failures :
151
+ raise exceptions .VideoUploadFailure (
152
+ e ,
153
+ 'Unexpected error when uploading through tus.'
154
+ )
155
+ sleep (pow (4 , failures ))
149
156
150
157
return attempt .get ('uri' )
151
158
0 commit comments