class Daemonic::Daemon
Attributes
options[R]
Public Class Methods
new(options)
click to toggle source
# File lib/daemonic/daemon.rb, line 6 def initialize(options) @options = options end
Public Instance Methods
restart(&block)
click to toggle source
# File lib/daemonic/daemon.rb, line 64 def restart(&block) ensure_pid_specified if running? stop start(&block) else puts "Not running. Starting a new worker." cleanup_pid_file start(&block) end end
start(&block)
click to toggle source
# File lib/daemonic/daemon.rb, line 10 def start(&block) fail ArgumentError, "No block given" if block.nil? if options[:daemonize] ensure_pid_specified fork do at_exit { cleanup_pid_file } Process.daemon(true) write_pid_file block.call end sleep 0.1 wait_until(options.fetch(:startup_timeout) { 1 }) { !running? } if running? puts "The daemon started successfully" else puts "The daemon did not start properly" exit 1 end else at_exit { cleanup_pid_file } write_pid_file block.call end end
status()
click to toggle source
# File lib/daemonic/daemon.rb, line 35 def status ensure_pid_specified if running? puts "Running with pid: #{pid.inspect}" exit 0 else puts "Not running. Pid: #{pid.inspect}" exit 2 end end
stop()
click to toggle source
# File lib/daemonic/daemon.rb, line 46 def stop ensure_pid_specified puts "Stopping" if running? Process.kill("TERM", pid) wait_until(options.fetch(:stop_timeout) { 5 }) { !running? } if running? puts "Couldn't shut down. Pid: #{pid}" exit 1 else puts "Worker shut down." end else puts "Not running. Pid: #{pid.inspect}" exit 1 end end
Private Instance Methods
cleanup_pid_file()
click to toggle source
# File lib/daemonic/daemon.rb, line 90 def cleanup_pid_file File.unlink(pid_file) if pid_file && File.exist?(pid_file) end
ensure_pid_specified()
click to toggle source
# File lib/daemonic/daemon.rb, line 118 def ensure_pid_specified unless pid_file puts "No location of PID specified." exit 1 end end
pid()
click to toggle source
# File lib/daemonic/daemon.rb, line 86 def pid File.exist?(pid_file) && Integer(File.read(pid_file).strip) end
pid_file()
click to toggle source
# File lib/daemonic/daemon.rb, line 101 def pid_file options[:pid] end
running?()
click to toggle source
# File lib/daemonic/daemon.rb, line 78 def running? return false unless pid Process.getpgid(pid) true rescue Errno::ESRCH false end
wait_until(timeout, &condition)
click to toggle source
# File lib/daemonic/daemon.rb, line 105 def wait_until(timeout, &condition) sleep 0.1 Timeout.timeout(timeout) do until condition.call print "." sleep 0.1 end end print "\n" rescue Timeout::Error print "\n" end
write_pid_file()
click to toggle source
# File lib/daemonic/daemon.rb, line 94 def write_pid_file if pid_file FileUtils.mkdir_p(File.dirname(pid_file)) File.open(pid_file, "w") { |f| f.puts Process.pid } end end