Skip to content

Commit

Permalink
Guard against GCS rate limits in Vision
Browse files Browse the repository at this point in the history
  • Loading branch information
blowmage committed May 15, 2018
1 parent 622b29d commit f0d4049
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion google-cloud-vision/acceptance/vision/vision_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

let(:https_url) { "https://raw.githubusercontent.com/GoogleCloudPlatform/google-cloud-ruby/master/acceptance/data/face.jpg" }

let(:bucket) { storage.bucket($vision_prefix) || storage.create_bucket($vision_prefix) }
let(:bucket) { storage.bucket($vision_prefix) || safe_gcs_execute { storage.create_bucket($vision_prefix) } }
let(:gcs_file) { bucket.file(face_image) || bucket.create_file(face_image) }
let(:gcs_url) { gcs_file.to_gs_url }

Expand Down
16 changes: 15 additions & 1 deletion google-cloud-vision/acceptance/vision_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ def self.run_one_method klass, method_name, reporter
end
end

def safe_gcs_execute retries: 20, delay: 2
current_retries = 0
loop do
begin
return yield
rescue Google::Cloud::ResourceExhaustedError
raise unless current_retries >= retries

sleep delay
current_retries += 1
end
end
end

# Create buckets to be shared with all the tests
require "time"
require "securerandom"
Expand All @@ -96,7 +110,7 @@ def clean_up_vision_storage_objects
puts "Cleaning up storage buckets after vision tests."
if b = $storage.bucket($vision_prefix)
b.files.all(&:delete)
b.delete
safe_gcs_execute { b.delete }
end
rescue => e
puts "Error while cleaning up storage buckets after vision tests.\n\n#{e}"
Expand Down

0 comments on commit f0d4049

Please sign in to comment.