Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tus retry logic with cooldown. #126

Merged
merged 1 commit into from
Jul 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions vimeo/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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')

Expand Down