Class: God::Conditions::HttpResponseCode
- Inherits:
-
PollCondition
- Object
- Behavior
- God::Condition
- PollCondition
- God::Conditions::HttpResponseCode
- Includes:
- ConditionHelper
- Defined in:
- lib/god/conditions/http_response_code.rb
Overview
Condition Symbol :http_response_code Type: Poll
Trigger based on the response from an HTTP request.
Paramaters
Required
+host+ is the hostname to connect [required]
--one of code_is or code_is_not--
+code_is+ trigger if the response code IS one of these
e.g. 500 or '500' or [404, 500] or %w{404 500}
+code_is_not+ trigger if the response code IS NOT one of these
e.g. 200 or '200' or [200, 302] or %w{200 302}
Optional
+port+ is the port to connect (default 80)
+path+ is the path to connect (default '/')
+headers+ is the hash of HTTP headers to send (default none)
+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/foo/bar is not a 200 (or if the connection is refused or times out:
on.condition(:http_response_code) do |c|
c.host = 'www.example.com'
c.path = '/foo/bar'
c.code_is_not = 200
end
Trigger if the response code is a 404 or a 500 (will not be triggered by a connection refusal or timeout):
on.condition(:http_response_code) do |c|
c.host = 'www.example.com'
c.path = '/foo/bar'
c.code_is = [404, 500]
end
Trigger if the response code is not a 200 five times in a row:
on.condition(:http_response_code) do |c|
c.host = 'www.example.com'
c.path = '/foo/bar'
c.code_is_not = 200
c.times = 5
end
Trigger if the response code is not a 200 or does not respond within 10 seconds:
on.condition(:http_response_code) do |c|
c.host = 'www.example.com'
c.path = '/foo/bar'
c.code_is_not = 200
c.timeout = 10
end
Instance Attribute Summary collapse
-
#code_is ⇒ Object
e.g.
-
#code_is_not ⇒ Object
e.g.
-
#data ⇒ Object
e.g.
-
#headers ⇒ Object
e.g.
-
#host ⇒ Object
e.g.
-
#method ⇒ Object
e.g.
-
#path ⇒ Object
e.g.
-
#port ⇒ Object
e.g.
-
#timeout ⇒ Object
e.g.
-
#times ⇒ Object
e.g.
-
#trigger_on ⇒ Object
e.g.
Attributes inherited from PollCondition
Attributes inherited from God::Condition
#info, #notify, #phase, #transition
Attributes inherited from Behavior
Instance Method Summary collapse
-
#initialize ⇒ HttpResponseCode
constructor
A new instance of HttpResponseCode.
- #prepare ⇒ Object
- #reset ⇒ Object
- #test ⇒ Object
- #valid? ⇒ Boolean
Methods included from ConditionHelper
Methods inherited from PollCondition
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
#initialize ⇒ HttpResponseCode
Returns a new instance of HttpResponseCode.
79 80 81 82 83 84 85 86 87 |
# File 'lib/god/conditions/http_response_code.rb', line 79 def initialize super self.port = 80 self.path = '/' self.headers = {} self.times = [3, 5] self.timeout = 60.seconds self.method = :get end |
Instance Attribute Details
#code_is ⇒ Object
e.g. 500 or ‘500’ or [404, 500] or %w500
67 68 69 |
# File 'lib/god/conditions/http_response_code.rb', line 67 def code_is @code_is end |
#code_is_not ⇒ Object
e.g. 500 or ‘500’ or [404, 500] or %w500
67 68 69 |
# File 'lib/god/conditions/http_response_code.rb', line 67 def code_is_not @code_is_not end |
#data ⇒ Object
e.g. 500 or ‘500’ or [404, 500] or %w500
67 68 69 |
# File 'lib/god/conditions/http_response_code.rb', line 67 def data @data end |
#headers ⇒ Object
e.g. 500 or ‘500’ or [404, 500] or %w500
67 68 69 |
# File 'lib/god/conditions/http_response_code.rb', line 67 def headers @headers end |
#host ⇒ Object
e.g. 500 or ‘500’ or [404, 500] or %w500
67 68 69 |
# File 'lib/god/conditions/http_response_code.rb', line 67 def host @host end |
#method ⇒ Object
e.g. 500 or ‘500’ or [404, 500] or %w500
67 68 69 |
# File 'lib/god/conditions/http_response_code.rb', line 67 def method @method end |
#path ⇒ Object
e.g. 500 or ‘500’ or [404, 500] or %w500
67 68 69 |
# File 'lib/god/conditions/http_response_code.rb', line 67 def path @path end |
#port ⇒ Object
e.g. 500 or ‘500’ or [404, 500] or %w500
67 68 69 |
# File 'lib/god/conditions/http_response_code.rb', line 67 def port @port end |
#timeout ⇒ Object
e.g. 500 or ‘500’ or [404, 500] or %w500
67 68 69 |
# File 'lib/god/conditions/http_response_code.rb', line 67 def timeout @timeout end |
#times ⇒ Object
e.g. 500 or ‘500’ or [404, 500] or %w500
67 68 69 |
# File 'lib/god/conditions/http_response_code.rb', line 67 def times @times end |
#trigger_on ⇒ Object
e.g. 500 or ‘500’ or [404, 500] or %w500
67 68 69 |
# File 'lib/god/conditions/http_response_code.rb', line 67 def trigger_on @trigger_on end |
Instance Method Details
#prepare ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/god/conditions/http_response_code.rb', line 89 def prepare self.code_is = Array(self.code_is).map { |x| x.to_i } if self.code_is self.code_is_not = Array(self.code_is_not).map { |x| x.to_i } if self.code_is_not 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 |
#reset ⇒ Object
101 102 103 104 |
# File 'lib/god/conditions/http_response_code.rb', line 101 def reset @timeline.clear @history.clear end |
#test ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/god/conditions/http_response_code.rb', line 116 def test response = nil Net::HTTP.start(self.host, self.port) do |http| http.read_timeout = self.timeout if self.method == :get response = http.get(self.path, self.headers) else self.method == :post req = Net::HTTP::Post.new(self.path, self.headers) req.set_form_data(self.data, ';') if self.data response = http.request(req) end end # require 'ruby-debug'; debugger actual_response_code = response.code.to_i if self.code_is && self.code_is.include?(actual_response_code) pass(actual_response_code) elsif self.code_is_not && !self.code_is_not.include?(actual_response_code) pass(actual_response_code) else fail(actual_response_code) end rescue Errno::ECONNREFUSED self.code_is ? fail('Refused') : pass('Refused') rescue Errno::ECONNRESET self.code_is ? fail('Reset') : pass('Reset') rescue EOFError self.code_is ? fail('EOF') : pass('EOF') rescue Timeout::Error self.code_is ? fail('Timeout') : pass('Timeout') rescue Errno::ETIMEDOUT self.code_is ? fail('Timedout') : pass('Timedout') rescue Exception => failure self.code_is ? fail(failure.class.name) : pass(failure.class.name) end |
#valid? ⇒ Boolean
106 107 108 109 110 111 112 113 |
# File 'lib/god/conditions/http_response_code.rb', line 106 def valid? valid = true valid &= complain("Attribute 'host' must be specified", self) if self.host.nil? valid &= complain("Attribute 'method' must be in [:get, :post]", self) if ![:get, :post].include?(self.method) valid &= complain("One (and only one) of attributes 'code_is' and 'code_is_not' must be specified", self) if (self.code_is.nil? && self.code_is_not.nil?) || (self.code_is && self.code_is_not) valid end |