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
- #call_middleware_after_request ⇒ Object
- #call_middleware_before_request ⇒ Object
- #call_middleware_on_error ⇒ Object
- #call_middleware_on_failure ⇒ Object
-
#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 |
# File 'lib/rhod/command.rb', line 5 def initialize(*args, &block) opts = args[-1].kind_of?(Hash) ? args.pop : {} @env = { :args => args || [], :request => block, :retries => opts[:retries] || 0, :attempts => 0, :logger => opts[:logger], :backoffs => Rhod::Backoffs.backoff_sugar_to_enumerator(opts[:backoffs]) || Rhod::Backoffs::Logarithmic.new(1.3), :fallback => opts[:fallback], :pool => opts[:pool], :exceptions => opts[:exceptions] || EXCEPTIONS, :profile_name => opts[:profile_name], :middleware => opts[:middleware_stack] || Rhod::Middleware.new, } end |
Class Method Details
.execute(*args, &block) ⇒ Object
Class methods
24 25 26 27 |
# File 'lib/rhod/command.rb', line 24 def self.execute(*args, &block) this = self.new(*args, &block) this.execute end |
Instance Method Details
#call_middleware_after_request ⇒ Object
68 69 70 |
# File 'lib/rhod/command.rb', line 68 def call_middleware_after_request @env = @env[:middleware].on(:after, @env) end |
#call_middleware_before_request ⇒ Object
64 65 66 |
# File 'lib/rhod/command.rb', line 64 def call_middleware_before_request @env = @env[:middleware].on(:before, @env) end |
#call_middleware_on_error ⇒ Object
72 73 74 |
# File 'lib/rhod/command.rb', line 72 def call_middleware_on_error @env = @env[:middleware].on(:error, @env) end |
#call_middleware_on_failure ⇒ Object
76 77 78 |
# File 'lib/rhod/command.rb', line 76 def call_middleware_on_failure @env = @env[:middleware].on(:failure, @env) end |
#execute ⇒ Object
Instance methods
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rhod/command.rb', line 31 def execute begin if @env[:pool] @env[:pool].with do |conn| @env[:args] = [conn].concat(@env[:args]) call_middleware_before_request @env[:result] = @env[:request].call(*@env[:args]) call_middleware_after_request @env[:result] end else call_middleware_before_request @env[:result] = @env[:request].call(*@env[:args]) call_middleware_after_request @env[:result] end rescue *@env[:exceptions] => e @env[:attempts] += 1 @env[:next_attempt] = @env[:backoffs].next if @env[:attempts] <= @env[:retries] @env[:logger].warn("Rhod - Caught an exception: #{e.}. Attempt #{@env[:attempts]} in #{sprintf("%.2f", @env[:next_attempt])} secs") if @env[:logger] && @env[:logger].respond_to?(:warn) call_middleware_on_error sleep(@env[:next_attempt]) retry else call_middleware_on_failure return @env[:fallback].call(*@env[:args]) if @env[:fallback] raise end end end |