Class: Spidy::Connector::Retryable

Inherits:
Object
  • Object
show all
Defined in:
lib/spidy/connector.rb

Overview

retry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connector, logger:, wait_time:) ⇒ Retryable

Returns a new instance of Retryable.



63
64
65
66
67
68
# File 'lib/spidy/connector.rb', line 63

def initialize(connector, logger:, wait_time:)
  @origin_connector = connector
  @logger = logger
  @wait_time = wait_time
  @retry_attempt_count = 5
end

Instance Attribute Details

#origin_connectorObject (readonly)

Returns the value of attribute origin_connector.



61
62
63
# File 'lib/spidy/connector.rb', line 61

def origin_connector
  @origin_connector
end

Instance Method Details

#call(url, &block) ⇒ Object



70
71
72
# File 'lib/spidy/connector.rb', line 70

def call(url, &block)
  connect(url, &block)
end

#connect(url, retry_attempt_count: @retry_attempt_count, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/spidy/connector.rb', line 74

def connect(url, retry_attempt_count: @retry_attempt_count, &block)
  @logger.call('connnector.get': url, 'connnector.accessed': Time.current)
  @origin_connector.call(url, &block)
rescue Spidy::Connector::Retry => e
  @logger.call('retry.accessed': Time.current,
               'retry.uri': url,
               'retry.response_code': e.response_code,
               'retry.attempt_count': retry_attempt_count)

  retry_attempt_count -= 1
  if retry_attempt_count.positive?
    sleep @wait_time
    @origin_connector.refresh! if @origin_connector.respond_to?(:refresh!)
    retry
  end
  raise e.error
end