Commit e8c1dc8a by Jon Yurek

Backoff when we get a SlowDown error

parent dca87ec5
...@@ -329,6 +329,7 @@ module Paperclip ...@@ -329,6 +329,7 @@ module Paperclip
def flush_writes #:nodoc: def flush_writes #:nodoc:
@queued_for_write.each do |style, file| @queued_for_write.each do |style, file|
retries = 0
begin begin
log("saving #{path(style)}") log("saving #{path(style)}")
acl = @s3_permissions[style] || @s3_permissions[:default] acl = @s3_permissions[style] || @s3_permissions[:default]
...@@ -357,9 +358,17 @@ module Paperclip ...@@ -357,9 +358,17 @@ module Paperclip
write_options.merge!(@s3_headers) write_options.merge!(@s3_headers)
s3_object(style).write(file, write_options) s3_object(style).write(file, write_options)
rescue AWS::S3::Errors::NoSuchBucket => e rescue AWS::S3::Errors::NoSuchBucket
create_bucket create_bucket
retry retry
rescue AWS::S3::Errors::SlowDown
retries += 1
if retries <= 3
sleep((retries ** 2) * 500)
retry
else
raise
end
ensure ensure
file.rewind file.rewind
end end
......
...@@ -711,6 +711,15 @@ describe Paperclip::Storage::S3 do ...@@ -711,6 +711,15 @@ describe Paperclip::Storage::S3 do
"Expect all the files to be deleted." "Expect all the files to be deleted."
end end
it "will retry to save again but back off on SlowDown" do
@dummy.avatar.stubs(:sleep)
AWS::S3::S3Object.any_instance.stubs(:write).
raises(AWS::S3::Errors::SlowDown.new(stub, stub(status: 503, body: "")))
expect {@dummy.save}.to raise_error(AWS::S3::Errors::SlowDown)
expect(@dummy.avatar).to have_received(:sleep).times(3)
end
context "and saved" do context "and saved" do
before do before do
object = stub object = stub
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment