Class: HermesMessengerOfTheGods::Endpoints::Base
- Inherits:
-
Object
- Object
- HermesMessengerOfTheGods::Endpoints::Base
- Defined in:
- lib/hermes_messenger_of_the_gods/endpoints/base.rb
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
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#result ⇒ Object
Returns the value of attribute result.
Instance Method Summary collapse
- #_transmit_payload(message, options) ⇒ Object
- #backoff(retry_number) ⇒ Object
- #bulk_dispatch!(messages, options) ⇒ Object
- #dispatch(*args) ⇒ Object
- #dispatch!(message, options = {}) ⇒ Object
- #fetch_option(option, *args, &blk) ⇒ Object
- #handle_failure(_job, e) ⇒ Object
- #handle_success(_job) ⇒ Object
-
#initialize(name, endpoint, options = {}) ⇒ Base
constructor
A new instance of Base.
- #max_retries ⇒ Object
- #retry_from(exception) ⇒ Object
- #teardown ⇒ Object
- #transform_message(message) ⇒ Object
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, = {}) self.name = name self. = self.class::DEFAULT_OPTIONS.merge() self.endpoint = endpoint self.errors = [] end |
Instance Attribute Details
#endpoint ⇒ Object
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 |
#errors ⇒ Object
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 |
#name ⇒ Object
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 |
#options ⇒ Object
Returns the value of attribute options.
13 14 15 |
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 13 def @options end |
#result ⇒ Object
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(, ) to_transmit_payload((), , ) 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 [:backoff] backoff_arg = if [:backoff].respond_to?(:call) [:backoff] else DEFAULT_RETRYS[[:backoff]] end sleep_time = backoff_arg.call(retry_number) if sleep_time sleep(sleep_time * ([: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!(, ) payloads = .map do || _transmit_payload(, ) 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!(, = {}) retry_number = 0 begin self.result = transmit(_transmit_payload(, )) 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 [option].respond_to?(:call) [option].call(*args, &blk) else [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_retries ⇒ Object
95 96 97 |
# File 'lib/hermes_messenger_of_the_gods/endpoints/base.rb', line 95 def max_retries [: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 |
#teardown ⇒ Object
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 () transformer_name = "to_#{self.class.to_s.demodulize.underscore}_message" if [:transformer] if [:transformer].respond_to?(:call) [:transformer].call() else .send([:transformer]) end elsif .respond_to?(transformer_name) .public_send(transformer_name) elsif .respond_to?(:_build_for_transmission) ._build_for_transmission else end end |