Class: Chore::CLI

Inherits:
Object
  • Object
show all
Includes:
Util, Singleton
Defined in:
lib/chore/cli.rb

Overview

Class that handles the command line interactions in Chore. It primarily is responsible for invoking the Chore process with the provided configuration to begin processing jobs.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#constantize, #procline

Constructor Details

#initializeCLI

Returns a new instance of CLI.



21
22
23
24
25
# File 'lib/chore/cli.rb', line 21

def initialize
  @options = {}
  @registered_opts = {}
  @stopping = false
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/chore/cli.rb', line 19

def options
  @options
end

#registered_optsObject (readonly)

Returns the value of attribute registered_opts.



19
20
21
# File 'lib/chore/cli.rb', line 19

def registered_opts
  @registered_opts
end

Class Method Details

.register_option(key, *args, &blk) ⇒ Object

register_option is a method for plugins or other components to register command-line config options.

  • key is the name for this option that can be referenced from Chore.config.key

  • *args is an OptionParser style list of options.

  • &blk is an option block, passed to OptionParser

Examples

Chore::CLI.register_option 'sample', '-s', '--sample-key SOME_VAL', 'A description of this value'

Chore::CLI.register_option 'something', '-g', '--something-complex VALUE', 'A description' do |arg|
  # make sure your key here matches the key you register
  options[:something] arg.split(',')
end


40
41
42
# File 'lib/chore/cli.rb', line 40

def self.register_option(key,*args,&blk)
  instance.register_option(key,*args,&blk)
end

Instance Method Details

#parse(args = ARGV) ⇒ Object

:nodoc:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/chore/cli.rb', line 72

def parse(args=ARGV) #:nodoc:
  Chore.configuring = true
  setup_options

  # parse once to load the config file & require options
  # any invalid options are ignored the first time around since booting the
  # system may register additional options from 3rd-party libs
  parse_opts(args, true)
  parse_config_file(@options[:config_file], true) if @options[:config_file]

  validate!
  boot_system

  # parse again to pick up options required by loaded classes
  # any invalid options will raise an exception this time
  parse_opts(args)
  parse_config_file(@options[:config_file]) if @options[:config_file]
  detect_queues
  Chore.configure(options)
  Chore.configuring = false
  validate_strategy!
end

#parse_config_file(file, ignore_errors = false) ⇒ Object

:nodoc:



66
67
68
69
70
# File 'lib/chore/cli.rb', line 66

def parse_config_file(file, ignore_errors = false) #:nodoc:
  data = File.read(file)
  data = ERB.new(data).result
  parse_opts(data.split(/\s/).map!(&:chomp).map!(&:strip), ignore_errors)
end

#register_option(key, *args, &blk) ⇒ Object

:nodoc:



44
45
46
47
# File 'lib/chore/cli.rb', line 44

def register_option(key,*args,&blk) #:nodoc:
  registered_opts[key] = {:args => args}
  registered_opts[key].merge!(:block => blk) if blk
end

#run!(args = ARGV) ⇒ Object

Start up the consuming side of the application. This calls Chore::Manager#start.



50
51
52
53
54
# File 'lib/chore/cli.rb', line 50

def run!(args=ARGV)
  parse(args)
  @manager = Chore::Manager.new
  @manager.start
end

#shutdownObject

Begins the Chore shutdown process. This will call Chore::Manager#shutdown if it is not already in the process of stopping Exits with code 0



58
59
60
61
62
63
64
# File 'lib/chore/cli.rb', line 58

def shutdown
  unless @stopping
    @stopping = true
    @manager.shutdown! if @manager
    exit(0)
  end
end