Class: RetryingS3Client

Inherits:
Object
  • Object
show all
Defined in:
lib/retrying_s3_client.rb,
lib/retrying_s3_client/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s3, logger, options = {}) ⇒ RetryingS3Client

Returns a new instance of RetryingS3Client.



9
10
11
12
13
# File 'lib/retrying_s3_client.rb', line 9

def initialize(s3, logger, options = {})
  @s3 = s3
  @logger = logger
  @sleeper = options[:sleeper] || Kernel.method(:sleep)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



15
16
17
# File 'lib/retrying_s3_client.rb', line 15

def method_missing(sym, *args, &block)
  with_backoff { @s3.send(sym, *args, &block) }
end

Class Method Details

.wrap(s3, logger, options) ⇒ Object



4
5
6
7
# File 'lib/retrying_s3_client.rb', line 4

def self.wrap(s3, logger, options)
  return s3 if s3.is_a?(RetryingS3Client)
  new(s3, logger, options)
end

Instance Method Details

#with_backoff(backoff = 1) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/retrying_s3_client.rb', line 19

def with_backoff(backoff = 1)
  yield
rescue Aws::S3::Errors::Http503Error
  @logger.error "Got Aws::S3::Errors::Http503Error, sleeping #{backoff}"
  @sleeper.call(backoff)
  @logger.info "Woke up after Aws::S3::Errors::Http503Error retrying again"
  backoff *= 2
  retry
end