Class: Rhod::Command
- Inherits:
-
Object
- Object
- Rhod::Command
- Defined in:
- lib/rhod/command.rb
Constant Summary collapse
- EXCEPTIONS =
[Exception, StandardError]
Class Method Summary collapse
-
.execute(*args, &block) ⇒ Object
Class methods.
Instance Method Summary collapse
-
#execute ⇒ Object
Instance methods.
-
#initialize(*args, &block) ⇒ Command
constructor
A new instance of Command.
Constructor Details
#initialize(*args, &block) ⇒ Command
Returns a new instance of Command.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rhod/command.rb', line 5 def initialize(*args, &block) opts = args[-1].kind_of?(Hash) ? args.pop : {} @args = args || [] @request = block @retries = opts[:retries] || 0 @attempts = 0 @logger = opts[:logger] @backoffs = Rhod::Backoffs.backoff_sugar_to_enumerator(opts[:backoffs]) @backoffs ||= Rhod::Backoffs::Logarithmic.new(1.3) @fallback = opts[:fallback] @pool = opts[:pool] @exceptions = opts[:exceptions] || EXCEPTIONS end |
Class Method Details
.execute(*args, &block) ⇒ Object
Class methods
28 29 30 31 |
# File 'lib/rhod/command.rb', line 28 def self.execute(*args, &block) this = self.new(*args, &block) this.execute end |
Instance Method Details
#execute ⇒ Object
Instance methods
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rhod/command.rb', line 35 def execute begin if @pool @pool.with do |conn| @args = [conn].concat(@args) @request.call(*@args) end else @request.call(*@args) end rescue *@exceptions => e @attempts += 1 @next_attempt = @backoffs.next if @attempts <= @retries @logger.warn("Rhod - Caught an exception: #{e.}. Attempt #{@attempts} in #{sprintf("%.2f", @next_attempt)} secs") if @logger && @logger.respond_to?(:warn) sleep(@next_attempt) retry else return @fallback.call(*@args) if @fallback raise end end end |