class Oxidized::CLI

Attributes

pidfile[R]

Public Class Methods

new() click to toggle source
# File lib/oxidized/cli.rb, line 22
def initialize
  _args, @opts = parse_opts

  Config.load(@opts)
  Oxidized.setup_logger

  @pidfile = File.expand_path(Oxidized.config.pid)
end

Public Instance Methods

run() click to toggle source
# File lib/oxidized/cli.rb, line 7
def run
  check_pid
  Process.daemon if @opts[:daemonize]
  write_pid
  begin
    Oxidized.logger.info "Oxidized starting, running as pid #{$PROCESS_ID}"
    Oxidized.new
  rescue StandardError => error
    crash error
    raise
  end
end

Private Instance Methods

check_pid() click to toggle source
# File lib/oxidized/cli.rb, line 82
def check_pid
  return unless 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
crash(error) click to toggle source
# File lib/oxidized/cli.rb, line 31
def crash(error)
  Oxidized.logger.fatal "Oxidized crashed, crashfile written in #{Config::Crash}"
  File.open Config::Crash, 'w' do |file|
    file.puts '-' * 50
    file.puts Time.now.utc
    file.puts error.message + ' [' + error.class.to_s + ']'
    file.puts '-' * 50
    file.puts error.backtrace
    file.puts '-' * 50
  end
end
parse_opts() click to toggle source
# File lib/oxidized/cli.rb, line 43
def parse_opts
  opts = Slop.parse do |opt|
    opt.on '-d', '--debug', 'turn on debugging'
    opt.on '--daemonize', 'Daemonize/fork the process'
    opt.on '-h', '--help', 'show usage' do
      puts opt
      exit
    end
    opt.on '--show-exhaustive-config', 'output entire configuration, including defaults' do
      asetus = Config.load
      puts asetus.to_yaml asetus.cfg
      Kernel.exit
    end
    opt.on '-v', '--version', 'show version' do
      puts Oxidized::VERSION_FULL
      Kernel.exit
    end
  end
  [opts.arguments, opts]
end
pid_status(pidfile) click to toggle source
# File lib/oxidized/cli.rb, line 94
def pid_status(pidfile)
  return :exited unless File.exist?(pidfile)

  pid = ::File.read(pidfile).to_i
  return :dead if pid.zero?

  Process.kill(0, pid)
  :running
rescue Errno::ESRCH
  :dead
rescue Errno::EPERM
  :not_owned
end
pidfile?() click to toggle source
# File lib/oxidized/cli.rb, line 66
def pidfile?
  !!pidfile
end
write_pid() click to toggle source
# File lib/oxidized/cli.rb, line 70
def write_pid
  return unless pidfile?

  begin
    File.open(pidfile, ::File::CREAT | ::File::EXCL | ::File::WRONLY) { |f| f.write(Process.pid.to_s) }
    at_exit { File.delete(pidfile) if File.exist?(pidfile) }
  rescue Errno::EEXIST
    check_pid
    retry
  end
end