From: tom Date: Fri, 4 Jan 2008 23:36:55 +0000 (-0800) Subject: add --log-level to cli tool X-Git-Url: https://repo.or.cz/w/god.git/commitdiff_plain/d4b77cc7127fe97923092dc0359b1c1ab3ab84b8 add --log-level to cli tool --- diff --git a/bin/god b/bin/god index 4f73aef..57b86d8 100755 --- a/bin/god +++ b/bin/god @@ -9,7 +9,7 @@ require 'optparse' require 'drb' begin - options = {:daemonize => true, :port => 17165} + options = {:daemonize => true, :port => 17165, :log_level => "info"} opts = OptionParser.new do |opts| opts.banner = <<-EOF @@ -71,6 +71,10 @@ begin opts.on("-V", "Print extended version and build information") do options[:info] = true end + + opts.on("--log-level LEVEL", "Log level [debug|info|fatal]") do |x| + options[:log_level] = x.to_sym + end end opts.parse! diff --git a/lib/god.rb b/lib/god.rb index b4cdd69..075ffb3 100644 --- a/lib/god.rb +++ b/lib/god.rb @@ -138,6 +138,7 @@ module God PID_FILE_DIRECTORY_DEFAULT = '/var/run/god' DRB_PORT_DEFAULT = 17165 DRB_ALLOW_DEFAULT = ['127.0.0.1'] + LOG_LEVEL_DEFAULT = :info class << self # user configurable @@ -146,7 +147,8 @@ module God :port, :allow, :log_buffer_size, - :pid_file_directory + :pid_file_directory, + :log_level # internal attr_accessor :inited, @@ -167,6 +169,7 @@ module God self.allow = nil self.log_buffer_size = nil self.pid_file_directory = nil + self.log_level = nil # Initialize internal data. # @@ -188,7 +191,13 @@ module God self.pid_file_directory ||= PID_FILE_DIRECTORY_DEFAULT self.port ||= DRB_PORT_DEFAULT self.allow ||= DRB_ALLOW_DEFAULT - LOG.level = Logger::INFO + self.log_level ||= LOG_LEVEL_DEFAULT + + # log level + log_level_map = {:debug => Logger::DEBUG, + :info => Logger::INFO, + :fatal => Logger::FATAL} + LOG.level = log_level_map[self.log_level] # init has been executed self.inited = true diff --git a/lib/god/cli/run.rb b/lib/god/cli/run.rb index f9b93ad..4c640b1 100644 --- a/lib/god/cli/run.rb +++ b/lib/god/cli/run.rb @@ -50,6 +50,11 @@ module God God.pid = @options[:pid] end + # set log level if requested + if @options[:log_level] + God.log_level = @options[:log_level] + end + # load config if @options[:config] unless File.exist?(@options[:config]) @@ -96,6 +101,11 @@ module God God.port = @options[:port] end + # set log level if requested + if @options[:log_level] + God.log_level = @options[:log_level] + end + if @options[:config] unless File.exist?(@options[:config]) abort "File not found: #{@options[:config]}" diff --git a/lib/god/conditions/cpu_usage.rb b/lib/god/conditions/cpu_usage.rb index 3057502..facb85f 100644 --- a/lib/god/conditions/cpu_usage.rb +++ b/lib/god/conditions/cpu_usage.rb @@ -61,8 +61,7 @@ module God end def test - pid = self.watch.pid - process = System::Process.new(pid) + process = System::Process.new(self.pid) @timeline.push(process.percent_cpu) history = "[" + @timeline.map { |x| "#{x > self.above ? '*' : ''}#{x}%%" }.join(", ") + "]" diff --git a/lib/god/conditions/http_response_code.rb b/lib/god/conditions/http_response_code.rb index 8184153..a4e4dbd 100644 --- a/lib/god/conditions/http_response_code.rb +++ b/lib/god/conditions/http_response_code.rb @@ -121,6 +121,8 @@ module God end rescue Errno::ECONNREFUSED self.code_is ? fail('Refused') : pass('Refused') + rescue EOFError + self.code_is ? fail('EOF') : pass('EOF') rescue Timeout::Error self.code_is ? fail('Timeout') : pass('Timeout') end diff --git a/lib/god/conditions/memory_usage.rb b/lib/god/conditions/memory_usage.rb index b5f8075..4a2903f 100644 --- a/lib/god/conditions/memory_usage.rb +++ b/lib/god/conditions/memory_usage.rb @@ -63,8 +63,7 @@ module God end def test - pid = self.pid - process = System::Process.new(pid) + process = System::Process.new(self.pid) @timeline.push(process.memory) history = "[" + @timeline.map { |x| "#{x > self.above ? '*' : ''}#{x}kb" }.join(", ") + "]" diff --git a/lib/god/process.rb b/lib/god/process.rb index 45f2811..974afcc 100644 --- a/lib/god/process.rb +++ b/lib/god/process.rb @@ -125,6 +125,11 @@ module God @pid_file ||= default_pid_file end + # Fetch the PID from pid_file. If the pid_file does not + # exist, then use the PID from the last time it was read. + # If it has never been read, then return nil. + # + # Returns Integer(pid) or nil def pid contents = File.read(self.pid_file).strip rescue '' real_pid = contents =~ /^\d+$/ ? contents.to_i : nil diff --git a/lib/god/timer.rb b/lib/god/timer.rb index a46d4dd..e033625 100644 --- a/lib/god/timer.rb +++ b/lib/god/timer.rb @@ -94,6 +94,7 @@ module God def schedule(condition, delay = condition.interval) @mutex.synchronize do unless @conditions.include?(condition) + applog(nil, :debug, "timer schedule #{condition} in #{delay}") @events << TimerEvent.new(condition, delay) @conditions << condition @events.sort! { |x, y| x.at <=> y.at } @@ -117,6 +118,7 @@ module God # # Returns nothing def trigger(event) + applog(nil, :debug, "timer trigger #{event}") Hub.trigger(event.condition, event.phase) end diff --git a/test/configs/stress/stress.god b/test/configs/stress/stress.god index acb2ad7..5420fbe 100644 --- a/test/configs/stress/stress.god +++ b/test/configs/stress/stress.god @@ -1,4 +1,4 @@ -('01'..'20').each do |i| +('01'..'08').each do |i| God.watch do |w| w.name = "stress-#{i}" w.start = "ruby " + File.join(File.dirname(__FILE__), *%w[simple_server.rb]) diff --git a/test/configs/test.rb b/test/configs/test.rb index 81c2394..00cdcb2 100644 --- a/test/configs/test.rb +++ b/test/configs/test.rb @@ -1,46 +1,21 @@ -if $0 == __FILE__ - require File.join(File.dirname(__FILE__), *%w[.. .. lib god]) -end - ENV['GOD_TEST_RAILS_ROOT'] || abort("Set a rails root for testing in an environment variable called GOD_TEST_RAILS_ROOT") RAILS_ROOT = ENV['GOD_TEST_RAILS_ROOT'] -God.init do |g| - # g.host = - # g.port = 7777 - # g.pid_file_directory = -end - -class SimpleNotifier - def self.notify(str) - puts "Notifying: #{str}" - end -end +port = 5000 God.watch do |w| - w.name = "local-3000" + w.name = "local-#{port}" w.interval = 5.seconds - w.start = "mongrel_rails start -P ./log/mongrel.pid -c #{RAILS_ROOT} -p 3001 -d" + w.start = "mongrel_rails start -P ./log/mongrel.pid -c #{RAILS_ROOT} -p #{port} -d" w.restart = "mongrel_rails restart -P ./log/mongrel.pid -c #{RAILS_ROOT}" w.stop = "mongrel_rails stop -P ./log/mongrel.pid -c #{RAILS_ROOT}" - w.restart_grace = 5.seconds - w.stop_grace = 5.seconds - w.autostart = true - w.uid = 'kev' - w.gid = 'kev' w.group = 'mongrels' w.pid_file = File.join(RAILS_ROOT, "log/mongrel.pid") # clean pid files before start if necessary w.behavior(:clean_pid_file) - w.behavior(:notify_when_flapping) do |b| - b.failures = 5 - b.seconds = 60.seconds - b.notifier = SimpleNotifier - end - # determine the state on startup w.transition(:init, { true => :up, false => :start }) do |on| on.condition(:process_running) do |c| @@ -73,5 +48,14 @@ God.watch do |w| c.above = 10.percent c.times = [3, 5] end + + on.condition(:http_response_code) do |c| + c.host = 'localhost' + c.port = port + c.path = '/' + c.code_is_not = 200 + c.timeout = 10.seconds + c.times = [3, 5] + end end end \ No newline at end of file