From 889de6651b36d3eba10ac4afd6ea40eaa1aa84b5 Mon Sep 17 00:00:00 2001 From: Tom Werner Date: Tue, 18 Sep 2007 18:27:25 -0700 Subject: [PATCH] add http_response_code condition --- History.txt | 3 ++ Manifest.txt | 1 + bin/god | 18 +++++++-- lib/god.rb | 1 + lib/god/conditions/http_response_code.rb | 65 ++++++++++++++++++++++++++++++++ lib/god/server.rb | 2 +- test/test_http_response_code.rb | 5 +++ 7 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 lib/god/conditions/http_response_code.rb create mode 100644 test/test_http_response_code.rb diff --git a/History.txt b/History.txt index 07de1af..2f0333a 100644 --- a/History.txt +++ b/History.txt @@ -5,6 +5,7 @@ * Add TriggerCondition for conditions that need info about state changes * Implement notification system * Add Tasks (a generalization of Watches) to do non-process related tasks + * Add example init.d file in GOD_INSTALL_DIR/init/god * Minor Enchancements * Allow EventConditions to do transition overloading * Report errors during god startup instead of failing silently @@ -12,6 +13,8 @@ * Better usage info for `god --help` * Report usage when just `god` is run * Explain what's going on when attempting to rebind to an in-use port + * Add -b option to god binary to auto-bind to an unused port + * Add `god quit` to stop god without stopping any tasks * New Conditions * Flapping < TriggerCondition - trigger on state change * New Contacts diff --git a/Manifest.txt b/Manifest.txt index 8500c12..25d9920 100644 --- a/Manifest.txt +++ b/Manifest.txt @@ -8,6 +8,7 @@ examples/gravatar.god ext/god/extconf.rb ext/god/kqueue_handler.c ext/god/netlink_handler.c +init/god lib/god.rb lib/god/behavior.rb lib/god/behaviors/clean_pid_file.rb diff --git a/bin/god b/bin/god index bcc171d..293146d 100755 --- a/bin/god +++ b/bin/god @@ -12,7 +12,7 @@ opts = OptionParser.new do |opts| opts.banner = <<-EOF Usage: Starting: - god -c [-p ] [-P ] [-l ] [-D] + god -c [-p | -b] [-P ] [-l ] [-D] Querying: god [-p ] @@ -29,7 +29,8 @@ Usage: load load a config into a running god log show realtime log for given task status show status of each task - terminate kill god and all tasks + quit stop god + terminate stop god and all tasks Options: EOF @@ -38,10 +39,14 @@ EOF options[:config] = x end - opts.on("-pPORT", "--port PORT", "Communications port") do |x| + opts.on("-pPORT", "--port PORT", "Communications port (default 17165)") do |x| options[:port] = x end + opts.on("-b", "--auto-bind", "Auto-bind to an unused port number") do + options[:port] = "0" + end + opts.on("-PFILE", "--pid FILE", "Where to write the PID file") do |x| options[:pid] = x end @@ -142,6 +147,13 @@ elsif command = ARGV[0] puts e.message puts e.backtrace.join("\n") end + elsif command == 'quit' + begin + server.terminate + abort 'Could not stop god' + rescue DRb::DRbConnError + puts 'Stopped god' + end elsif command == 'terminate' begin t = Thread.new { loop { STDOUT.print('.'); STDOUT.flush; sleep(1) } } diff --git a/lib/god.rb b/lib/god.rb index 498439c..6310a06 100644 --- a/lib/god.rb +++ b/lib/god.rb @@ -31,6 +31,7 @@ require 'god/conditions/always' require 'god/conditions/lambda' require 'god/conditions/degrading_lambda' require 'god/conditions/flapping' +require 'god/conditions/http_response_code' require 'god/contact' require 'god/contacts/email' diff --git a/lib/god/conditions/http_response_code.rb b/lib/god/conditions/http_response_code.rb new file mode 100644 index 0000000..95d677b --- /dev/null +++ b/lib/god/conditions/http_response_code.rb @@ -0,0 +1,65 @@ +require 'net/http' + +module God + module Conditions + + class HttpResponseCode < PollCondition + attr_accessor :code_is, :code_is_not, :times, :host, :port, :timeout, :path + + def initialize + super + self.times = [1, 1] + end + + def prepare + self.code_is = Array(self.code) if self.code_is + self.code_is_not = Array(self.code_is_not) if self.code_is_not + + if self.times.kind_of?(Integer) + self.times = [self.times, self.times] + end + + @timeline = Timeline.new(self.times[1]) + end + + def valid? + valid = true + valid &= complain("Attribute 'host' must be specified", self) if self.host.nil? + valid &= complain("Attribute 'port' must be specified", self) if self.port.nil? + valid &= complain("Attribute 'path' must be specified", self) if self.path.nil? + 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 &= complain("Attribute 'timeout' must be specified", self) if self.timeout.nil? + valid + end + + def test + response = nil + + Net::HTTP.start(self.host, self.port) do |http| + http.read_timeout = self.timeout + response = http.head(self.path) + end + + if self.code_is && self.code_is.include?(response.code) + pass + elsif self.code_is_not && !self.code.include?(response.code) + pass + else + false + end + rescue Timeout::Error + self.code_is ? false : pass + end + + private + + def pass + @timeline.clear + return true + end + + end + + end +end \ No newline at end of file diff --git a/lib/god/server.rb b/lib/god/server.rb index e65302e..2cb3332 100644 --- a/lib/god/server.rb +++ b/lib/god/server.rb @@ -12,7 +12,6 @@ module God @host = host @port = port @acl = %w{deny all} + allow.inject([]) { |acc, a| acc + ['allow', a] } - puts "Starting on #{@host}:#{@port}" start end @@ -32,6 +31,7 @@ module God begin @drb ||= DRb.start_service("druby://#{@host}:#{@port}", self) + puts "Started on #{DRb.uri}" rescue Errno::EADDRINUSE DRb.start_service server = DRbObject.new nil, "druby://127.0.0.1:#{@port}" diff --git a/test/test_http_response_code.rb b/test/test_http_response_code.rb new file mode 100644 index 0000000..77ba0e2 --- /dev/null +++ b/test/test_http_response_code.rb @@ -0,0 +1,5 @@ +require File.dirname(__FILE__) + '/helper' + +class TestHttpResponseCode < Test::Unit::TestCase + +end \ No newline at end of file -- 2.11.4.GIT