lib/dante/runner.rb in dante-0.1.4 vs lib/dante/runner.rb in dante-0.1.5
- old
+ new
@@ -101,12 +101,13 @@
trap("INT") {
interrupt
exit
}
+
trap("TERM"){
- interrupt
+ log "Trying to stop #{@name}..."
exit
}
@startup_command.call(self.options) if @startup_command
end
@@ -126,16 +127,16 @@
self.stop
self.start
end
def interrupt
- # begin
+ if options[:debug]
raise Interrupt
- sleep(1)
- # rescue Interrupt
- # log "Interrupt received; stopping #{@name}"
- # end
+ sleep 1
+ else
+ log "Interrupt received; stopping #{@name}"
+ end
end
# Returns true if process is not running
def daemon_stopped?
! self.daemon_running?
@@ -209,11 +210,11 @@
def kill_pid(k='*')
Dir[options[:pid_path]].each do |f|
begin
pid = IO.read(f).chomp.to_i
FileUtils.rm f
- Process.kill('INT', pid)
+ Process.kill('TERM', pid)
log "Stopped PID: #{pid} at #{f}"
rescue => e
log "Failed to stop! #{k}: #{e}"
end
end
@@ -221,21 +222,29 @@
# 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]
+ # if the log directory doesn't exist, create it
+ FileUtils.mkdir_p File.dirname(options[:log_path]), :mode => 0755
+ # touch the log file to create it
FileUtils.touch log_path
- STDOUT.reopen(log_path, 'a')
- STDERR.reopen STDOUT
+ # Set permissions on the log file
File.chmod(0644, log_path)
- STDOUT.sync = true
+ # Reopen $stdout (NOT +STDOUT+) to start writing to the log file
+ $stdout.reopen(log_path, 'a')
+ # Redirect $stderr to $stdout
+ $stderr.reopen $stdout
+ $stdout.sync = true
else # redirect to /dev/null
- STDIN.reopen "/dev/null"
- STDOUT.reopen "/dev/null", "a"
- STDERR.reopen STDOUT
+ # We're not bothering to sync if we're dumping to /dev/null
+ # because /dev/null doesn't care about buffered output
+ $stdin.reopen '/dev/null'
+ $stdout.reopen '/dev/null', 'a'
+ $stderr.reopen $stdout
end
- log_path = options[:log_path] ? options[:log_path] : "/dev/null"
+ 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)
@@ -246,10 +255,10 @@
end
elapsed_seconds < timeout_seconds
end
def log(message)
- puts message if options[:debug]
+ puts message
end
end
end