From 57c95a285cddb1d4f57384236fee8f7d938a9202 Mon Sep 17 00:00:00 2001 From: Tom Preston-Werner Date: Sat, 29 Sep 2007 00:38:03 -0700 Subject: [PATCH] remove redundant rescues in cli --- bin/god | 105 ++++++++++++++++++++++++++----------------------------------- lib/god.rb | 10 +++--- 2 files changed, 49 insertions(+), 66 deletions(-) diff --git a/bin/god b/bin/god index ed7b271..16dfe05 100755 --- a/bin/god +++ b/bin/god @@ -16,13 +16,13 @@ begin Usage: Starting: god -c [-p | -b] [-P ] [-l ] [-D] - + Querying: god [-p ] god [-p ] god -v god -V (must be run as root to be accurate on Linux) - + Commands: start start task or group restart restart task or group @@ -34,77 +34,73 @@ begin status show status of each task quit stop god terminate stop god and all tasks - + Options: EOF - + opts.on("-cCONFIG", "--config-file CONFIG", "Configuration file") do |x| options[:config] = x end - + 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 - + opts.on("-lFILE", "--log FILE", "Where to write the log file") do |x| options[:log] = x end - + opts.on("-D", "--no-daemonize", "Don't daemonize") do options[:daemonize] = false end - + opts.on("-v", "--version", "Print the version number and exit") do options[:version] = true end - + opts.on("-V", "Print extended version and build information") do options[:info] = true end end - + opts.parse! - + if options[:version] require 'god' - + # print version puts "Version #{God::VERSION}" exit elsif options[:info] require 'god' - + puts "Version: #{God::VERSION}" puts "Polls: enabled" puts "Events: " + God::EventHandler.event_system - + exit elsif command = ARGV[0] require 'god' - + # a command was specified - + # connect to remote drb DRb.start_service server = DRbObject.new nil, "druby://127.0.0.1:#{options[:port]}" - + begin server.ping rescue DRb::DRbConnError puts "The server is not available (or you do not have permissions to access it)" abort - rescue => e - puts e.message - puts e.backtrace.join("\n") - abort end if command == 'load' @@ -126,17 +122,15 @@ begin end end - puts errors unless errors.empty? + unless errors.empty? + puts errors + exit(1) + end elsif command == 'status' - begin - watches = server.status - watches.keys.sort.each do |name| - state = watches[name][:state] - puts "#{name}: #{state}" - end - rescue => e - puts e.message - puts e.backtrace.join("\n") + watches = server.status + watches.keys.sort.each do |name| + state = watches[name][:state] + puts "#{name}: #{state}" end elsif command == 'log' begin @@ -152,9 +146,6 @@ begin puts "No such watch" rescue DRb::DRbConnError puts "The server went away" - rescue => e - puts e.message - puts e.backtrace.join("\n") end elsif command == 'quit' begin @@ -164,20 +155,15 @@ begin puts 'Stopped god' end elsif command == 'terminate' - begin - t = Thread.new { loop { STDOUT.print('.'); STDOUT.flush; sleep(1) } } - if server.stop_all - t.kill; STDOUT.puts - puts 'Stopped all watches' - else - t.kill; STDOUT.puts - puts 'Could not stop all watches within 10 seconds' - end - rescue => e - puts e.message - puts e.backtrace.join("\n") + t = Thread.new { loop { STDOUT.print('.'); STDOUT.flush; sleep(1) } } + if server.stop_all + t.kill; STDOUT.puts + puts 'Stopped all watches' + else + t.kill; STDOUT.puts + puts 'Could not stop all watches within 10 seconds' end - + begin server.terminate abort 'Could not stop god' @@ -187,15 +173,15 @@ begin else # get the name of the watch/group name = ARGV[1] - + begin puts "Sending '#{command}' command" - + t = Thread.new { loop { sleep(1); STDOUT.print('.'); STDOUT.flush; sleep(1) } } - + # send command watches = server.control(name, command) - + # output response t.kill; STDOUT.puts puts 'The following watches were affected:' @@ -205,13 +191,8 @@ begin rescue God::InvalidCommandError t.kill rescue nil; STDOUT.puts abort "Command '#{command}' is not valid. Run 'god --help' for usage" - rescue => e - puts e.message - puts e.backtrace.join("\n") end end - - exit else # start god $run = true @@ -291,14 +272,16 @@ begin if options[:pid] File.open(options[:pid], 'w') { |f| f.write pid } end - + ::Process.detach pid - + exit end end rescue Exception => e - unless e.instance_of?(SystemExit) + if e.instance_of?(SystemExit) + raise + else puts e.message puts e.backtrace.join("\n") end diff --git a/lib/god.rb b/lib/god.rb index adfeb1a..23a8148 100644 --- a/lib/god.rb +++ b/lib/god.rb @@ -86,10 +86,10 @@ God::EventHandler.load module Kernel alias_method :abort_orig, :abort - def abort(text = '') + def abort(text = nil) $run = false - LOG.log(nil, :error, text) unless text.empty? - abort_orig(text) + LOG.log(nil, :error, text) if text + text ? abort_orig(text) : exit(1) end alias_method :exit_orig, :exit @@ -235,9 +235,9 @@ module God t.register! # log - if God.running + if self.running && existing_watch LOG.log(t, :info, "#{t.name} Reloaded config") - else + elsif self.running LOG.log(t, :info, "#{t.name} Loaded config") end end -- 2.11.4.GIT