From 9032a98d25a8b6b8ffd98c3cf674bea02b4bb7cf Mon Sep 17 00:00:00 2001 From: DomModica Date: Fri, 13 Jul 2018 12:47:31 -0400 Subject: [PATCH] Tus retry logic with cooldown. --- vimeo/upload.py | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/vimeo/upload.py b/vimeo/upload.py index eaa26f4..4e97e1b 100644 --- a/vimeo/upload.py +++ b/vimeo/upload.py @@ -3,11 +3,12 @@ from __future__ import absolute_import -import os import io +import os import requests.exceptions -from tusclient import client from . import exceptions +from time import sleep +from tusclient import client try: basestring @@ -119,7 +120,7 @@ def replace(self, video_uri, filename, **kwargs): def __perform_tus_upload(self, filename, attempt): """Take an upload attempt and perform the actual upload via tus. - + Upon encountering an error/exception will attempt retries (3) with an exponential cooldown. https://tus.io/ Args: @@ -135,17 +136,23 @@ def __perform_tus_upload(self, filename, attempt): video. """ upload_link = attempt.get('upload').get('upload_link') - - try: - with io.open(filename, 'rb') as fs: - tus_client = client.TusClient('https://files.tus.vimeo.com') - uploader = tus_client.uploader(file_stream=fs, url=upload_link) - uploader.upload() - except Exception as e: - raise exceptions.VideoUploadFailure( - e, - 'Unexpected error when uploading through tus.' - ) + failures = 0 + max_failures = 3 + + while failures < max_failures: + try: + with io.open(filename, 'rb') as fs: + tus_client = client.TusClient('https://files.tus.vimeo.com') + uploader = tus_client.uploader(file_stream=fs, url=upload_link) + uploader.upload() + except Exception as e: + failures += 1 + if failures >= max_failures: + raise exceptions.VideoUploadFailure( + e, + 'Unexpected error when uploading through tus.' + ) + sleep(pow(4, failures)) return attempt.get('uri')