lib/god/cli/run.rb in resurrected_god-0.14.0 vs lib/god/cli/run.rb in resurrected_god-1.0.0
- old
+ new
@@ -1,8 +1,7 @@
module God
module CLI
-
class Run
def initialize(options)
@options = options
dispatch
@@ -10,13 +9,11 @@
def dispatch
# have at_exit start god
$run = true
- if @options[:syslog]
- require 'god/sys_logger'
- end
+ require 'god/sys_logger' if @options[:syslog]
# run
if @options[:daemonize]
run_daemonized
else
@@ -40,33 +37,25 @@
def default_run
# make sure we have STDIN/STDOUT redirected immediately
setup_logging
# start attached pid watcher if necessary
- if @options[:attach]
- self.attach
- end
+ attach if @options[:attach]
- if @options[:port]
- God.port = @options[:port]
- end
+ God.port = @options[:port] if @options[:port]
- if @options[:events]
- God::EventHandler.load
- end
+ God::EventHandler.load if @options[:events]
# set log level, defaults to WARN
- if @options[:log_level]
- God.log_level = @options[:log_level]
- else
- God.log_level = @options[:daemonize] ? :warn : :info
- end
+ God.log_level = if @options[:log_level]
+ @options[:log_level]
+ else
+ @options[:daemonize] ? :warn : :info
+ end
if @options[:config]
- if !@options[:config].include?('*') && !File.exist?(@options[:config])
- abort "File not found: #{@options[:config]}"
- end
+ abort "File not found: #{@options[:config]}" if !@options[:config].include?('*') && !File.exist?(@options[:config])
# start the event handler
God::EventHandler.start if God::EventHandler.loaded?
load_config @options[:config]
@@ -80,91 +69,78 @@
default_run
end
def run_daemonized
# trap and ignore SIGHUP
- Signal.trap('HUP') {}
+ Signal.trap('HUP') {} # block must be passed
# trap and log-reopen SIGUSR1
Signal.trap('USR1') { setup_logging }
pid = fork do
- begin
- require 'god'
+ require 'god'
- # set pid if requested
- if @options[:pid] # and as deamon
- God.pid = @options[:pid]
- end
+ # set pid if requested
+ God.pid = @options[:pid] if @options[:pid] # and as daemon
- default_run
+ default_run
- unless God::EventHandler.loaded?
- puts
- puts "***********************************************************************"
- puts "*"
- puts "* Event conditions are not available for your installation of god."
- puts "* You may still use and write custom conditions using the poll system"
- puts "*"
- puts "***********************************************************************"
- puts
- end
-
- rescue => e
- puts e.message
- puts e.backtrace.join("\n")
- abort "There was a fatal system error while starting god (see above)"
+ unless God::EventHandler.loaded?
+ puts
+ puts '***********************************************************************'
+ puts '*'
+ puts '* Event conditions are not available for your installation of god.'
+ puts '* You may still use and write custom conditions using the poll system'
+ puts '*'
+ puts '***********************************************************************'
+ puts
end
+ rescue => e
+ puts e.message
+ puts e.backtrace.join("\n")
+ abort 'There was a fatal system error while starting god (see above)'
end
- if @options[:pid]
- File.open(@options[:pid], 'w') { |f| f.write pid }
- end
+ File.write(@options[:pid], pid) if @options[:pid]
::Process.detach pid
exit
end
def setup_logging
log_file = God.log_file
log_file = File.expand_path(@options[:log]) if @options[:log]
- log_file = "/dev/null" if !log_file && @options[:daemonize]
- if log_file
- puts "Sending output to log file: #{log_file}" unless @options[:daemonize]
+ log_file = '/dev/null' if !log_file && @options[:daemonize]
+ return unless log_file
- # reset file descriptors
- STDIN.reopen "/dev/null"
- STDOUT.reopen(log_file, "a")
- STDERR.reopen STDOUT
- STDOUT.sync = true
- end
+ puts "Sending output to log file: #{log_file}" unless @options[:daemonize]
+
+ # reset file descriptors
+ $stdin.reopen '/dev/null'
+ $stdout.reopen(log_file, 'a')
+ $stderr.reopen $stdout
+ $stdout.sync = true
end
def load_config(config)
files = File.directory?(config) ? Dir['**/*.god'] : Dir[config]
- abort "No files could be found" if files.empty?
+ abort 'No files could be found' if files.empty?
files.each do |god_file|
- unless load_god_file(god_file)
- abort "File '#{god_file}' could not be loaded"
- end
+ abort "File '#{god_file}' could not be loaded" unless load_god_file(god_file)
end
end
def load_god_file(god_file)
applog(nil, :info, "Loading #{god_file}")
load File.expand_path(god_file)
true
rescue Exception => e
- if e.instance_of?(SystemExit)
- raise
- else
- puts "There was an error in #{god_file}"
- puts "\t" + e.message
- puts "\t" + e.backtrace.join("\n\t")
- false
- end
- end
+ raise if e.instance_of?(SystemExit)
- end # Run
-
+ puts "There was an error in #{god_file}"
+ puts "\t#{e.message}"
+ puts "\t#{e.backtrace.join("\n\t")}"
+ false
+ end
+ end
end
end