| In: |
bin/rq.rb
|
| Parent: | Object |
the Main class is responsible for parsing command line paramters and switches, doing some validation, initializing logging, and, ultimately, delegating the bulk of the work to a MainHelper based on the mode given. the relationship between Main and MainHelper is a tight one by design - the primary purpose of it being to prevent the Main class from becoming 10000 lines long. the delegators used include:
| OPTSPEC | = | #--{{{ [ [ '--priority=priority', '-p', 'modes |
| an enumeration of option specifications used to parse command line | ||
| CONFIG_DEFAULT_PATH | = | 'rq.conf' |
| the default config file searched for has this basename | ||
| CONFIG_SEARCH_PATH | = | %w( . ~ /dmsp/reference/etc /usr/local/etc /usr/etc /etc ) |
| config files are searched for using this list of locations | ||
| Q | = | ENV['RQ_Q'] || ENV['RQ_QUEUE'] |
| the queue can be specified in the environment | ||
| argv | [R] | |
| cmd | [R] | |
| daemon | [R] | |
| env | [R] | |
| logger | [R] | |
| mode | [R] | |
| options | [R] | |
| q | [R] | |
| qpath | [R] |
given a command line and environment run the rq program
# File bin/rq.rb, line 173
173: def initialize argv = ARGV, env = ENV
174: #--{{{
175: begin
176: @logger = Logger::new STDERR
177: @argv = Util::mcp(argv.to_a)
178: @env = Util::mcp(env.to_hash)
179: @cmd = ([$0] + @argv).join(' ')
180:
181: parse_options
182:
183: if(@options.has_key?('help') or @argv.include?('help'))
184: usage('port' => STDOUT, 'long' => true)
185: exit EXIT_SUCCESS
186: end
187:
188: if(@options.has_key?('template') or (idx = @argv.index('template')))
189: gen_template(@options['template'] || @argv[idx + 1])
190: exit EXIT_SUCCESS
191: end
192:
193: if @options.has_key?('version')
194: puts RQ::VERSION
195: exit EXIT_SUCCESS
196: end
197:
198: parse_argv
199:
200: status = run
201:
202: case status
203: when Integer
204: exit status
205: else
206: exit(status ? EXIT_SUCCESS : EXIT_FAILURE)
207: end
208: rescue => e
209: unless SystemExit === e
210: logerr e
211: exit EXIT_FAILURE
212: else
213: exit e.status
214: end
215: end
216: #--}}}
217: end
delegated to a Configurator
# File bin/rq.rb, line 365
365: def configure
366: #--{{{
367: init_logging
368: configurator = Configurator::new self
369: configurator.configure
370: #--}}}
371: end
generate a template/sample config file which can then be edited
# File bin/rq.rb, line 482
482: def gen_template template
483: #--{{{
484: ConfigFile::gen_template(template)
485: self
486: #--}}}
487: end
initialize configuration file - not currenlty utilized
# File bin/rq.rb, line 469
469: def init_config
470: #--{{{
471: @config =
472: if @options['config']
473: ConfigFile::new(@options['config'])
474: else
475: ConfigFile::any CONFIG_DEFAULT_PATH, CONFIG_SEARCH_PATH
476: end
477: debug { "config.path <#{ @config.path }>" }
478: @config
479: #--}}}
480: end
initialize logging object - all classes then use this object
# File bin/rq.rb, line 425
425: def init_logging
426: #--{{{
427: log, log_age, log_size, verbosity =
428: @options.values_at 'log', 'log_age', 'log_size', 'verbosity'
429: log_age = atoi log_age rescue nil
430: log_size = atoi log_size rescue nil
431: $logger = @logger = Logger::new(log || STDERR, log_age, log_size)
432: #
433: # hack to fix Logger sync bug
434: #
435: @logger.class.instance_eval do
436: attr :logdev unless @logger.respond_to?(:logdev)
437: end
438:
439: @logdev = @logger.logdev.dev
440: @logdev.sync = true
441: level = nil
442: verbosity ||= 'info'
443: verbosity =
444: case verbosity
445: when /^\s*(?:4|d|debug)\s*$/io
446: level = 'Logging::DEBUG'
447: 4
448: when /^\s*(?:3|i|info)\s*$/io
449: level = 'Logging::INFO'
450: 3
451: when /^\s*(?:2|w|warn)\s*$/io
452: level = 'Logging::WARN'
453: 2
454: when /^\s*(?:1|e|error)\s*$/io
455: level = 'Logging::ERROR'
456: 1
457: when /^\s*(?:0|f|fatal)\s*$/io
458: level = 'Logging::FATAL'
459: 0
460: else
461: abort "illegal verbosity setting <#{ verbosity }>"
462: end
463: @logger.level = 2 - ((verbosity % 5) - 2)
464: #debug {"logging level <#{ level }>"}
465: @logger
466: #--}}}
467: end
extract command lines args
# File bin/rq.rb, line 219
219: def parse_argv
220: #--{{{
221: @qpath = Q || @argv.shift
222: @mode = @argv.shift
223: #--}}}
224: end
uses OPTSPEC to parse command line switches
# File bin/rq.rb, line 412
412: def parse_options
413: #--{{{
414: @op = OptionParser.new
415: @options = {}
416: OPTSPEC.each do |spec|
417: k = spec.first.gsub(%/(?:--)|(?:=.*$)|(?:\s+)/o,'')
418: @op.def_option(*spec){|v| @options[k] = v}
419: end
420: @op.parse! @argv
421: @options
422: #--}}}
423: end
select a MainHelper based on mode and delegate to it
# File bin/rq.rb, line 226
226: def run
227: #--{{{
228: @qpath = Util::realpath @qpath
229:
230: if @mode.nil? or @mode.strip.empty?
231: usage 'port' => STDERR, 'long' => false
232: exit EXIT_FAILURE
233: end
234:
235: shortcuts = {
236: 'c' => 'create',
237: 's' => 'submit',
238: 'l' => 'list',
239: 'ls' => 'list',
240: 't' => 'status',
241: 'd' => 'delete',
242: 'rm' => 'delete',
243: 'u' => 'update',
244: 'q' => 'query',
245: 'e' => 'execute',
246: 'C' => 'configure',
247: 'S' => 'snapshot',
248: 'L' => 'lock',
249: 'b' => 'backup',
250: 'r' => 'rotate',
251: 'h' => 'help',
252: 'f' => 'feed',
253: }
254:
255: if((longmode = shortcuts[@mode]))
256: @mode = longmode
257: end
258:
259: begin
260: case @mode
261: when 'create'
262: create
263: when 'submit'
264: submit
265: when 'list'
266: list
267: when 'status'
268: status
269: when 'delete'
270: delete
271: when 'update'
272: update
273: when 'query'
274: query
275: when 'execute'
276: execute
277: when 'configure'
278: configure
279: when 'snapshot'
280: snapshot
281: when 'lock'
282: lock
283: when 'backup'
284: backup
285: when 'rotate'
286: rotate
287: when 'help'
288: usage 'port' => STDOUT, 'long' => true
289: exit EXIT_SUCCESS
290: when 'feed'
291: feed
292: else
293: raise "invalid mode <#{ @mode }>"
294: end
295: rescue Errno::EPIPE => e
296: raise if STDOUT.tty?
297: end
298: #--}}}
299: end
delegated to a Snapshotter
# File bin/rq.rb, line 373
373: def snapshot
374: #--{{{
375: init_logging
376: snapshotter = Snapshotter::new self
377: snapshotter.snapshot
378: #--}}}
379: end
delegated to a StatusLister
# File bin/rq.rb, line 325
325: def status
326: #--{{{
327: init_logging
328: statuslister = StatusLister::new self
329: statuslister.statuslist
330: #--}}}
331: end