class Upman::Core::Daemon

Attributes

options[R]
quit[R]

Public Class Methods

new(options) click to toggle source
# File lib/upman/core/daemon.rb, line 12
def initialize(options)
  @options = options
  options[:logfile] = File.expand_path(logfile) if logfile? # daemonization might change CWD so expand any relative paths in advance
  options[:pidfile] = File.expand_path(pidfile) if pidfile? # (ditto)


end

Public Instance Methods

check_pid() click to toggle source
# File lib/upman/core/daemon.rb, line 125
def check_pid
  if pidfile?
    case pid_status(pidfile)
    when :running, :not_owned
      puts "A server is already running. Check #{pidfile}"
      exit(1)
    when :dead
      File.delete(pidfile)
    end
  end
end
daemonize() click to toggle source
DAEMONIZING, PID MANAGEMENT, and OUTPUT REDIRECTION
# File lib/upman/core/daemon.rb, line 92
def daemonize
  exit if fork
  Process.setsid
  exit if fork
  Dir.chdir "/"
end
daemonize?() click to toggle source
# File lib/upman/core/daemon.rb, line 20
def daemonize?
  options[:daemonize]
end
logfile() click to toggle source
# File lib/upman/core/daemon.rb, line 24
def logfile
  options[:logfile]
end
logfile?() click to toggle source
# File lib/upman/core/daemon.rb, line 32
def logfile?
  !logfile.nil?
end
pid_status(pidfile) click to toggle source
# File lib/upman/core/daemon.rb, line 137
def pid_status(pidfile)
  return :exited unless File.exists?(pidfile)
  pid = ::File.read(pidfile).to_i
  return :dead if pid == 0
  Process.kill(0, pid)
  :running
rescue Errno::ESRCH
  :dead
rescue Errno::EPERM
  :not_owned
end
pidfile() click to toggle source
# File lib/upman/core/daemon.rb, line 28
def pidfile
  options[:pidfile]
end
pidfile?() click to toggle source
# File lib/upman/core/daemon.rb, line 36
def pidfile?
  !pidfile.nil?
end
redirect_output() click to toggle source
# File lib/upman/core/daemon.rb, line 99
def redirect_output
  FileUtils.mkdir_p(File.dirname(logfile), :mode => 0755)
  FileUtils.touch logfile
  File.chmod(0644, logfile)
  $stderr.reopen(logfile, 'a')
  $stdout.reopen($stderr)
  $stdout.sync = $stderr.sync = true
end
run!() click to toggle source
# File lib/upman/core/daemon.rb, line 43
def run!

  check_pid
  daemonize if daemonize?
  write_pid
  trap_signals

  if logfile?
    redirect_output
  elsif daemonize?
    suppress_output
  end

  api = Upman::Utils::Api.new
  unless api.check_connection
    return nil
  end

  register = Upman::Core::Register.new
  register.perform

  threads = []

  Thread.abort_on_exception = true

  threads << Thread.new do
    server = Upman::Server::Socket.new
    server.run!
  end

  threads << Thread.new do
    worker = Upman::Core::Worker.new
    worker.run!
  end

  trap("INT") do
    warn "CTRL+C - Shutdown all threads"
    threads.each { |t| Thread.kill t }
    exit 130
  end

  threads.each { |t| t.join }

end
suppress_output() click to toggle source
# File lib/upman/core/daemon.rb, line 108
def suppress_output
  $stderr.reopen('/dev/null', 'a')
  $stdout.reopen($stderr)
end
trap_signals() click to toggle source
SIGNAL HANDLING
# File lib/upman/core/daemon.rb, line 153
def trap_signals
  trap(:QUIT) do # graceful shutdown
    @quit = true
  end
end
write_pid() click to toggle source
# File lib/upman/core/daemon.rb, line 113
def write_pid
  if pidfile?
    begin
      File.open(pidfile, ::File::CREAT | ::File::EXCL | ::File::WRONLY) {|f| f.write("#{Process.pid}")}
      at_exit {File.delete(pidfile) if File.exists?(pidfile)}
    rescue Errno::EEXIST
      check_pid
      retry
    end
  end
end