Class: FluQ::CLI

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

Constant Summary collapse

SIGNALS =
[ :QUIT, :INT, :TERM, :HUP ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Constructor



21
22
23
24
25
26
27
# File 'lib/fluq/cli.rb', line 21

def initialize
  super

  # Parse options
  @options = {}
  parser.parse!(ARGV)
end

Instance Attribute Details

#optionsObject (readonly)

attr_reader [Hash] options



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

def options
  @options
end

Class Method Details

.runObject

Runs the CLI



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

def self.run
  if BasicSocket.respond_to?(:do_not_reverse_lookup=)
    BasicSocket.do_not_reverse_lookup = true
  end
  new.run
end

Instance Method Details

#configured?Boolean

Returns true if configured.

Returns:

  • (Boolean)

    true if configured



69
70
71
# File 'lib/fluq/cli.rb', line 69

def configured?
  options[:config] && File.file?(options[:config])
end

#runObject



29
30
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
59
60
61
62
63
64
65
66
# File 'lib/fluq/cli.rb', line 29

def run
  # Exit if not configured correctly
  unless configured?
    puts parser
    exit
  end

  # Boot and add project's lib/ dir to load path
  require 'fluq'
  $LOAD_PATH.unshift FluQ.root.join('lib')
  procline "(starting)"

  # Setup logger
  if options[:log]
    FileUtils.mkdir_p(File.dirname(options[:log]))
    FluQ.logger = ::Logger.new(options[:log])
  end
  if options[:verbose]
    FluQ.logger.level = ::Logger::DEBUG
  end

  # Write PID file
  @pidfile = options[:pidfile] || FluQ.root.join("tmp", "pids", "fluq.pid")
  FileUtils.mkdir_p(File.dirname(@pidfile))
  File.open(@pidfile, "w") {|f| f.write Process.pid }

  # Trap signals
  SIGNALS.each do |signal|
    trap(signal) {|*| shutdown! }
  end

  # Start
  log "Starting FluQ #{FluQ::VERSION} (#{options[:config]})"
  FluQ::Runner.run do |runner|
    FluQ::DSL::Root.new(options[:config]).apply(runner)
    procline
  end
end