RQ::Main (Class)

In: bin/rq.rb
Parent: Object
MainHelper StatusLister Snapshotter Feeder Deleter Relayer Executor Submitter Locker Backer Configurator Lister Rotater Creator Updater Querier ::Hash ConfigFile DRbUndumped JobRunner Main QDB JobQueue JobRunnerDaemon Array SleepCycle Job ArrayFields ::OrderedHash OrderedAutoHash LogMethods Refresher lib/rq-2.0.0/updater.rb lib/rq-2.0.0/lister.rb lib/rq-2.0.0/submitter.rb lib/rq-2.0.0/refresher.rb lib/rq-2.0.0/snapshotter.rb lib/rq-2.0.0/jobrunnerdaemon.rb lib/rq-2.0.0/configfile.rb lib/rq-2.0.0/querier.rb lib/rq-2.0.0/mainhelper.rb lib/rq-2.0.0/creator.rb lib/rq-2.0.0/feeder.rb lib/rq-2.0.0/qdb.rb bin/rq.rb lib/rq-2.0.0/configurator.rb lib/rq-2.0.0/jobqueue.rb lib/rq-2.0.0/backer.rb lib/rq-2.0.0/sleepcycle.rb lib/rq-2.0.0/jobrunner.rb lib/rq-2.0.0/executor.rb lib/rq-2.0.0/relayer.rb lib/rq-2.0.0/job.rb lib/rq-2.0.0/rotater.rb lib/rq-2.0.0/statuslister.rb lib/rq-2.0.0/locker.rb lib/rq-2.0.0/deleter.rb lib/rq-2.0.0/orderedautohash.rb Usage Util LogClassMethods LoggerExt LogMethods Logging RQ Module: RQ

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:

Methods

backup   configure   create   delete   execute   feed   gen_template   init_config   init_logging   list   lock   new   parse_argv   parse_options   query   rotate   run   snapshot   status   submit   update  

Constants

OPTSPEC = #--{{{ [ [ '--priority=priority', '-p', 'modes : set the job(s) priority - lowest(0) .. highest(n) - (default 0)'
  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

Attributes

argv  [R] 
cmd  [R] 
daemon  [R] 
env  [R] 
logger  [R] 
mode  [R] 
options  [R] 
q  [R] 
qpath  [R] 

Included Modules

Util Logging Usage

Public Class methods

given a command line and environment run the rq program

[Source]

     # 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

Public Instance methods

delegated to a Backer

[Source]

     # File bin/rq.rb, line 389
389:       def backup 
390: #--{{{
391:         init_logging
392:         backer = Backer::new self
393:         backer.backup
394: #--}}}
395:       end

delegated to a Configurator

[Source]

     # File bin/rq.rb, line 365
365:       def configure 
366: #--{{{
367:         init_logging
368:         configurator = Configurator::new self
369:         configurator.configure
370: #--}}}
371:       end

delegated to a Creator

[Source]

     # File bin/rq.rb, line 301
301:       def create 
302: #--{{{
303:         init_logging
304:         creator = Creator::new self
305:         creator.create
306: #--}}}
307:       end

delegated to a Deleter

[Source]

     # File bin/rq.rb, line 333
333:       def delete
334: #--{{{
335:         init_logging
336:         deleter = Deleter::new self
337:         deleter.delete
338: #--}}}
339:       end

delegated to a Executor

[Source]

     # File bin/rq.rb, line 357
357:       def execute 
358: #--{{{
359:         init_logging
360:         executor = Executor::new self
361:         executor.execute
362: #--}}}
363:       end

delegated to a Feeder

[Source]

     # File bin/rq.rb, line 405
405:       def feed 
406: #--{{{
407:         feeder = Feeder::new self
408:         feeder.feed
409: #--}}}
410:       end

generate a template/sample config file which can then be edited

[Source]

     # 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

[Source]

     # 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

[Source]

     # 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

delegated to a Lister

[Source]

     # File bin/rq.rb, line 317
317:       def list 
318: #--{{{
319:         init_logging
320:         lister = Lister::new self
321:         lister.list
322: #--}}}
323:       end

delegated to a Locker

[Source]

     # File bin/rq.rb, line 381
381:       def lock
382: #--{{{
383:         init_logging
384:         locker = Locker::new self
385:         locker.lock
386: #--}}}
387:       end

extract command lines args

[Source]

     # 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

[Source]

     # 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

delegated to a Querier

[Source]

     # File bin/rq.rb, line 349
349:       def query
350: #--{{{
351:         init_logging
352:         querier = Querier::new self
353:         querier.query
354: #--}}}
355:       end

delegated to a Rotater

[Source]

     # File bin/rq.rb, line 397
397:       def rotate 
398: #--{{{
399:         init_logging
400:         rotater = Rotater::new self
401:         rotater.rotate
402: #--}}}
403:       end

select a MainHelper based on mode and delegate to it

[Source]

     # 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

[Source]

     # 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

[Source]

     # File bin/rq.rb, line 325
325:       def status 
326: #--{{{
327:         init_logging
328:         statuslister = StatusLister::new self
329:         statuslister.statuslist
330: #--}}}
331:       end

delegated to a Submitter

[Source]

     # File bin/rq.rb, line 309
309:       def submit 
310: #--{{{
311:         init_logging
312:         submitter = Submitter::new self
313:         submitter.submit
314: #--}}}
315:       end

delegated to a Updater

[Source]

     # File bin/rq.rb, line 341
341:       def update 
342: #--{{{
343:         init_logging
344:         updater = Updater::new self
345:         updater.update
346: #--}}}
347:       end

[Validate]