Class: HermesMessengerOfTheGods::Endpoints::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/hermes_messenger_of_the_gods/endpoints/base.rb

Direct Known Subclasses

Local, Sns, Sqs

Constant Summary collapse

DEFAULT_OPTIONS =
{ retries: 3, jitter: true, backoff: :linear, jsonify: true }.freeze
DEFAULT_RETRYS =
{
  linear: ->(n) { n }, # Wait 1 sec, then two secondns, then three...
  exponential: ->(n) { (2**n) / (n + 1).to_f }, # Wait 1, then 1.3, then 2 sec
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, endpoint, options = {}) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
20
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 15

def initialize(name, endpoint, options = {})
  self.name = name
  self.options = self.class::DEFAULT_OPTIONS.merge(options)
  self.endpoint = endpoint
  self.errors = []
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



13
14
15
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 13

def endpoint
  @endpoint
end

#errorsObject

Returns the value of attribute errors.



13
14
15
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 13

def errors
  @errors
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 13

def name
  @name
end

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 13

def options
  @options
end

#resultObject

Returns the value of attribute result.



13
14
15
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 13

def result
  @result
end

Instance Method Details

#_transmit_payload(message, options) ⇒ Object



28
29
30
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 28

def _transmit_payload(message, options)
  to_transmit_payload(transform_message(message), message, options)
end

#backoff(retry_number) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 58

def backoff(retry_number)
  return unless options[:backoff]

  backoff_arg = if options[:backoff].respond_to?(:call)
                  options[:backoff]
                else
                  DEFAULT_RETRYS[options[:backoff]]
                end
  sleep_time = backoff_arg.call(retry_number)

  if sleep_time
    sleep(sleep_time * (options[:jitter] ? rand(0.3..1) : 1))
  end
end

#bulk_dispatch!(messages, options) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 32

def bulk_dispatch!(messages, options)
  payloads = messages.map do |message|
    _transmit_payload(message, options)
  end

  bulk_transmit(payloads)
end

#dispatch(*args) ⇒ Object



22
23
24
25
26
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 22

def dispatch(*args)
  dispatch!(*args)
rescue StandardError
  false
end

#dispatch!(message, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 40

def dispatch!(message, options = {})
  retry_number = 0
  begin
    self.result = transmit(_transmit_payload(message, options))
  rescue StandardError => e
    errors << e
    retry_number += 1
    if retry_number < max_retries && retry_from(e)
      backoff(retry_number)
      retry
    else
      raise
    end
  end

  true
end

#fetch_option(option, *args, &blk) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 105

def fetch_option(option, *args, &blk)
  if options[option].respond_to?(:call)
    options[option].call(*args, &blk)
  else
    options[option]
  end || {}
end

#handle_failure(_job, e) ⇒ Object



103
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 103

def handle_failure(_job, e); end

#handle_success(_job) ⇒ Object



101
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 101

def handle_success(_job); end

#max_retriesObject



95
96
97
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 95

def max_retries
  options[:retries] || 0
end

#retry_from(exception) ⇒ Object



91
92
93
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 91

def retry_from(exception)
  !exception.is_a?(HermesMessengerOfTheGods::Endpoints::FatalError)
end

#teardownObject



99
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 99

def teardown; end

#transform_message(message) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 73

def transform_message(message)
  transformer_name = "to_#{self.class.to_s.demodulize.underscore}_message"

  if options[:transformer]
    if options[:transformer].respond_to?(:call)
      options[:transformer].call(message)
    else
      message.send(options[:transformer])
    end
  elsif message.respond_to?(transformer_name)
    message.public_send(transformer_name)
  elsif message.respond_to?(:_build_for_transmission)
    message._build_for_transmission
  else
    message
  end
end