Class: MicroQ::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.runObject



6
7
8
9
10
11
# File 'lib/micro_q/cli.rb', line 6

def self.run
  @cli ||= new
  @cli.parse
  @cli.verify!
  @cli.setup
end

Instance Method Details

#parseObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/micro_q/cli.rb', line 13

def parse
  opts = Slop.parse do
    banner 'Usage: microq [options]'

    on 'r=', 'The path to the rails application'
    on 'require=', 'The path to the rails application'
    on 'w=', 'The number of worker threads'
    on 'workers=', 'The number of worker threads'
  end

  @workers = opts[:workers] || opts[:w]
  @require = opts[:require] || opts[:r]
end

#setupObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/micro_q/cli.rb', line 31

def setup
  puts 'Requiring rails...'
  require 'rails'

  puts 'Requiring rails application...'
  if File.directory?(@require)
    require File.expand_path("#{@require}/config/environment.rb")
  else
    require @require
  end

  aws_keys = MicroQ.config.aws.try(:keys) || []
  raise 'SQS mode requires an aws :key and :secret see https://github.com/bnorton/micro_q/wiki/Named-Queues' unless aws_keys.include?(:key) && aws_keys.include?(:secret)

  MicroQ.configure do |config|
    config.queue = MicroQ::Queue::Sqs # set workers after since this must set workers=0 internally
    config.workers = @workers.to_i if @workers
    config['worker_mode?'] = true
  end

  puts "Running micro_q in SQS mode with #{MicroQ.config.workers} workers... Hit ctl+c to stop...\n"
  MicroQ.start

  sleep
rescue Interrupt
  puts 'Exiting via interrupt'
  exit(1)
end

#verify!Object



27
28
29
# File 'lib/micro_q/cli.rb', line 27

def verify!
  raise "Need a valid path to a rails application, you gave us #{@require}\n" unless /environment\.rb/ === @require || File.exist?("#{@require}/config/application.rb")
end