class Invoker::Daemon
rip off from borg github.com/code-mancers/borg/blob/master/lib/borg/borg_daemon.rb
Attributes
process_name[R]
Public Class Methods
new()
click to toggle source
# File lib/invoker/daemon.rb, line 7 def initialize @process_name = 'invoker' end
Public Instance Methods
daemonize()
click to toggle source
# File lib/invoker/daemon.rb, line 38 def daemonize if fork sleep(2) exit(0) else Process.setsid File.open(pid_file, "w") do |file| file.write(Process.pid.to_s) end Invoker::Logger.puts "Invoker daemon log is available at #{log_file}" redirect_io(log_file) $0 = process_name end end
dead?()
click to toggle source
pidfile exists but process isn’t running
# File lib/invoker/daemon.rb, line 80 def dead? status == 1 end
kill_process()
click to toggle source
# File lib/invoker/daemon.rb, line 53 def kill_process pgid = Process.getpgid(pid) Process.kill('-TERM', pgid) File.delete(pid_file) if File.exist?(pid_file) Invoker::Logger.puts "Stopped Invoker daemon" end
log_file()
click to toggle source
# File lib/invoker/daemon.rb, line 34 def log_file File.join(Invoker.home, ".invoker", "#{process_name}.log") end
pid()
click to toggle source
# File lib/invoker/daemon.rb, line 30 def pid File.read(pid_file).strip.to_i end
pid_file()
click to toggle source
# File lib/invoker/daemon.rb, line 26 def pid_file File.join(Invoker.home, ".invoker", "#{process_name}.pid") end
pidfile_exists?()
click to toggle source
# File lib/invoker/daemon.rb, line 71 def pidfile_exists? File.exist?(pid_file) end
process_running?()
click to toggle source
# File lib/invoker/daemon.rb, line 60 def process_running? Process.kill(0, pid) true rescue Errno::ESRCH false end
running?()
click to toggle source
# File lib/invoker/daemon.rb, line 75 def running? status == 0 end
start()
click to toggle source
# File lib/invoker/daemon.rb, line 11 def start if running? Invoker::Logger.puts "Invoker daemon is already running" exit(0) elsif dead? File.delete(pid_file) if File.exist?(pid_file) end Invoker::Logger.puts "Running Invoker daemon" daemonize end
status()
click to toggle source
# File lib/invoker/daemon.rb, line 67 def status @status ||= check_process_status end
stop()
click to toggle source
# File lib/invoker/daemon.rb, line 22 def stop kill_process end
Private Instance Methods
check_process_status()
click to toggle source
# File lib/invoker/daemon.rb, line 86 def check_process_status if pidfile_exists? && process_running? 0 elsif pidfile_exists? # but not process_running 1 else 3 end end
redirect_file_to_target(file, target = "/dev/null")
click to toggle source
# File lib/invoker/daemon.rb, line 120 def redirect_file_to_target(file, target = "/dev/null") begin file.reopen(target) rescue; end end
redirect_io(logfile_name = nil)
click to toggle source
# File lib/invoker/daemon.rb, line 96 def redirect_io(logfile_name = nil) redirect_file_to_target($stdin) redirect_stdout(logfile_name) redirect_stderr end
redirect_stderr()
click to toggle source
# File lib/invoker/daemon.rb, line 102 def redirect_stderr redirect_file_to_target($stderr, $stdout) $stderr.sync = true end
redirect_stdout(logfile_name)
click to toggle source
# File lib/invoker/daemon.rb, line 107 def redirect_stdout(logfile_name) if logfile_name begin $stdout.reopen logfile_name, "a" $stdout.sync = true rescue StandardError redirect_file_to_target($stdout) end else redirect_file_to_target($stdout) end end