Skip to content

Commit

Permalink
Add backoff logic to GoogleCloudStorage.exists().
Browse files Browse the repository at this point in the history
  • Loading branch information
aronasorman committed Mar 21, 2019
1 parent 783fba7 commit 408ae62
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions contentcuration/contentcuration/utils/gcs_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
import mimetypes
import tempfile

import backoff
from django.core.files import File
from django.core.files.storage import Storage
from google.cloud.exceptions import InternalServerError
from google.cloud.storage import Client
from google.cloud.storage.blob import Blob


OLD_STUDIO_STORAGE_PREFIX = "/contentworkshop_content/"

MAX_RETRY_TIME = 60 # seconds


class GoogleCloudStorage(Storage):

Expand Down Expand Up @@ -47,7 +50,7 @@ def open(self, name, mode="rb", blob_object=None):
"""
# We don't have any logic for returning the file object in write
# so just raise an error if we get any mode other than rb
assert mode == "rb",\
assert mode == "rb", \
("Sorry, we can't handle any open mode other than rb."
" Please use Storage.save() instead.")

Expand All @@ -71,7 +74,13 @@ def open(self, name, mode="rb", blob_object=None):
django_file.just_downloaded = True
return django_file

@backoff.on_exception(backoff.expo, InternalServerError, max_time=MAX_RETRY_TIME)
def exists(self, name):
"""
Check if a resource with the given name exists. Has a maximum backoff time of MAX_RETRY_TIME.
:param name: the name of the resource to check
:return: True if the resource with the name exists, or False otherwise.
"""
blob = self.bucket.get_blob(name)
return blob

Expand Down

0 comments on commit 408ae62

Please sign in to comment.