Class: Readorder::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/readorder/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Runner

Returns a new instance of Runner.



8
9
10
11
# File 'lib/readorder/runner.rb', line 8

def initialize( opts = {} )
  @options = opts.dup
  initialize_logging
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/readorder/runner.rb', line 6

def options
  @options
end

Instance Method Details

#initialize_loggingObject



27
28
29
# File 'lib/readorder/runner.rb', line 27

def initialize_logging
  Readorder::Log.init( options )
end

#loggerObject



13
14
15
# File 'lib/readorder/runner.rb', line 13

def logger
  @logger ||= ::Logging::Logger[self]
end

#run(command_name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/readorder/runner.rb', line 31

def run( command_name )
  cmd = Command.find( command_name ).new( @options )
  begin
    setup_signal_handling( cmd )
    cmd.before
    cmd.run
  rescue => e
    logger.error "while running #{command_name} : #{e.message}"
    e.backtrace.each do |l|
      logger.debug l
    end
    cmd.error
  ensure
    cmd.after
  end
end

#setup_signal_handling(cmd) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/readorder/runner.rb', line 17

def setup_signal_handling( cmd )
  %w[ INT QUIT TERM ].each do |s|
    Signal.trap( s ) do
      logger.info "received signal #{s} -- shutting down"
      cmd.shutdown
      exit 1
    end
  end
end