Class: PikaQue::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/pika_que/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#environmentObject

Returns the value of attribute environment.



10
11
12
# File 'lib/pika_que/cli.rb', line 10

def environment
  @environment
end

Instance Method Details

#configObject



41
42
43
# File 'lib/pika_que/cli.rb', line 41

def config
  PikaQue.config
end

#daemonizeObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pika_que/cli.rb', line 58

def daemonize
  return unless config[:daemon]

  files_to_reopen = []
  ObjectSpace.each_object(File) do |file|
    files_to_reopen << file unless file.closed?
  end

  ::Process.daemon(true, true)

  files_to_reopen.each do |file|
    begin
      file.reopen file.path, "a+"
      file.sync = true
    rescue ::Exception
    end
  end

  [$stdout, $stderr].each do |io|
    File.open(config[:logfile], 'ab') do |f|
      io.reopen(f)
    end
    io.sync = true
  end
  $stdin.reopen(File::NULL)

  init_logger
end

#init_config(opts) ⇒ Object



45
46
47
48
49
50
# File 'lib/pika_que/cli.rb', line 45

def init_config(opts)
  if opts[:config]
    config.load(File.expand_path(opts[:config]))
  end
  config.merge!(opts)
end

#init_loggerObject



52
53
54
55
56
# File 'lib/pika_que/cli.rb', line 52

def init_logger
  PikaQue::Logging.init_logger(config[:logfile]) if config[:logfile]
  PikaQue.logger.level = ::Logger::WARN if config[:quiet]
  PikaQue.logger.level = ::Logger::DEBUG if config[:verbose]
end

#load_appObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/pika_que/cli.rb', line 96

def load_app
  if File.directory?(config[:require])
    rails_path = File.expand_path(File.join(config[:require], 'config', 'environment.rb'))
    if File.exist?(rails_path)
      ENV['RACK_ENV'] = ENV['RAILS_ENV'] = environment
      PikaQue.logger.info "found rails project (#{config[:require]}), booting app in #{ENV['RACK_ENV']} environment"
      require 'rails'
      require 'pika_que/rails'
      require rails_path
      ::Rails.application.eager_load!
    end
  else
    require(File.expand_path(config[:require])) || raise(ArgumentError, 'require returned false')
  end
end

#parse(args = ARGV) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/pika_que/cli.rb', line 12

def parse(args = ARGV)
  opts = parse_options(args)
  init_config(opts)
  init_logger
  daemonize
  write_pid
end

#parse_options(args) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/pika_que/cli.rb', line 122

def parse_options(args)
  opts = {}

  @parser = OptionParser.new do |o|
    o.banner = 'usage: pika_que [options]'

    o.on '-c', '--concurrency INT', "processor threads to use" do |arg|
      opts[:concurrency] = Integer(arg)
    end

    o.on '-d', '--daemon', "Daemonize process" do |arg|
      opts[:daemon] = arg
    end

    o.on '-e', '--environment ENV', "Application environment" do |arg|
      opts[:environment] = arg
    end

    o.on '-q', '--quiet', "Print quiet output" do |arg|
      opts[:quiet] = arg
    end

    o.on '-v', '--verbose', "Print verbose output" do |arg|
      opts[:verbose] = arg
    end

    o.on '-r', '--require [PATH|DIR]', "Location of Rails application with workers or file to require" do |arg|
      opts[:require] = arg
    end

    o.on '-w', '--worker WORKER(S)', "comma separated list of workers" do |arg|
      opts[:workers] = arg.split(",")
    end

    o.on '--no-delay', "turn off delay processor" do |arg|
      opts[:delay] = arg
    end

    o.on '-C', '--config PATH', "path to config yml file" do |arg|
      opts[:config] = arg
    end

    o.on '-L', '--logfile PATH', "path to writable logfile" do |arg|
      opts[:logfile] = arg
    end

    o.on '-P', '--pidfile PATH', "path to pidfile" do |arg|
      opts[:pidfile] = arg
    end

    o.on '-V', '--version', "Print version and exit" do
      puts "PikaQue #{PikaQue::VERSION}"
      exit 0
    end

    o.on_tail '-h', '--help', 'Show this message and exit' do
      puts o
      exit 0
    end
  end

  @parser.parse!(args)

  @environment = opts[:environment] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'

  opts[:logfile] ||= opts[:daemon] ? 'pika_que.log' : STDOUT

  opts
end

#prepare_serverObject



112
113
114
115
116
117
118
119
120
# File 'lib/pika_que/cli.rb', line 112

def prepare_server
  PikaQue.middleware do |chain|
    config[:middlewares].each{ |m| chain.add PikaQue::Util.constantize(m) } if config[:middlewares]
  end

  PikaQue.reporters do |rptrs|
    config[:reporters].each{ |r| rptrs << PikaQue::Util.constantize(r).new }
  end
end

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pika_que/cli.rb', line 20

def run

  load_app
  prepare_server

  runner = Runner.new.tap{ |r| r.setup_processors }

  begin

    Launcher.launch(runner) do
      runner.run
    end

    exit 0
  rescue Interrupt, SetupError => e
    PikaQue.logger.info "Shutting down: #{e.class.name} received"
    runner.stop
    exit 1
  end
end

#write_pidObject



87
88
89
90
91
92
93
94
# File 'lib/pika_que/cli.rb', line 87

def write_pid
  if path = config[:pidfile]
    pidfile = File.expand_path(path)
    File.open(pidfile, 'w') do |f|
      f.puts ::Process.pid
    end
  end
end