lib/rflow/daemon_process.rb in rflow-1.0.1 vs lib/rflow/daemon_process.rb in rflow-1.1.0
- old
+ new
@@ -1,10 +1,13 @@
+require 'rflow/pid_file'
+
class RFlow
class DaemonProcess
- def initialize(name, role = name)
+ def initialize(name, role = name, options = {})
@name = name
@role = role
+ @pid_file = PIDFile.new(options[:pid_file_path]) if options[:pid_file_path]
end
def daemonize!
RFlow.logger.info "#{@name} daemonizing"
establish_daemon_pipe
@@ -17,27 +20,30 @@
daemonize_process
end
end
def run!
+ write_pid_file
register_logging_context
update_process_name
handle_signals
spawn_subprocesses
signal_successful_start
RFlow.logger.info "#{@role} started"
run_process
ensure
unhandle_signals
+ remove_pid_file
end
def spawn_subprocesses; end
def subprocesses; []; end
def shutdown!(reason)
RFlow.logger.info "#{@name} shutting down due to #{reason}"
+ remove_pid_file
unhandle_signals
signal_subprocesses('QUIT')
RFlow.logger.info "#{@name} exiting"
end
@@ -63,10 +69,11 @@
if child_pid > 1
RFlow.logger.info "#{@role} indicated successful daemonization"
exit 0
else
RFlow.logger.error "#{@role} failed to start"
+ STDERR.puts "\n\n*** #{@role} failed to start; see log file for details"
exit! 1
end
end
def daemonize_process
@@ -77,11 +84,11 @@
def close_stdio_streams
$stdout.sync = $stderr.sync = true
[$stdin, $stdout, $stderr].each do |stream|
stream.binmode
- begin; stream.reopen "/dev/null"; rescue ::Exception; end
+ begin; stream.reopen '/dev/null'; rescue ::Exception; end
end
end
def register_logging_context
# arrange for process's name to appear in log messages
@@ -149,7 +156,10 @@
subprocesses.reject {|p| p.pid.nil? }.each do |p|
RFlow.logger.info "Signaling #{p.name} with #{signal}"
Process.kill(signal, p.pid)
end
end
+
+ def write_pid_file; @pid_file.write if @pid_file; end
+ def remove_pid_file; @pid_file.safe_unlink if @pid_file; end
end
end