Class: God::Conditions::SimpleResponse

Inherits:
PollCondition show all
Defined in:
lib/god/conditions/simple_response.rb

Overview

Condition Symbol :simple_response Type: Poll

Trigger based on the response data

Paramaters

 Required
   +host+ is the hostname to connect [required]
   --one of code_is or code_is_not--
   +data_is+ trigger if the response code IS
   +data_is_not+ trigger if the response code IS NOT
   +data_contains+ trigger if the data CONTAINS
   +data_without+ trigger if the data DOES NOT CONTAIN
Optional
   +port+ is the port to connect (default 80)
   +type+ either tcp or udp
   +times+ is the number of times after which to trigger (default 1)
           e.g. 3 (times in a row) or [3, 5] (three out of fives times)
   +timeout+ is the time to wait for a connection (default 60.seconds)

Examples

Trigger if the response code from www.example.com is not “foo” (or if the connection is refused or times out:

on.condition(:simple_response) do |c|
  c.host = 'www.example.com'
  c.data_is_not = "foo"
end

Instance Attribute Summary collapse

Attributes inherited from PollCondition

#interval

Attributes inherited from God::Condition

#info, #notify, #phase, #transition

Attributes inherited from Behavior

#watch

Instance Method Summary collapse

Methods inherited from PollCondition

#after, #before

Methods inherited from God::Condition

#friendly_name, generate, valid?

Methods inherited from Behavior

#after_restart, #after_start, #after_stop, #before_restart, #before_start, #before_stop, #friendly_name, generate

Methods included from God::Configurable

#base_name, complain, #complain, #friendly_name

Constructor Details

#initializeSimpleResponse

Returns a new instance of SimpleResponse.



44
45
46
47
48
49
# File 'lib/god/conditions/simple_response.rb', line 44

def initialize
  super
        self.type = 'udp'
  self.times = [1, 1]
  self.timeout = 10.seconds
end

Instance Attribute Details

#data_containsObject

Returns the value of attribute data_contains.



34
35
36
# File 'lib/god/conditions/simple_response.rb', line 34

def data_contains
  @data_contains
end

#data_isObject

Returns the value of attribute data_is.



34
35
36
# File 'lib/god/conditions/simple_response.rb', line 34

def data_is
  @data_is
end

#data_is_notObject

Returns the value of attribute data_is_not.



34
35
36
# File 'lib/god/conditions/simple_response.rb', line 34

def data_is_not
  @data_is_not
end

#data_withoutObject

Returns the value of attribute data_without.



34
35
36
# File 'lib/god/conditions/simple_response.rb', line 34

def data_without
  @data_without
end

#hostObject

Returns the value of attribute host.



34
35
36
# File 'lib/god/conditions/simple_response.rb', line 34

def host
  @host
end

#portObject

Returns the value of attribute port.



34
35
36
# File 'lib/god/conditions/simple_response.rb', line 34

def port
  @port
end

#timeoutObject

Returns the value of attribute timeout.



34
35
36
# File 'lib/god/conditions/simple_response.rb', line 34

def timeout
  @timeout
end

#timesObject

Returns the value of attribute times.



34
35
36
# File 'lib/god/conditions/simple_response.rb', line 34

def times
  @times
end

#typeObject

Returns the value of attribute type.



34
35
36
# File 'lib/god/conditions/simple_response.rb', line 34

def type
  @type
end

Instance Method Details

#prepareObject



51
52
53
54
55
56
57
58
# File 'lib/god/conditions/simple_response.rb', line 51

def prepare
  if self.times.kind_of?(Integer)
    self.times = [self.times, self.times]
  end
  
  @timeline = Timeline.new(self.times[1])
  @history = Timeline.new(self.times[1])
end

#resetObject



60
61
62
63
# File 'lib/god/conditions/simple_response.rb', line 60

def reset
  @timeline.clear
  @history.clear
end

#testObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/god/conditions/simple_response.rb', line 74

def test
  response = nil
  
        if self.type == 'tcp'
          sock = TCPSocket.new(self.host, self.port)
        else
          sock = UDPSocket.new()
          sock.connect(self.host, self.port)
        end

        response = sock.readlines.join(' ')


  if self.data_is && self.data_is.include?(response)
    pass(response)
  elsif self.data_contains && response =~ /#{self.data_contains}/
    pass(response)
  elsif self.data_is_not && self.data_is_not.include?(response)
    fail(response)
  elsif self.data_without && response =~ /#{self.data_without}/
    fail(response)
  else
    pass(response)
  end
rescue Errno::ECONNREFUSED
  fail('Refused')
rescue Errno::ECONNRESET
  fail('Reset')
rescue EOFError
  fail('EOF')
rescue Timeout::Error
  fail('Timeout')
rescue Errno::ETIMEDOUT
  fail('Timedout')
rescue Exception => failure
  fail(failure.class.name)
end

#valid?Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
# File 'lib/god/conditions/simple_response.rb', line 65

def valid?
  valid = true
  valid &= complain("Attribute 'type' must be either 'tcp' or 'udp'", self) if (self.type != 'tcp' && self.type != 'udp')
  valid &= complain("Attribute 'host' must be specified", self) if self.host.nil?
  valid &= complain("One (and only one) of attributes 'data_is', 'data_is_not', 'data_contains', and 'data_without' must be specified", self) if
    (self.data_is.nil? && self.data_contains.nil? && self.data_is_not.nil? && self.data_without.nil?) || (self.data_is && self.data_contains) || (self.data_is && self.data_is_not) || (self.data_is && self.data_without) || (self.data_contains && self.data_is_not) || (self.data_contains && self.data_without) || (self.data_is_not && self.data_without)
  valid
end