Class: Readorder::Command

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

Overview

The Command is the base class for any class that wants to implement a command line command for

Inheriting from this calss will make the class registered and be available for invocation from the Runner class

The lifecycle of a command is:

1) instantiation with a hash parameter
2) before
3) run
4) after
5) error calld if the runner catches and exception from the command

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Command

Returns a new instance of Command.



30
31
32
33
34
35
36
# File 'lib/readorder/command.rb', line 30

def initialize( opts = {} )
  @options = opts
  @filelist = nil
  @analyzer = nil
  @output = nil
  @delete_results = true
end

Instance Attribute Details

#analyzerObject (readonly)

Returns the value of attribute analyzer.



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

def analyzer
  @analyzer
end

#filelistObject (readonly)

Returns the value of attribute filelist.



26
27
28
# File 'lib/readorder/command.rb', line 26

def filelist
  @filelist
end

#optionsObject (readonly)

Returns the value of attribute options.



25
26
27
# File 'lib/readorder/command.rb', line 25

def options
  @options
end

#outputObject (readonly)

Returns the value of attribute output.



28
29
30
# File 'lib/readorder/command.rb', line 28

def output
  @output
end

Class Method Details

.command_nameObject



21
22
23
# File 'lib/readorder/command.rb', line 21

def self.command_name
  name.split("::").last.downcase
end

.commandsObject

The list of commands registered.



152
153
154
155
156
157
# File 'lib/readorder/command.rb', line 152

def commands
  unless defined? @commands
    @commands = []
  end
  return @commands
end

.find(name) ⇒ Object

get the command klass for the given name



160
161
162
# File 'lib/readorder/command.rb', line 160

def find( name )
  @commands.find { |klass| klass.command_name == name }
end

.inherited(klass) ⇒ Object

this method is invoked by the Ruby interpreter whenever a class inherts from Command. This is how commands register to be invoked



144
145
146
147
148
# File 'lib/readorder/command.rb', line 144

def inherited( klass )
  return unless klass.instance_of? Class
  return if commands.include? klass
  commands << klass
end

Instance Method Details

#afterObject

called by runner when all is done



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/readorder/command.rb', line 123

def after() 
  if options['error-filelist'] then
    if analyzer.bad_data_count > 0 then
      File.open( options['error-filelist'], "w+" ) do |f|
        analyzer.dump_errors_to( f )
      end
      logger.info "wrote error filelist to #{options['error-filelist']}"
    end
  end

  if output != $stdout then
    output.close
    results.close 
    File.unlink( results_dbfile ) if @delete_results
  end
end

#beforeObject

called by the Runner before the command, this can be used to setup additional items for the command



104
# File 'lib/readorder/command.rb', line 104

def before() ; end

#command_nameObject



94
95
96
# File 'lib/readorder/command.rb', line 94

def command_name
  self.class.command_name
end

#errorObject

called by the Runner if an error is encountered during the run method



112
113
114
# File 'lib/readorder/command.rb', line 112

def error() 
  results.close
end

#get_physical?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/readorder/command.rb', line 81

def get_physical?
  return false if @options['inode']
  unless Datum.is_linux? then
    logger.warn "unable to get physical block number, this is not a linux machine, it is #{Config::CONFIG['host_os']}"
    return false
  end
  unless Process.euid == 0 then
    logger.warn "no permissions to get physical block number, try running as root."
    return false
  end
  return true
end

#loggerObject



98
99
100
# File 'lib/readorder/command.rb', line 98

def logger
  ::Logging::Logger[self]
end

#resultsObject



64
65
66
# File 'lib/readorder/command.rb', line 64

def results
  @results ||= Results.new( results_dbfile, options['batch-size'] )
end

#results_dbfileObject



54
55
56
57
58
59
60
61
62
# File 'lib/readorder/command.rb', line 54

def results_dbfile
  if options['output'] then
    output_dname = File.dirname( options['output'] )
    output_bname = File.basename( options['output'], '.*' )
    return File.join( output_dname, "#{output_bname}.db" )
  else 
    return ":memory:"
  end
end

#runObject

called by the Runner to execute the command

Raises:



107
108
109
# File 'lib/readorder/command.rb', line 107

def run
  raise Error, "Unknown command `#{command_name}`"
end

#shutdownObject

called by runner if a signal is hit



117
118
119
120
# File 'lib/readorder/command.rb', line 117

def shutdown() 
  results.close
  @delete_results = false
end