class Process::Daemon
Provides the infrastructure for spawning a daemon.
Constants
- TIMEOUT
Daemon
startup timeout- VERSION
Attributes
The process title of the daemon.
The directory the daemon will run in.
Public Class Methods
The process controller, responsible for managing the daemon process start, stop, restart, etc.
# File lib/process/daemon.rb, line 172 def controller(options = {}) @controller ||= Controller.new(instance, options) end
The main entry point for daemonized scripts.
# File lib/process/daemon.rb, line 177 def daemonize(*args, **options) args = ARGV if args.empty? controller(options).daemonize(args) end
A shared instance of the daemon.
# File lib/process/daemon.rb, line 167 def instance @instance ||= self.new end
Initialize the daemon in the given working root.
# File lib/process/daemon.rb, line 34 def initialize(working_directory = ".") @working_directory = working_directory @shutdown_notification = Notification.new end
Start the shared daemon instance.
# File lib/process/daemon.rb, line 184 def start controller.start end
Check if the shared daemon instance is runnning or not.
# File lib/process/daemon.rb, line 194 def status controller.status end
Stop the shared daemon instance.
# File lib/process/daemon.rb, line 189 def stop controller.stop end
Public Instance Methods
Check the last few lines of the log file to find out if the daemon crashed.
# File lib/process/daemon.rb, line 85 def crashed? count = 3 LogFile.open(log_file_path).tail_log do |line| return true if line.match("=== Daemon Crashed") break if (count -= 1) == 0 end return false end
Return the directory to store log files in.
# File lib/process/daemon.rb, line 49 def log_directory File.join(working_directory, "log") end
Standard log file for stdout and stderr.
# File lib/process/daemon.rb, line 54 def log_file_path File.join(log_directory, "#{name}.log") end
Mark the output log.
# File lib/process/daemon.rb, line 69 def mark_log File.open(log_file_path, "a") do |log_file| log_file.puts "=== Log Marked @ #{Time.now.to_s} [#{Process.pid}] ===" end end
Return the name of the daemon
# File lib/process/daemon.rb, line 41 def name return self.class.name.gsub(/[^a-zA-Z0-9]+/, '-') end
The main function to setup any environment required by the daemon
# File lib/process/daemon.rb, line 98 def prefork # Ignore any previously setup signal handler for SIGINT: trap(:INT, :DEFAULT) # We update the working directory to a full path: @working_directory = File.expand_path(working_directory) FileUtils.mkdir_p(log_directory) FileUtils.mkdir_p(runtime_directory) end
Standard location of process pid file.
# File lib/process/daemon.rb, line 64 def process_file_path File.join(runtime_directory, "#{name}.pid") end
Request that the sleep_until_interrupted
function call returns.
# File lib/process/daemon.rb, line 124 def request_shutdown @shutdown_notification.signal end
If you want to implement a long running process you override this method. You may like to call super but it is not necessary to use the supplied interruption machinery.
# File lib/process/daemon.rb, line 142 def run sleep_until_interrupted end
Runtime data directory for the daemon.
# File lib/process/daemon.rb, line 59 def runtime_directory File.join(working_directory, "run") end
This function should terminate any active processes in the daemon and return as quickly as possible.
# File lib/process/daemon.rb, line 147 def shutdown end
Call this function to sleep until the daemon is sent SIGINT.
# File lib/process/daemon.rb, line 129 def sleep_until_interrupted trap(:INT) do self.request_shutdown end @shutdown_notification.wait end
The entry point from the newly forked process.
# File lib/process/daemon.rb, line 151 def spawn self.title = self.name self.startup begin self.run rescue Interrupt $stderr.puts "Daemon interrupted, proceeding to shutdown." end self.shutdown end
This function must setup the daemon quickly and return.
# File lib/process/daemon.rb, line 138 def startup end
Prints some information relating to daemon startup problems.
# File lib/process/daemon.rb, line 76 def tail_log(output) lines = LogFile.open(log_file_path).tail_log do |line| line.match("=== Log Marked") || line.match("=== Daemon Exception Backtrace") end output.puts lines end
Set the process title - only works after daemon has forked.
# File lib/process/daemon.rb, line 113 def title= title @title = title if Process.respond_to? :setproctitle Process.setproctitle(@title) else $0 = @title end end