Class: Pmux::LogView::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/pmux-logview/application.rb

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



9
10
11
12
13
14
15
16
# File 'lib/pmux-logview/application.rb', line 9

def initialize
  @config = nil
  @foreground = false
  @config_file_path = "/etc/pmux-logview/pmux-logview.conf"
  @pidfile = "/var/run/pmux-logview.pid"
  @host = "0.0.0.0"
  @port = 28080
end

Instance Method Details

#daemonizeObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/pmux-logview/application.rb', line 42

def daemonize
  if !@foreground
    exit!(0) if Process.fork
    Process.setsid
    exit!(0) if Process.fork
    STDIN.reopen("/dev/null", "r")
    STDOUT.reopen("/dev/null", "w")
    STDERR.reopen("/dev/null", "w")
  end
end

#load_configObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pmux-logview/application.rb', line 26

def load_config
  @config = {} if @config.nil?
  return false if @config_file_path.nil? || @config_file_path == ""
  begin
    new_config = YAML.load_file(@config_file_path)
  rescue Errno::ENOENT
    puts "not found password file (#{@config_file_path})"
  rescue Errno::EACCES
    puts "can not access password file (#{@config_file_path})"
  rescue Exception => e
    puts "error occurred in password loading: #{e}"
    puts e.backtrace.join("\n")
  end
    @config = new_config
end

#parse_argsObject



18
19
20
21
22
23
24
# File 'lib/pmux-logview/application.rb', line 18

def parse_args
  OptionParser.new do |opt|
    opt.on('-c [config_file_path]', '--config [config_file_path]') {|v| @config_file_path = v}
    opt.on('-F', '--foreground') {|v| @foreground = true}
    opt.parse!(ARGV)
  end
end

#runObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pmux-logview/application.rb', line 53

def run
  initialize()
  parse_args()
  daemonize()
  load_config()
  @config["foreground"] = @foreground
  @pidfile = @config["pidfile"] if @config["pidfile"]
  open(@pidfile, 'w') {|f| f << Process.pid } if @pidfile
  @host = @config["host"] if @config["host"]
  @port = @config["port"] if @config["port"]
  Controller.setup(@config)
  Controller.run! :bind => @host, :host => @host, :port => @port
end