Class: Rhod::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/rhod/command.rb

Constant Summary collapse

EXCEPTIONS =
[Exception, StandardError]

Class Method Summary collapse

Instance Method Summary collapse

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_requestObject



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_requestObject



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_errorObject



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_failureObject



76
77
78
# File 'lib/rhod/command.rb', line 76

def call_middleware_on_failure
  @env = @env[:middleware].on(:failure, @env)
end

#executeObject

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.message}.  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