Class: RR::BaseRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyrep/base_runner.rb

Overview

This class implements the base functionality for runners that process table specs.

Direct Known Subclasses

ScanRunner, SyncRunner

Constant Summary collapse

DEFAULT_OPTIONS =

Default options if not overriden in command line

{
  :table_specs => []
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject

Provided options. Possible values:

  • :config_file: path to config file

  • :table_specs: array of table specification strings



18
19
20
# File 'lib/rubyrep/base_runner.rb', line 18

def options
  @options
end

#report_printer_argObject

The specified option parameter for the report printer



24
25
26
# File 'lib/rubyrep/base_runner.rb', line 24

def report_printer_arg
  @report_printer_arg
end

#report_printer_classObject

The class for the selected report printer



21
22
23
# File 'lib/rubyrep/base_runner.rb', line 21

def report_printer_class
  @report_printer_class
end

#selected_progress_printerObject

Returns the command line selected ScanProgressPrinters class



37
38
39
# File 'lib/rubyrep/base_runner.rb', line 37

def selected_progress_printer
  @selected_progress_printer
end

#sessionObject

Returns the active Session. Loads config file and creates session if necessary.



148
149
150
151
152
153
154
# File 'lib/rubyrep/base_runner.rb', line 148

def session
  unless @session
    load options[:config_file]
    @session = Session.new Initializer.configuration
  end
  @session
end

Class Method Details

.run(args) ⇒ Object

Entry points for executing a processing run. args: the array of command line options that were provided by the user.



182
183
184
185
186
187
188
189
190
# File 'lib/rubyrep/base_runner.rb', line 182

def self.run(args)
  runner = new

  status = runner.process_options(args)
  if runner.options
    runner.execute
  end
  status
end

Instance Method Details

#add_specific_options(opts) ⇒ Object

Intended to be overwritten by derived classes that need to add additional options to the provided OptionParser object.



135
136
# File 'lib/rubyrep/base_runner.rb', line 135

def add_specific_options(opts)
end

#create_processor(left_table, right_table) ⇒ Object

Creates a processor that does something with the given table. A processor needs to implement a run method that yields for progress reporting purposes pairs of diff_type and row as defined under DirectTableScan#run.



129
130
131
# File 'lib/rubyrep/base_runner.rb', line 129

def create_processor(left_table, right_table)
  # not implemented in the base class
end

#executeObject

Executes a run based on the established options.



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rubyrep/base_runner.rb', line 165

def execute
  session.configuration.exclude_rubyrep_tables
  table_pairs.each do |table_pair|
    report_printer.scan table_pair[:left], table_pair[:right] do
      processor = create_processor \
        table_pair[:left], table_pair[:right]
      processor.progress_printer = progress_printer
      processor.run do |diff_type, row|
        report_printer.report_difference diff_type, row
      end
    end
  end
  signal_scanning_completion
end

#prepare_table_pairs(table_pairs) ⇒ Object

Intended to be overwritten by derived classes that need to modify the table_pairs.

  • table_pairs: array of table pairs as returned by TableSpecResolver#resolve

Returns the new table pairs array.



142
143
144
# File 'lib/rubyrep/base_runner.rb', line 142

def prepare_table_pairs(table_pairs)
  table_pairs
end

#process_options(args) ⇒ Object

Parses the given command line parameter array. Returns the status (as per UNIX conventions: 1 if parameters were invalid, 0 otherwise)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rubyrep/base_runner.rb', line 57

def process_options(args)
  status = 0
  self.options = DEFAULT_OPTIONS.clone

  parser = OptionParser.new do |opts|
    opts.banner = <<EOS
Usage: #{$0} #{self.class.name.sub(/^.*::(.*)Runner$/, '\\1').downcase} [options] [table_spec] [table_spec] ...

  #{summary_description}

  table_spec can be either:
* a specific table name (e. g. 'users') or
* a pair of (specific) table names (e. g.: 'users,users_backup')
    (In this case the first table in the 'left' database is compared
     with the second table in the 'right' database.)
* a regular expression (e. g. '/^user/') [case insensitive match]
  If no table_specs are provided via command line, the ones from the
  configuration file are used.
EOS
    opts.separator ""
    opts.separator "  Specific options:"

    ScanReportPrinters.on_printer_selection(opts) do |printer_class, arg|
      self.report_printer_class = printer_class
      self.report_printer_arg = arg
    end

    ScanProgressPrinters.on_printer_selection(opts) do |printer|
      self.selected_progress_printer = printer
    end

    opts.on("-c", "--config", "=CONFIG_FILE",
      "Mandatory. Path to configuration file.") do |arg|
      options[:config_file] = arg
    end
    
    add_specific_options(opts)

    opts.on_tail("--help", "Show this message") do
      $stderr.puts opts
      self.options = nil
    end
  end

  begin
    unprocessed_args = parser.parse!(args)
    if options # this will be +nil+ if the --help option is specified
      options[:table_specs] = unprocessed_args
      raise("Please specify configuration file") unless options.include?(:config_file)
    end
  rescue Exception => e
    $stderr.puts "Command line parsing failed: #{e}"
    $stderr.puts parser.help
    self.options = nil
    status = 1
  end

  return status
end

#progress_printerObject

Returns the active ScanProgressPrinter class (as selected through the command line options OR if none was selected, the default one).



41
42
43
44
45
46
47
48
# File 'lib/rubyrep/base_runner.rb', line 41

def progress_printer
  if selected_progress_printer
    selected_progress_printer
  else
    printer_key = session.configuration.options[:scan_progress_printer]
    ScanProgressPrinters.printers[printer_key][:printer_class]
  end
end

#report_printerObject

Returns the active ScanReportPrinters instance (as selected through the command line options OR if none was selected, the default one).



28
29
30
31
32
33
34
# File 'lib/rubyrep/base_runner.rb', line 28

def report_printer
  unless @report_printer
    printer_class = report_printer_class || ScanReportPrinters::ScanSummaryReporter
    @report_printer ||= printer_class.new(session, report_printer_arg)
  end
  @report_printer
end

#signal_scanning_completionObject

Signals scan completion to the (active) scan report printer if it supports that method.



119
120
121
122
123
# File 'lib/rubyrep/base_runner.rb', line 119

def signal_scanning_completion
  if report_printer.respond_to? :scanning_finished
    report_printer.scanning_finished
  end
end

#summary_descriptionObject

Returns the default command summary description (nothing). Should be overwritten by child classes.



52
# File 'lib/rubyrep/base_runner.rb', line 52

def summary_description; ""; end

#table_pairsObject

Returns the table pairs that should be processed. Refer to TableSpecRsolver#resolve for format of return value.



160
161
162
# File 'lib/rubyrep/base_runner.rb', line 160

def table_pairs
  prepare_table_pairs(session.configured_table_pairs(options[:table_specs]))
end