lib/dante/runner.rb in dante-0.1.2 vs lib/dante/runner.rb in dante-0.1.3

- old
+ new

@@ -29,10 +29,11 @@ @name = name @startup_command = block @options = { :host => '0.0.0.0', :pid_path => "/var/run/#{@name}.pid", + :log_path => false, :debug => true }.merge(defaults) end # Accepts options for the process @@ -67,13 +68,11 @@ exit if fork Process.setsid exit if fork store_pid(Process.pid) File.umask 0000 - STDIN.reopen "/dev/null" - STDOUT.reopen "/dev/null", "a" - STDERR.reopen STDOUT + redirect_output! start end # Ensure process is running if until_true(MAX_START_TRIES) { self.daemon_running? } log "Daemon has started successfully" @@ -156,10 +155,14 @@ opts.on("-d", "--daemon", "Daemonize mode") do |v| options[:daemonize] = v end + opts.on("-l", "--log FILE", String, "Logfile for output") do |v| + options[:log_path] = v + end + opts.on("-k", "--kill [PORT]", String, "Kill specified running daemons - leave blank to kill all.") do |v| options[:kill] = v end opts.on("-u", "--user USER", String, "User to run as") do |user| @@ -195,9 +198,26 @@ log "Stopped PID: #{pid} at #{f}" rescue => e log "Failed to stop! #{k}: #{e}" end end + end + + # Redirect output based on log settings (reopens stdout/stderr to specified logfile) + # If log_path is nil, redirect to /dev/null to quiet output + def redirect_output! + if log_path = options[:log_path] + FileUtils.touch log_path + File.open(log_path, 'a') do |f| + $stdout.reopen(f) + $stderr.reopen(f) + end + else # redirect to /dev/null + STDIN.reopen "/dev/null" + STDOUT.reopen "/dev/null", "a" + STDERR.reopen STDOUT + end + log_path = options[:log_path] ? options[:log_path] : "/dev/null" end # Runs until the block condition is met or the timeout_seconds is exceeded # until_true(10) { ...return_condition... } def until_true(timeout_seconds, interval=1, &block) \ No newline at end of file