lib/process/daemon.rb in process-daemon-0.5.1 vs lib/process/daemon.rb in process-daemon-0.5.3

- old
+ new

@@ -43,52 +43,50 @@ # Server.daemonize # # The base directory specifies a path such that: # working_directory = "." # log_directory = #{working_directory}/log - # log_file_path = #{log_directory}/#{daemon_name}.log + # log_file_path = #{log_directory}/#{name}.log # runtime_directory = #{working_directory}/run - # process_file_path = #{runtime_directory}/#{daemon_name}.pid + # process_file_path = #{runtime_directory}/#{name}.pid class Daemon - def initialize(base_directory = ".") - @base_directory = base_directory + def initialize(working_directory = ".") + @working_directory = working_directory end # Return the name of the daemon - def daemon_name + def name return self.class.name.gsub(/[^a-zA-Z0-9]+/, '-') end # The directory the daemon will run in. - def working_directory - @base_directory - end + attr :working_directory # Return the directory to store log files in. def log_directory File.join(working_directory, "log") end # Standard log file for stdout and stderr. def log_file_path - File.join(log_directory, "#{daemon_name}.log") + File.join(log_directory, "#{name}.log") end # Runtime data directory for the daemon. def runtime_directory File.join(working_directory, "run") end # Standard location of process pid file. def process_file_path - File.join(runtime_directory, "#{daemon_name}.pid") + File.join(runtime_directory, "#{name}.pid") end # Mark the output log. def mark_log File.open(log_file_path, "a") do |log_file| - log_file.puts "=== Log Marked @ #{Time.now.to_s} ===" + log_file.puts "=== Log Marked @ #{Time.now.to_s} [#{Process.pid}] ===" end end # Prints some information relating to daemon startup problems. def tail_log(output) @@ -112,12 +110,17 @@ return false end # The main function to setup any environment required by the daemon def prefork - @base_directory = File.expand_path(@base_directory) if @base_directory - + # We freeze the working directory because it can't change after forking: + @working_directory = File.expand_path(working_directory) + + def self.working_directory + @working_directory + end + FileUtils.mkdir_p(log_directory) FileUtils.mkdir_p(runtime_directory) end # The main function to start the daemon @@ -125,10 +128,10 @@ end # The main function to stop the daemon def shutdown # Interrupt all children processes, preferably to stop them so that they are not left behind. - Process.kill(0, :INT) + Process.kill(:INT, 0) end def run trap("INT") do shutdown