Class: Zebra::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/zebra/command_line.rb

Overview

Loads command line options and assigns them to singleton Config object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommandLine

Returns a new instance of CommandLine.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/zebra/command_line.rb', line 13

def initialize
  Zebra.config.chdir = File.dirname(__FILE__)
  Zebra.config.tmp_dir = '/tmp'
  Zebra.config.daemonize = false

  begin
    @parser = OptionParser.new() do |opts|
      opts.banner = "Usage: #{$0} --config CONFIG [worker|server|queue]"
      
      opts.on('-c', '--config CONFIG', 'Configuration file') do |config_file|
        Zebra.config.config_file = config_file
      end

      opts.on('-d', '--daemonize', 'Daemonize the process') do |daemonize|
        Zebra.config.daemonize = daemonize
      end
      
      opts.on('-p', '--pid-file PID-FILE', 'Pid-File to save the process id') do |pid_file|
        Zebra.config.pid_file = pid_file
      end
        
      opts.on('-l', '--log-file LOG-FILE', 'Log File') do |log_file|
        Zebra.config.log_file = log_file
      end
    end
    @parser.parse!
    Zebra.config.mode = ARGV.shift if ARGV.length > 0
    raise MissingArgumentException.new("Missing --config parameter") unless Zebra.config.config_file?
    raise MissingArgumentException.new("Missing mode of operation: server|proxy|queue") unless Zebra.config.mode?
  rescue SystemExit 
    exit(1)
  rescue MissingArgumentException => e
    puts usage(e)
  rescue ArgumentError => e
    puts usage(e)
  rescue Exception => e
    puts "#{e.class}: #{e.message}"
    puts e.backtrace.join("\n\t")
  end
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



11
12
13
# File 'lib/zebra/command_line.rb', line 11

def controller
  @controller
end

Instance Method Details

#daemonizeObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/zebra/command_line.rb', line 68

def daemonize
  # Become a daemon
  if RUBY_VERSION < "1.9"
    exit if fork
    Process.setsid
    exit if fork
    Dir.chdir "/" 
    STDIN.reopen "/dev/null"
    STDOUT.reopen "/dev/null", "a" 
    STDERR.reopen "/dev/null", "a" 
  else
    Process.daemon
  end 
end

#executeObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/zebra/command_line.rb', line 83

def execute
  #If log file is specified logs messages to that file, else on stdout
  log_file = Zebra.config.log_file
  fh = nil
  if log_file
    fh = File.open(log_file, 'a')
  else
    fh = STDERR
  end

  fh.sync = true
  Zebra.log = Logger.new(fh)

  Zebra.log.datetime_format = "%Y-%m-%d %H:%M:%S"
  Zebra.log.formatter = proc { |severity, datetime, progname, msg| sprintf "%-15s | %5s | %s\n", datetime.strftime(Zebra.log.datetime_format), severity, msg }
  Zebra.config.namespace ||= $0.to_s
  
  # ZMQ sockets are not thread/process safe
  daemonize if Zebra.config.daemonize

  begin
    case Zebra.config.mode.to_sym
    when :server
      config = Zebra.config.server || {}
      config[:logger] = Zebra.log
      @controller = ProxyServer.new
    when :worker
      config = Zebra.config.worker || {}
      config[:logger] = Zebra.log
      @controller = ProxyWorker.new(config)
    when :queue
      config = Zebra.config.queue || {}
      config[:logger] = Zebra.log
      @controller = Queue.new(config)
    else
      raise UnsupportedArgumentException.new("Cannot handle #{Zebra.config.mode} mode")
    end

    
    if Zebra.config.pid_file?
      Zebra.log.debug("Writing pid file #{Zebra.config.pid_file}")
      File.open(Zebra.config.pid_file, 'w') do |f| 
        f.write(Process.pid)
      end
    end


    @controller.dispatch
  rescue Interrupt => e
    Zebra.log.info e.message
  end
end

#usage(e = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/zebra/command_line.rb', line 54

def usage(e = nil)
  output = ''
  case e
  when MissingArgumentException
    output += "#{e.message}\n"
  when Exception
    output += "#{e.class}: #{e.message}\n"
  when Nil
    # Do nothing 
  end
  output += @parser.to_s
  output 
end